url
stringlengths 36
137
| content
stringlengths 1.21k
7.75M
|
---|---|
https://www.mongodb.com/docs/manual/core/security-scram/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
SCRAM
<a class="headerlink leafygreen-ui-1kepx47" href="#scram" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="scram">
</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="#features" target="_self">
<span>
Features
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#driver-support" target="_self">
<span>
Driver Support
</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>
<p class="leafygreen-ui-1kp3ins">
Salted Challenge Response Authentication Mechanism (SCRAM) is the
default authentication mechanism for MongoDB.
</p>
<p class="leafygreen-ui-1kp3ins">
When a user
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/authenticate-a-user/#std-label-authentication-auth-as-user">
authenticates
</a>
themselves, MongoDB uses SCRAM to verify the supplied user credentials
against the user's
<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
<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>
</p>
<p class="leafygreen-ui-1kp3ins">
SCRAM is based on the IETF
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://tools.ietf.org/html/rfc5802" rel="noopener noreferrer">
<span>
RFC 5802
</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 that defines best
practices for the implementation of challenge-response mechanisms for
authenticating users with passwords.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-authentication-scram-sha-1">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-authentication-scram-sha-256">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-scram-features">
</span>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Features
<a class="headerlink leafygreen-ui-1kepx47" href="#features" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="features">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
MongoDB's implementation of SCRAM provides:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A tunable work factor (the iteration count)
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Per-user random salts
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Bi-directional authentication between server and client
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-scram-mechanisms">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
SCRAM Mechanisms
<a class="headerlink leafygreen-ui-1kepx47" href="#scram-mechanisms" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="scram-mechanisms">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB supports the following SCRAM mechanisms:
</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>
SCRAM Mechanism
</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">
SCRAM-SHA-1
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Uses the SHA-1 hashing function.
</p>
<p class="leafygreen-ui-1kp3ins">
To modify the iteration count for
<code class="leafygreen-ui-1l06pbn">
SCRAM-SHA-1
</code>
, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.scramIterationCount">
<code class="leafygreen-ui-1l06pbn">
scramIterationCount
</code>
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
SCRAM-SHA-256
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Uses the SHA-256 hashing function.
</p>
<p class="leafygreen-ui-1kp3ins">
To modify the iteration count for
<code class="leafygreen-ui-1l06pbn">
SCRAM-SHA-256
</code>
, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.scramSHA256IterationCount">
<code class="leafygreen-ui-1l06pbn">
scramSHA256IterationCount
</code>
.
</a>
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
When you create or update a SCRAM user, you can indicate:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the SCRAM mechanism to use
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
whether the server or the client digests the password
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
When you use
<code class="leafygreen-ui-1l06pbn">
SCRAM-SHA-256
</code>
, MongoDB requires server-side password
hashing, which means that the server digests the password. For more
information, see
<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>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.updateUser/#mongodb-method-db.updateUser">
<code class="leafygreen-ui-1l06pbn">
db.updateUser()
</code>
.
</a>
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Driver Support
<a class="headerlink leafygreen-ui-1kepx47" href="#driver-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="driver-support">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The minimum driver versions that support
<code class="leafygreen-ui-1l06pbn">
SCRAM
</code>
are:
</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>
Driver Language
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
<p class="leafygreen-ui-1kp3ins">
Version
</p>
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Driver Language
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
<p class="leafygreen-ui-1kp3ins">
Version
</p>
</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>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://github.com/mongodb/mongo-c-driver/releases" rel="noopener noreferrer">
<span>
1.1.0
</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>
</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/drivers/php/" target="_self">
<span>
PHP
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://pecl.php.net/package/mongodb" rel="noopener noreferrer">
<span>
1.0
</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>
</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>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://github.com/mongodb/mongo-cxx-driver/releases" rel="noopener noreferrer">
<span>
1.0.0
</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>
</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/drivers/python/" target="_self">
<span>
Python
</span>
</a>
</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.python.org/pypi/pymongo/" rel="noopener noreferrer">
<span>
2.8
</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>
</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#
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://github.com/mongodb/mongo-csharp-driver/releases" rel="noopener noreferrer">
<span>
1.10
</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>
</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/drivers/perl/" target="_self">
<span>
Perl
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://metacpan.org/release/MongoDB" rel="noopener noreferrer">
<span>
1.0.0
</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>
</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>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://github.com/mongodb/mongo-go-driver/releases" rel="noopener noreferrer">
<span>
1.0.0
</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>
</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/drivers/ruby/" target="_self">
<span>
Ruby
</span>
</a>
</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/mongo" rel="noopener noreferrer">
<span>
1.12
</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>
</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/" target="_self">
<span>
Java
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://github.com/mongodb/mongo-java-driver/releases" rel="noopener noreferrer">
<span>
2.13
</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>
</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/drivers/rust/" target="_self">
<span>
Rust
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://github.com/mongodb/mongo-rust-driver/releases" rel="noopener noreferrer">
<span>
1.0.0
</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>
</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>
Motor
</span>
</a>
</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.python.org/pypi/motor/" rel="noopener noreferrer">
<span>
0.4
</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>
</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/drivers/scala/" target="_self">
<span>
Scala
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://github.com/mongodb/casbah/releases" rel="noopener noreferrer">
<span>
2.8.0
</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>
</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>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://github.com/mongodb/node-mongodb-native/releases" rel="noopener noreferrer">
<span>
1.4.29
</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>
</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/drivers/swift/" target="_self">
<span>
Swift
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://github.com/mongodb/mongo-swift-driver/releases" rel="noopener noreferrer">
<span>
1.0.0
</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>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</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>
<p class="leafygreen-ui-1kp3ins">
If you use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#std-label-authentication-parameters">
SCRAM-SHA-1
<!-- -->
:
</a>
</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/glossary/#std-term-md5">
md5
</a>
is necessary but is not used for cryptographic purposes,
and
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
if you use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/configure-fips/#std-label-fips-overview">
FIPS mode
</a>
, then instead of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#std-label-authentication-parameters">
SCRAM-SHA-1
</a>
use:
</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="#std-label-authentication-scram" target="_self">
<span>
SCRAM-SHA-256
<!-- -->
,
</span>
</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
<!-- -->
,
</a>
</p>
</li>
<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">
LDAP
</a>
, or
</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>
</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="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/blog/post/improved-password-based-authentication-mongodb-30-scram-explained-part-1?tck=docs_server" target="_self">
<span>
Blog Post: Improved Password-Based Authentication: SCRAM Explained
(Part 1)
</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/blog/post/improved-password-based-authentication-mongodb-30-scram-explained-part-2?tck=docs_server" target="_self">
<span>
Blog Post: Improved Password-Based Authentication: SCRAM Explained
(Part 2)
</span>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/change-events/rename/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
<code class="leafygreen-ui-1l06pbn">
rename
</code>
Event
<a class="headerlink leafygreen-ui-1kepx47" href="#rename-event" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="rename-event">
</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="#summary" target="_self">
<span>
Summary
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" 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-x9koo9" href="#expanded-event-information" target="_self">
<span>
Expanded Event Information
</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">
Summary
<a class="headerlink leafygreen-ui-1kepx47" href="#summary" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="summary">
</div>
</a>
</h2>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
rename
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-rename" 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-rename">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
A
<code class="leafygreen-ui-1l06pbn">
rename
</code>
event occurs when a collection is renamed.
</p>
</dd>
</dl>
</section>
<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>
<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">
_id
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label--idref--id">
</span>
<p class="leafygreen-ui-1kp3ins">
A
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-BSON">
BSON
</a>
object which serves as an identifier for the
change stream event. This value is used as the
<code class="leafygreen-ui-1l06pbn">
resumeToken
</code>
for the
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
parameter when resuming a change stream.
The
<code class="leafygreen-ui-1l06pbn">
_id
</code>
object has the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_data\" : <bindata|hex>\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">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "_data" : <BinData|hex 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">
The
<code class="leafygreen-ui-1l06pbn">
_data
</code>
type depends on the MongoDB versions
and, in some cases, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setFeatureCompatibilityVersion/#std-label-view-fcv">
feature compatibility version (fCV)
</a>
at the time of the change stream's
opening or resumption. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/changeStreams/#std-label-change-stream-resume-token">
Resume Tokens
</a>
for the
full list of
<code class="leafygreen-ui-1l06pbn">
_data
</code>
types.
</p>
<p class="leafygreen-ui-1kp3ins">
For an example of resuming a change stream by
<code class="leafygreen-ui-1l06pbn">
resumeToken
</code>
, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/changeStreams/#std-label-change-stream-resume">
Resume a Change Stream
<!-- -->
.
</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">
clusterTime
</code>
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label--idref--clusterTime">
</span>
<p class="leafygreen-ui-1kp3ins">
The timestamp from the oplog entry associated with the event.
</p>
<p class="leafygreen-ui-1kp3ins">
Change stream event notifications associated with a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
multi-document transaction
</a>
all have the same
<code class="leafygreen-ui-1l06pbn">
clusterTime
</code>
value: the time when the transaction
was committed.
</p>
<p class="leafygreen-ui-1kp3ins">
Events with the same
<code class="leafygreen-ui-1l06pbn">
clusterTime
</code>
may not all relate to the same transaction.
Some events don't relate to a transaction at all. Starting in MongoDB 8.0,
this may be true for events on any deployment. In previous versions, this
behavior was possible only for events on a sharded cluster.
</p>
<p class="leafygreen-ui-1kp3ins">
To identify events for a single transaction, you can use the
combination of
<code class="leafygreen-ui-1l06pbn">
lsid
</code>
and
<code class="leafygreen-ui-1l06pbn">
txnNumber
</code>
in the change stream
event document.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
8.0
</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">
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>
<span class="leafygreen-ui-1qnjd6d" id="std-label--idref--collectionUUID">
</span>
<p class="leafygreen-ui-1kp3ins">
<span class="leafygreen-ui-vm4wms" tabindex="0">
UUID
</span>
identifying the collection
where the change occurred.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</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">
lsid
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label--idref--lsid">
</span>
<p class="leafygreen-ui-1kp3ins">
The identifier for the session associated with the transaction.
</p>
<p class="leafygreen-ui-1kp3ins">
Only present if the operation is part of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/">
multi-document
transaction
<!-- -->
.
</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">
ns
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label--idref--ns">
</span>
<p class="leafygreen-ui-1kp3ins">
The namespace (database and or collection) affected by the event.
</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">
ns.db
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label--idref--ns.db">
</span>
<p class="leafygreen-ui-1kp3ins">
The name of the database where the event occurred.
</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">
ns.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>
<span class="leafygreen-ui-1qnjd6d" id="std-label--idref--ns.coll">
</span>
<p class="leafygreen-ui-1kp3ins">
The name of the collection where the event occurred.
</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">
operationDescription
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label--idref--operationDescription">
</span>
<p class="leafygreen-ui-1kp3ins">
Additional information on the change operation.
</p>
<p class="leafygreen-ui-1kp3ins">
This document and its subfields only appears when the change stream uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-streams-expanded-events">
expanded events
<!-- -->
.
</a>
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<div class="line-block">
<div class="line">
<code class="leafygreen-ui-1l06pbn">
operationDescription.
</code>
</div>
<div class="line">
<code class="leafygreen-ui-1l06pbn">
dropTarget
</code>
</div>
</div>
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label--idref--od-dropTarget">
</span>
<p class="leafygreen-ui-1kp3ins">
<span class="leafygreen-ui-vm4wms" tabindex="0">
UUID
</span>
of the collection that was dropped
in the rename operation.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<div class="line-block">
<div class="line">
<code class="leafygreen-ui-1l06pbn">
operationDescription.
</code>
</div>
<div class="line">
<code class="leafygreen-ui-1l06pbn">
to
</code>
</div>
</div>
</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--idref--od.to">
</span>
<p class="leafygreen-ui-1kp3ins">
The new namespace of the collection after the rename.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<div class="line-block">
<div class="line">
<code class="leafygreen-ui-1l06pbn">
operationDescription.
</code>
</div>
<div class="line">
<code class="leafygreen-ui-1l06pbn">
to.coll
</code>
</div>
</div>
</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--idref--od.to.coll">
</span>
<p class="leafygreen-ui-1kp3ins">
The new name of the collection after the rename.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<div class="line-block">
<div class="line">
<code class="leafygreen-ui-1l06pbn">
operationDescription.
</code>
</div>
<div class="line">
<code class="leafygreen-ui-1l06pbn">
to.db
</code>
</div>
</div>
</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--idref--od.to.db">
</span>
<p class="leafygreen-ui-1kp3ins">
The new name of the database after the rename.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</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">
operationType
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label--idref--operationType">
</span>
<p class="leafygreen-ui-1kp3ins">
The type of operation that the change notification reports.
</p>
<p class="leafygreen-ui-1kp3ins">
Returns a value of
<code class="leafygreen-ui-1l06pbn">
rename
</code>
for these change events.
</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">
to
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label--idref--to">
</span>
<p class="leafygreen-ui-1kp3ins">
The new namespace of the collection after the rename.
</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">
to.coll
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label--idref--to.coll">
</span>
<p class="leafygreen-ui-1kp3ins">
The new name of the collection after the rename.
</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">
to.db
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label--idref--to.db">
</span>
<p class="leafygreen-ui-1kp3ins">
The new name of the database after the rename.
</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">
txnNumber
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
NumberLong
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label--idref--txnNumber">
</span>
<p class="leafygreen-ui-1kp3ins">
Together with the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label--idref--lsid" target="_self">
<span>
lsid
</span>
</a>
, a number that helps uniquely
identify a transction.
</p>
<p class="leafygreen-ui-1kp3ins">
Only present if the operation is part of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/">
multi-document
transaction
<!-- -->
.
</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">
wallTime
</code>
</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-ISODate">
ISODate
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label--idref--wallTime">
</span>
<p class="leafygreen-ui-1kp3ins">
The server date and time of the database operation.
<code class="leafygreen-ui-1l06pbn">
wallTime
</code>
differs from
<code class="leafygreen-ui-1l06pbn">
clusterTime
</code>
in that
<code class="leafygreen-ui-1l06pbn">
clusterTime
</code>
is a timestamp
taken from the oplog entry associated with the database operation
event.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</div>
</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>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Expanded Event Information
<a class="headerlink leafygreen-ui-1kepx47" href="#expanded-event-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="expanded-event-information">
</div>
</a>
</h3>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0, when the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-streams-expanded-events">
showExpandedEvents
</a>
option is set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
for the change
stream, the
<code class="leafygreen-ui-1l06pbn">
rename
</code>
event includes an
<code class="leafygreen-ui-1l06pbn">
operationDescription
</code>
document.
This document provides a
<code class="leafygreen-ui-1l06pbn">
to
</code>
field showing the changed database and
collection and a
<code class="leafygreen-ui-1l06pbn">
dropTarget
</code>
field indicating whether the
<code class="leafygreen-ui-1l06pbn">
rename
</code>
operation removed the collection before the rename.
</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 example illustrates a
<code class="leafygreen-ui-1l06pbn">
rename
</code>
event:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\": { <resume> },\n \"operationType\": \"rename\",\n \"clusterTime\": <timestamp>,\n \"wallTime\": <isodate>,\n \"ns\": {\n \"db\": \"engineering\",\n \"coll\": \"users\"\n },\n \"to\": {\n \"db\": \"engineering\",\n \"coll\": \"people\"\n },\n \"operationDescription\": {\n \"to\": {\n \"db\": \"engineering\",\n \"coll\": \"people\"\n }\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-attr">"_id"</span><span class="lg-highlight-punctuation">:</span> <span class="lg-highlight-punctuation">{</span> <Resume Token> <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">"rename"</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></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-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">"engineering"</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">"users"</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">"to"</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">"engineering"</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">"people"</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">"operationDescription"</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">"to"</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">"engineering"</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">"people"</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>
<p class="leafygreen-ui-1kp3ins">
A
<code class="leafygreen-ui-1l06pbn">
rename
</code>
event leads to an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/invalidate/#std-label-change-event-invalidate">
invalidate event
</a>
for change streams opened
against its
<code class="leafygreen-ui-1l06pbn">
ns
</code>
collection or
<code class="leafygreen-ui-1l06pbn">
to
</code>
collection.
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$merge (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-merge--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="-merge--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="#considerations" target="_self">
<span>
Considerations
</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>
<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 the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage, which outputs the
aggregation pipeline results to a collection. For the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/mergeObjects/#mongodb-expression-exp.-mergeObjects">
<code class="leafygreen-ui-1l06pbn">
$mergeObjects
</code>
</a>
operator, which merges documents into a
single document, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/mergeObjects/#mongodb-expression-exp.-mergeObjects">
<code class="leafygreen-ui-1l06pbn">
$mergeObjects
</code>
.
</a>
</p>
</div>
</div>
<dl class="pipeline">
<dt>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-pipeline-pipe.-merge" 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.-merge">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Writes the results of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
aggregation pipeline
</a>
to a specified collection. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
operator must be the
<strong>
last
</strong>
stage in the
pipeline.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Can output to a collection in the same or different database.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Can output to the same collection that is being aggregated. For more
information, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-behavior-same-collection" target="_self">
<span>
Output to the Same Collection that is Being Aggregated
<!-- -->
.
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Consider the following points when using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
<code class="leafygreen-ui-1l06pbn">
$out
</code>
</a>
stages in an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
aggregation pipeline
<!-- -->
:
</a>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.0, pipelines with a
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
stage can run on
replica set
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary">
secondary
</a>
nodes if all the nodes in the cluster
have the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setFeatureCompatibilityVersion/#std-label-view-fcv">
featureCompatibilityVersion
</a>
set to
<code class="leafygreen-ui-1l06pbn">
5.0
</code>
or higher and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#std-label-read-preference">
read preference
</a>
allows secondary reads.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
and
<code class="leafygreen-ui-1l06pbn">
$out
</code>
stages run on secondary nodes, but write
operations are sent to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
node.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Not all driver versions support
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
operations sent to
the secondary nodes. For details, see the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
driver
</span>
</a>
documentation.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
In earlier MongoDB versions, pipelines with
<code class="leafygreen-ui-1l06pbn">
$out
</code>
or
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
stages always run on the primary node and read preference isn't
considered.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Creates a new collection if the output collection does not already
exist.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Can incorporate results (insert new documents, merge documents,
replace documents, keep existing documents, fail the operation,
process documents with a custom update pipeline) into an existing
collection.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Can output to a sharded collection. Input collection can
also be sharded.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For a comparison with 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 which also outputs
the aggregation results to a collection, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-out-comparison" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
and
<code class="leafygreen-ui-1l06pbn">
$out
</code>
Comparison
<!-- -->
.
</span>
</a>
</p>
</dd>
</dl>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
On-Demand Materialized Views
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
can incorporate the pipeline results into an
existing output collection rather than perform a full replacement of
the collection. This functionality allows users to create on-demand
materialized views, where the content of the output collection is
incrementally updated when the pipeline is run.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on this use case, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/materialized-views/">
On-Demand Materialized Views
</a>
as well as the examples on this page.
</p>
<p class="leafygreen-ui-1kp3ins">
Materialized views are separate from read-only views. For
information on creating read-only views, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/views/">
read-only views
<!-- -->
.
</a>
</p>
</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">
You can use
<code class="leafygreen-ui-1l06pbn">
$merge
</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-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</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":"{ $merge: {\n into: <collection> -or- { db: <db>, coll: <collection> },\n on: <identifier> -or- [ <identifier>, ...], // Optional\n let: <variables>, // Optional\n whenMatched: <replace|keepexisting|merge|fail|pipeline>, // Optional\n whenNotMatched: <insert|discard|fail> // Optional\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-attr">$merge</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">into</span>: <<!-- -->collection<!-- -->> <!-- -->-<!-- -->or<!-- -->- <!-- -->{ <span class="lg-highlight-attr">db</span>: <<!-- -->db<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">coll</span>: <<!-- -->collection<!-- -->> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">on</span>: <!-- --><identifier field> -or- [ <identifier field1>, ...], // Optional</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> let: <variables>, // Optional</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> whenMatched: <replace|keepExisting|merge|fail|pipeline>, // Optional</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> whenNotMatched: <insert|discard|fail> // Optional</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 example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $merge: { into: \"myOutput\", on: \"_id\", whenMatched: \"replace\", whenNotMatched: \"insert\" } }","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">$merge</span>: <!-- -->{ <span class="lg-highlight-attr">into</span>: <span class="lg-highlight-string">"myOutput"</span>, <span class="lg-highlight-attr">on</span>: <span class="lg-highlight-string">"_id"</span>, <span class="lg-highlight-attr">whenMatched</span>: <span class="lg-highlight-string">"replace"</span>, <span class="lg-highlight-attr">whenNotMatched</span>: <span class="lg-highlight-string">"insert"</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 using all default options for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
, including writing
to a collection in the same database, you can use the simplified form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $merge: <collection> } // Output collection is in the same database","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">$merge</span>: <<!-- -->collection<!-- -->> <!-- -->} <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Output collection is </span><span class="lg-highlight-comment">in the same 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">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</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:17%"/>
<col style="width:83%"/>
</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-merge-into" target="_self">
<span>
into
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-into">
</span>
<p class="leafygreen-ui-1kp3ins">
The output collection. Specify either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The collection name as a string to output to a collection in
the same database where the aggregation is run. For example:
</p>
<div class="line-block">
<div class="line">
<code class="leafygreen-ui-1l06pbn">
into: "myOutput"
</code>
</div>
<div class="line">
<br/>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The database and collection name in a document to output to a
collection in the specified database. For example:
</p>
<div class="line-block">
<div class="line">
<code class="leafygreen-ui-1l06pbn">
into: { db:"myDB", coll:"myOutput" }
</code>
</div>
</div>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
If the output collection does not exist,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
creates the collection:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/replication/#std-label-replica-set">
replica set
</a>
or a standalone, if the output
database does not exist,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
also creates the database.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/sharding/#std-label-sharded-cluster">
sharded cluster
</a>
, the specified output
database must already exist.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The output collection can be a sharded 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-merge-on" target="_self">
<span>
on
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-on">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. Field or fields that act as a unique identifier for a
document. The identifier determines if a results document
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched" target="_self">
<span>
matches
</span>
</a>
an existing document in the
output collection. Specify either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A single field name as a string. For example:
</p>
<div class="line-block">
<div class="line">
<code class="leafygreen-ui-1l06pbn">
on: "_id"
</code>
</div>
<div class="line">
<br/>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A combination of fields in an array. For example:
</p>
<div class="line-block">
<div class="line">
<code class="leafygreen-ui-1l06pbn">
on: [ "date", "customerId" ]
</code>
</div>
</div>
<div class="line-block">
<div class="line">
The order of the fields in the array does not matter, and you
cannot specify the same field multiple times.
</div>
</div>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For the specified field or fields:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The aggregation results documents must contain the field(s)
specified in the
<code class="leafygreen-ui-1l06pbn">
on
</code>
, unless the
<code class="leafygreen-ui-1l06pbn">
on
</code>
field is the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field. If the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field is missing from a
results document, MongoDB adds it automatically.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The specified field or fields cannot contain a null or an array
value.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
requires a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-unique/#std-label-index-type-unique">
unique
</a>
,
index with keys that correspond to the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
identifier fields. Although the order of the index key
specification does not matter, the unique index must only contain
the
<code class="leafygreen-ui-1l06pbn">
on
</code>
fields as its keys.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The index must also have the same
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
collation
</a>
as the aggregation's collation.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The unique index can be a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-sparse/#std-label-index-type-sparse">
sparse
</a>
index.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The unique index cannot be a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-partial/#std-label-index-type-partial">
partial
</a>
index.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For output collections that already exist, the corresponding
index must already exist.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The default value for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
depends on the output collection:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the output collection does not exist, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
identifier must be and defaults to the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field. The
corresponding unique
<code class="leafygreen-ui-1l06pbn">
_id
</code>
index is automatically created.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To use a different
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
identifier field(s)
for a collection that does not exist, you can create the
collection first by creating a unique index on the desired
field(s). See the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-use-different-on" target="_self">
<span>
section on non-existent output
collection
</span>
</a>
for an example.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the existing output collection is unsharded, the
<code class="leafygreen-ui-1l06pbn">
on
</code>
identifier defaults to the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the existing output collection is a sharded collection, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
identifier defaults to all the shard
key fields and the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field. If specifying a different
<code class="leafygreen-ui-1l06pbn">
on
</code>
identifier, the
<code class="leafygreen-ui-1l06pbn">
on
</code>
must contain all the shard key
fields.
</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-merge-whenMatched" target="_self">
<span>
whenMatched
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-whenMatched">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. The behavior of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
if a result document
and an existing document in the collection have the same value
for the specified
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
field(s).
</p>
<p class="leafygreen-ui-1kp3ins">
You can specify either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
One of the pre-defined action strings:
</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>
Action
</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-merge-whenMatched-replace" target="_self">
<span>
"replace"
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-whenMatched-replace">
</span>
<p class="leafygreen-ui-1kp3ins">
Replace the existing document in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-into" target="_self">
<span>
output
collection
</span>
</a>
with the matching results
document.
</p>
<p class="leafygreen-ui-1kp3ins">
When performing a replace, the replacement document
cannot result in a modification of the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
value or,
if the output collection is sharded, the shard key
value. Otherwise, the operation generates an error.
</p>
<p class="leafygreen-ui-1kp3ins">
To avoid this error, if the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
field does not
include the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field, remove the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field in the
aggregation results to avoid the error, such as with a preceding
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
stage, and so on.
</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-merge-whenMatched-keepExisting" target="_self">
<span>
"keepExisting"
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-whenMatched-keepExisting">
</span>
<p class="leafygreen-ui-1kp3ins">
Keep the existing document in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-into" target="_self">
<span>
output
collection
<!-- -->
.
</span>
</a>
</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-merge-whenMatched-merge" target="_self">
<span>
"merge"
</span>
</a>
(Default)
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-whenMatched-merge">
</span>
<p class="leafygreen-ui-1kp3ins">
Merge the matching documents (similar to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/mergeObjects/#mongodb-expression-exp.-mergeObjects">
<code class="leafygreen-ui-1l06pbn">
$mergeObjects
</code>
</a>
operator).
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the results document contains fields not in the
existing document, add these new fields to the
existing document.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the results document contains fields in the existing
document, replace the existing field values with those
from the results document.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For example, if the output collection has the document:
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{ _id: 1, a: 1, b: 1 }
</code>
</p>
<p class="leafygreen-ui-1kp3ins">
And the aggregation results has the document:
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{ _id: 1, b: 5, z: 1 }
</code>
</p>
<p class="leafygreen-ui-1kp3ins">
Then, the merged document is:
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{ _id: 1, a: 1, b: 5, z: 1 }
</code>
</p>
<p class="leafygreen-ui-1kp3ins">
When performing a merge, the merged document cannot
result in a modification of the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
value or, if the
output collection is sharded, the shard key value.
Otherwise, the operation generates an error.
</p>
<p class="leafygreen-ui-1kp3ins">
To avoid this error, if the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
field does not
include the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field, remove the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field in the
aggregation results to avoid the error, such as with a preceding
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
stage, and so on.
</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-merge-whenMatched-fail" target="_self">
<span>
"fail"
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-whenMatched-fail">
</span>
<p class="leafygreen-ui-1kp3ins">
Stop and fail the aggregation operation. Any changes to
the output collection from previous documents are not
reverted.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-whenMatched-pipeline">
</span>
<p class="leafygreen-ui-1kp3ins">
An aggregation pipeline to update the document in the
collection.
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
[ <stage1>, <stage2> ... ]
</code>
</p>
<p class="leafygreen-ui-1kp3ins">
The pipeline can only consist of the following 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>
<p class="leafygreen-ui-1kp3ins">
The pipeline cannot modify the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
field's
value. For example, if you are matching on the field
<code class="leafygreen-ui-1l06pbn">
month
</code>
,
the pipeline cannot modify the
<code class="leafygreen-ui-1l06pbn">
month
</code>
field.
</p>
<p class="leafygreen-ui-1kp3ins">
The whenMatched
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
can directly access the fields of
the existing documents in the
<strong>
output
</strong>
collection using
<code class="leafygreen-ui-1l06pbn">
$<field>
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
To access the fields from the aggregation results documents,
use either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The built-in
<code class="leafygreen-ui-1l06pbn">
$$new
</code>
variable to access the field.
Specifically,
<code class="leafygreen-ui-1l06pbn">
$$new.<field>
</code>
. The
<code class="leafygreen-ui-1l06pbn">
$$new
</code>
variable is
only available if the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-let" target="_self">
<span>
let
</span>
</a>
specification is
omitted.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The user-defined variables in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-let" target="_self">
<span>
let
</span>
</a>
field.
</p>
<p class="leafygreen-ui-1kp3ins">
Specify the double dollar sign ($$) prefix together with the variable
name in the form
<code class="leafygreen-ui-1l06pbn">
$$<variable_name>
</code>
. For example,
<code class="leafygreen-ui-1l06pbn">
$$year
</code>
. If the
variable is set to a document, you can also include a document field in
the form
<code class="leafygreen-ui-1l06pbn">
$$<variable_name>.<field>
</code>
. For example,
<code class="leafygreen-ui-1l06pbn">
$$year.month
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
For more examples, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-variables-example" target="_self">
<span>
Use Variables to Customize the Merge
<!-- -->
.
</span>
</a>
</p>
</li>
</ul>
</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-merge-let" target="_self">
<span>
let
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-let">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. Specifies variables for use in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-pipeline" target="_self">
<span>
whenMatched
pipeline
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Specify a document with the variable names and value expressions:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ <variable_name_1>: <expression_1>,\n ...,\n <variable_name_n>: <expression_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">{ <<!-- -->variable_name_1<!-- -->><!-- -->: <<!-- -->expression_1<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <<!-- -->variable_name_n<!-- -->><!-- -->: <<!-- -->expression_n<!-- -->> <!-- -->}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If unspecified, defaults to
<code class="leafygreen-ui-1l06pbn">
{ new: "$$ROOT" }
</code>
(see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/aggregation-variables/#mongodb-variable-variable.ROOT">
<code class="leafygreen-ui-1l06pbn">
ROOT
</code>
</a>
). The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-pipeline" target="_self">
<span>
whenMatched pipeline
</span>
</a>
can access the
<code class="leafygreen-ui-1l06pbn">
$$new
</code>
variable.
</p>
<p class="leafygreen-ui-1kp3ins">
To access the variables in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-pipeline" target="_self">
<span>
whenMatched pipeline
<!-- -->
:
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Specify the double dollar sign ($$) prefix together with the variable
name in the form
<code class="leafygreen-ui-1l06pbn">
$$<variable_name>
</code>
. For example,
<code class="leafygreen-ui-1l06pbn">
$$year
</code>
. If the
variable is set to a document, you can also include a document field in
the form
<code class="leafygreen-ui-1l06pbn">
$$<variable_name>.<field>
</code>
. For example,
<code class="leafygreen-ui-1l06pbn">
$$year.month
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
For examples, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-variables-example" target="_self">
<span>
Use Variables to Customize the Merge
<!-- -->
.
</span>
</a>
</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-merge-whenNotMatched" target="_self">
<span>
whenNotMatched
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-whenNotMatched">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. The behavior of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
if a result document
does not match an existing document in the out collection.
</p>
<p class="leafygreen-ui-1kp3ins">
You can specify one of the pre-defined action strings:
</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>
Action
</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-merge-whenNotMatched-insert" target="_self">
<span>
"insert"
</span>
</a>
(Default)
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-whenNotMatched-insert">
</span>
<p class="leafygreen-ui-1kp3ins">
Insert the document into the output 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-merge-whenNotMatched-discard" target="_self">
<span>
"discard"
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-whenNotMatched-discard">
</span>
<p class="leafygreen-ui-1kp3ins">
Discard the document. Specifically,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
does
not insert the document into the output 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-merge-whenNotMatched-fail" target="_self">
<span>
"fail"
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-whenNotMatched-fail">
</span>
<p class="leafygreen-ui-1kp3ins">
Stop and fail the aggregation operation. Any changes
already written to the output collection are not
reverted.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-aggregation-merge-behavior">
</span>
</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">
<code class="leafygreen-ui-1l06pbn">
_id
</code>
Field Generation
<a class="headerlink leafygreen-ui-1kepx47" href="#_id-field-generation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-field-generation">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field is not present in a document from the
aggregation pipeline results, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage generates
it automatically.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, in the following aggregation pipeline,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
excludes the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field from the documents
passed into
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
. When
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
writes these
documents to the
<code class="leafygreen-ui-1l06pbn">
"newCollection"
</code>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
generates a
new
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field and value.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sales.aggregate( [\n { $project: { _id: 0 } },\n { $merge : { into : \"newCollection\" } }\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">sales</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">_id</span>: <span class="lg-highlight-number">0</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->$merge<!-- --> <!-- -->: <!-- -->{ <!-- -->into <!-- -->: <span class="lg-highlight-string">"newCollection"</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-merge-behavior-create-collection">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Create a New Collection if Output Collection is Non-Existent
<a class="headerlink leafygreen-ui-1kepx47" href="#create-a-new-collection-if-output-collection-is-non-existent" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-new-collection-if-output-collection-is-non-existent">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
operation creates a new collection if the
specified output collection does not exist.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The output collection is created when
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
writes
the first document into the collection and is immediately visible.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the aggregation fails, any writes completed by the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
before the error will not be rolled back.
</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">
For a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/replication/#std-label-replica-set">
replica set
</a>
or a standalone, if the
output database does not exist,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
also creates
the database.
</p>
<p class="leafygreen-ui-1kp3ins">
For a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/sharding/#std-label-sharded-cluster">
sharded cluster
</a>
, the specified
output database must already exist.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-use-different-on">
</span>
<p class="leafygreen-ui-1kp3ins">
If the output collection does not exist,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
requires
the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
identifier to be the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field. To use a
different
<code class="leafygreen-ui-1l06pbn">
on
</code>
field value for a collection that does not exist, you
can create the collection first by creating a unique index on the
desired field(s) first. For example, if the output collection
<code class="leafygreen-ui-1l06pbn">
newDailySales201905
</code>
does not exist and you want to specify the
<code class="leafygreen-ui-1l06pbn">
salesDate
</code>
field as the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
identifier:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.newDailySales201905.createIndex( { salesDate: 1 }, { unique: true } )\n\ndb.sales.aggregate( [\n { $match: { date: { $gte: new Date(\"2019-05-01\"), $lt: new Date(\"2019-06-01\") } } },\n { $group: { _id: { $dateToString: { format: \"%Y-%m-%d\", date: \"$date\" } }, totalqty: { $sum: \"$quantity\" } } },\n { $project: { _id: 0, salesDate: { $toDate: \"$_id\" }, totalqty: 1 } },\n { $merge : { into : \"newDailySales201905\", on: \"salesDate\" } }\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">newDailySales201905</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <span class="lg-highlight-attr">salesDate</span>: <span class="lg-highlight-number">1</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">unique</span>: <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">db.<span class="lg-highlight-property">sales</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-attr">date</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-05-01"</span>)<!-- -->, <span class="lg-highlight-attr">$lt</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-06-01"</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-attr">$dateToString</span>: <!-- -->{ <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"%Y-%m-%d"</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-string">"$date"</span> } }<!-- -->, <span class="lg-highlight-attr">totalqty</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$quantity"</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">salesDate</span>: <!-- -->{ <span class="lg-highlight-attr">$toDate</span>: <span class="lg-highlight-string">"$_id"</span> }<!-- -->, <span class="lg-highlight-attr">totalqty</span>: <span class="lg-highlight-number">1</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->$merge<!-- --> <!-- -->: <!-- -->{ <!-- -->into <!-- -->: <span class="lg-highlight-string">"newDailySales201905"</span>, <span class="lg-highlight-attr">on</span>: <span class="lg-highlight-string">"salesDate"</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">
Output to a Sharded Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#output-to-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="output-to-a-sharded-collection">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage can output to a sharded collection.
When the output collection is sharded,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
uses
the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field and all the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-shard-a-collection/#std-label-sharding-shard-key-creation">
shard key
</a>
fields as the default
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
identifier. If you override the default, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
identifier must include all the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-shard-a-collection/#std-label-sharding-shard-key-creation">
shard key
</a>
fields:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $merge: {\n into: \"<shardedcoll>\" or { db:\"<sharding>\", coll: \"<shardedcoll>\" },\n on: [ \"<shardkeyfield1>\", \"<shardkeyfield2>\",... ], // Shard key fields and any additional fields\n let: <variables>, // Optional\n whenMatched: <replace|keepexisting|merge|fail|pipeline>, // Optional\n whenNotMatched: <insert|discard|fail> // Optional\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-attr">$merge</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">into</span>: <span class="lg-highlight-string">"<shardedColl>"</span> or <!-- -->{ <span class="lg-highlight-attr">db</span>:<span class="lg-highlight-string">"<sharding enabled db>"</span>, <span class="lg-highlight-attr">coll</span>: <span class="lg-highlight-string">"<shardedColl>"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">on</span>: <!-- -->[ <span class="lg-highlight-string">"<shardkeyfield1>"</span>, <span class="lg-highlight-string">"<shardkeyfield2>"</span>,<!-- -->...<!-- --> ]<!-- -->, <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Shard key fields </span><span class="lg-highlight-comment">and any additional fields</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">let</span>: <<!-- -->variables<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-comment">// Optional</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">whenMatched</span>: <!-- --><replace|keepExisting|merge|fail|pipeline>, // Optional</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> whenNotMatched: <insert|discard|fail> // Optional</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 example, use the
<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>
method
to create a new sharded collection
<code class="leafygreen-ui-1l06pbn">
newrestaurants
</code>
with the
<code class="leafygreen-ui-1l06pbn">
postcode
</code>
field as the shard key.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.shardCollection(\n \"exampledb.newrestaurants\", // Namespace of the collection to shard\n { postcode: 1 }, // Shard key\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">shardCollection</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"exampledb.newrestaurants"</span>, <span class="lg-highlight-comment">// Namespace of</span><span class="lg-highlight-comment"> the collection to </span><span class="lg-highlight-comment">shard</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">postcode</span>: <span class="lg-highlight-number">1</span> }<!-- -->, <span class="lg-highlight-comment">// Shard 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>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
newrestaurants
</code>
collection will contain documents with
information on new restaurant openings by month (
<code class="leafygreen-ui-1l06pbn">
date
</code>
field) and
postcode (shard key); specifically, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
identifier is
<code class="leafygreen-ui-1l06pbn">
["date", "postcode"]
</code>
(the ordering of the fields
does not matter). Because
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
requires a unique
index with keys that correspond to the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
identifier fields, create the unique index
(the ordering of the fields do not matter):
<a class="footnote-reference header-buffer" href="#footnote-uniqueoption" id="ref-uniqueoption-id1">
[
<!-- -->
1
<!-- -->
]
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use exampledb\ndb.newrestaurants.createIndex( { postcode: 1, date: 1 }, { unique: 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">use exampledb</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">newrestaurants</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <span class="lg-highlight-attr">postcode</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">date</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">
With the sharded collection
<code class="leafygreen-ui-1l06pbn">
restaurants
</code>
and the unique index
created, you can use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
to output the aggregation
results to this collection, matching on
<code class="leafygreen-ui-1l06pbn">
[ "date", "postcode" ]
</code>
as in this example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use exampledb\n\ndb.openings.aggregate([\n { $group: {\n _id: { date: { $dateToString: { format: \"%Y-%m\", date: \"$date\" } }, postcode: \"$postcode\" },\n restaurants: { $push: \"$restaurantName\" } } },\n { $project: { _id: 0, postcode: \"$_id.postcode\", date: \"$_id.date\", restaurants: 1 } },\n { $merge: { into: \"newrestaurants\", \"on\": [ \"date\", \"postcode\" ], whenMatched: \"replace\", whenNotMatched: \"insert\" } }\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 exampledb</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">openings</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">$group</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <!-- -->{ <span class="lg-highlight-attr">date</span>: <!-- -->{ <span class="lg-highlight-attr">$dateToString</span>: <!-- -->{ <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"%Y-%m"</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-string">"$date"</span> } }<!-- -->, <span class="lg-highlight-attr">postcode</span>: <span class="lg-highlight-string">"$postcode"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">restaurants</span>: <!-- -->{ <span class="lg-highlight-attr">$push</span>: <span class="lg-highlight-string">"$restaurantName"</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">postcode</span>: <span class="lg-highlight-string">"$_id.postcode"</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-string">"$_id.date"</span>, <span class="lg-highlight-attr">restaurants</span>: <span class="lg-highlight-number">1</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$merge</span>: <!-- -->{ <span class="lg-highlight-attr">into</span>: <span class="lg-highlight-string">"newrestaurants"</span>, <span class="lg-highlight-string">"on"</span>: <!-- -->[ <span class="lg-highlight-string">"date"</span>, <span class="lg-highlight-string">"postcode"</span> ]<!-- -->, <span class="lg-highlight-attr">whenMatched</span>: <span class="lg-highlight-string">"replace"</span>, <span class="lg-highlight-attr">whenNotMatched</span>: <span class="lg-highlight-string">"insert"</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>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-uniqueoption" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<a href="#ref-uniqueoption-id1">
1
</a>
]
</td>
<td class="leafygreen-ui-17r2xs0">
<!-- -->
The
<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>
method can also create a
unique index on the shard key when passed the
<code class="leafygreen-ui-1l06pbn">
{ unique: true
}
</code>
option if: the shard key is
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/ranged-sharding/">
range-based
</a>
, the collection is empty, and a unique
index on the shard key doesn't already exist.
<!-- -->
In the previous example, because the
<code class="leafygreen-ui-1l06pbn">
on
</code>
identifier is the
shard key and another field, a separate operation to create the
corresponding index is required.
</td>
</tr>
</tbody>
</table>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-behavior-replaceCollection">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Replace Documents (
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
) vs Replace Collection (
<code class="leafygreen-ui-1l06pbn">
$out
</code>
)
<a class="headerlink leafygreen-ui-1kepx47" href="#replace-documents---merge--vs-replace-collection---out-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-documents---merge--vs-replace-collection---out-">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
can replace an existing document in the output
collection if the aggregation results contain a document or
documents that match based on the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
specification. As such,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
can replace all documents
in the existing collection if the aggregation results include
matching documents for all existing documents in the collection and
you specify
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-replace" target="_self">
<span>
"replace"
</span>
</a>
for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched" target="_self">
<span>
whenMatched
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
However,
<strong>
to replace an existing collection
</strong>
regardless of the
aggregation results, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
<code class="leafygreen-ui-1l06pbn">
$out
</code>
</a>
instead.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Existing Documents and
<code class="leafygreen-ui-1l06pbn">
_id
</code>
and Shard Key Values
<a class="headerlink leafygreen-ui-1kepx47" href="#existing-documents-and-_id-and-shard-key-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="existing-documents-and-_id-and-shard-key-values">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
errors if the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
results in a
change to an existing document's
<code class="leafygreen-ui-1l06pbn">
_id
</code>
value.
</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">
To avoid this error, if the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
field does not
include the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field, remove the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field in the
aggregation results to avoid the error, such as with a preceding
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
stage, and so on.
</p>
<p class="leafygreen-ui-1kp3ins">
Additionally, for a sharded collection,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
also
generates an error if it results in a change to the shard key value
of an exising document.
</p>
<p class="leafygreen-ui-1kp3ins">
Any writes completed by the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
before the error will
not be rolled back.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-unique-index-constraints">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Unique Index Constraints
<a class="headerlink leafygreen-ui-1kepx47" href="#unique-index-constraints" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-index-constraints">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the unique index used by
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
field(s) is dropped mid-aggregation, there is no
guarantee that the aggregation will be killed. If the aggregation
continues, there is no guarantee that documents do not have
duplicate
<code class="leafygreen-ui-1l06pbn">
on
</code>
field values.
</p>
<p class="leafygreen-ui-1kp3ins">
If the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
attempts to write a document that violates
any unique index on the output collection, the operation generates an
error. For example:
</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="#std-label-merge-whenNotMatched-insert" target="_self">
<span>
Insert a non-matching document
</span>
</a>
that violates a unique index other
than the index on the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
field(s).
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-fail" target="_self">
<span>
Fail
</span>
</a>
if there is a matching
document in the collection. Specifically, the operation attempts
to insert the matching document which violates the unique index on
the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
field(s).
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-replace" target="_self">
<span>
Replace an existing document
</span>
</a>
with a new document that
violates a unique index other than the index on the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
field(s).
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-merge" target="_self">
<span>
Merge the matching documents
</span>
</a>
that
results in a document that violates a unique index other than the
index on the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
field(s).
</p>
</li>
</ul>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Schema Validation
<a class="headerlink leafygreen-ui-1kepx47" href="#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="schema-validation">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If your collection uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/schema-validation/#std-label-schema-validation-overview">
schema validation
</a>
and has
<code class="leafygreen-ui-1l06pbn">
validationAction
</code>
set to
<code class="leafygreen-ui-1l06pbn">
error
</code>
, inserting an invalid document or updating a document with
invalid values with
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
throws a
<code class="leafygreen-ui-1l06pbn">
MongoServerError
</code>
and the
document is not written to the target collection. If there are multiple
invalid documents, only the first invalid document encountered throws an
error. All valid documents are written to the target collection, and all
invalid documents fail to write.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
whenMatched
</code>
Pipeline Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#whenmatched-pipeline-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="whenmatched-pipeline-behavior">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If all of the following are true for a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
inserts the document directly into the output
collection:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The value of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched" target="_self">
<span>
whenMatched
</span>
</a>
is an
aggregation pipeline,
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The value of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenNotMatched" target="_self">
<span>
whenNotMatched
</span>
</a>
is
<code class="leafygreen-ui-1l06pbn">
insert
</code>
, and
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
There is no match for a document in the output collection,
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
inserts the document directly into the output
collection.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-out-comparison">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
and
<code class="leafygreen-ui-1l06pbn">
$out
</code>
Comparison
<a class="headerlink leafygreen-ui-1kepx47" href="#-merge-and--out-comparison" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-merge-and--out-comparison">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
With the introduction of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
, MongoDB provides two
stages,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
<code class="leafygreen-ui-1l06pbn">
$out
</code>
</a>
, for writing the
results of the aggregation pipeline to a collection:
</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>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
<code class="leafygreen-ui-1l06pbn">
$out
</code>
</a>
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Can output to a collection in the same or different database.
</p>
</li>
</ul>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Can output to a collection in the same or different database.
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Creates a new collection if the output collection does not
already exist.
</p>
</li>
</ul>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Creates a new collection if the output collection does not
already exist.
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Can incorporate results (insert new documents, merge
documents, replace documents, keep existing documents, fail
the operation, process documents with a custom update pipeline) into
an existing collection.
</p>
<div class="line-block">
<div class="line">
See also
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-behavior-replaceCollection" target="_self">
<span>
Replace Documents (
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
) vs Replace Collection (
<code class="leafygreen-ui-1l06pbn">
$out
</code>
)
</span>
</a>
.
</div>
</div>
</li>
</ul>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Replaces the output collection completely if it already exists.
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Can output to a sharded collection. Input collection can
also be sharded.
</p>
</li>
</ul>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Cannot output to a sharded collection. Input collection,
however, can be sharded.
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Corresponds to SQL statements:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
MERGE
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
INSERT INTO T2 SELECT FROM T1
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
SELECT INTO T2 FROM T1
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Create/Refresh Materialized Views.
</p>
</li>
</ul>
</li>
</ul>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Corresponds to SQL statement:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
INSERT INTO T2 SELECT FROM T1
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
SELECT INTO T2 FROM T1
</code>
.
</p>
</li>
</ul>
</li>
</ul>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-behavior-same-collection">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Output to the Same Collection that is Being Aggregated
<a class="headerlink leafygreen-ui-1kepx47" href="#output-to-the-same-collection-that-is-being-aggregated" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-to-the-same-collection-that-is-being-aggregated">
</div>
</a>
</h3>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
When
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
outputs to the same collection that is being
aggregated, documents may get updated multiple times or the operation
may result in an infinite loop. This behavior occurs when the update
performed by
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
changes the physical location of
documents stored on disk. When the physical location of a document
changes,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
may view it as an entirely new document,
resulting in additional updates. For more information on this
behavior, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/wiki/Halloween_Problem" rel="noopener noreferrer">
<span>
Halloween Problem
<!-- -->
.
</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>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
can output to the same collection that is being aggregated.
You can also output to a collection which appears in other stages of the
pipeline, such as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup">
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
.
</a>
</p>
</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>
<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>
Restrictions
</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>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
transactions
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
An aggregation pipeline cannot use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
inside a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions-operations/#std-label-transactions-operations-crud">
transaction
<!-- -->
.
</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/core/timeseries-collections/#std-label-manual-timeseries-landing">
Time Series Collections
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
An aggregation pipeline cannot use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
to output to
a time series collection.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<div class="line-block">
<div class="line">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/views/#std-label-views-landing-page">
view definition
</a>
</div>
<div class="line">
<em>
Separate from materialized view
</em>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A view definition cannot include the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage.
If the view definition includes nested pipeline (for example,
the view definition includes
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/facet/#mongodb-pipeline-pipe.-facet">
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
</a>
stage), this
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage restriction applies to the nested
pipelines as well.
</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/lookup/#mongodb-pipeline-pipe.-lookup">
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</a>
stage
</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/lookup/#mongodb-pipeline-pipe.-lookup">
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</a>
stage's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#std-label-lookup-syntax-let-pipeline">
nested pipeline
</a>
cannot include the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage.
</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/facet/#mongodb-pipeline-pipe.-facet">
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
</a>
stage
</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/facet/#mongodb-pipeline-pipe.-facet">
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
</a>
stage's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#std-label-lookup-syntax-let-pipeline">
nested pipeline
</a>
cannot include the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage.
</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/unionWith/#mongodb-pipeline-pipe.-unionWith">
<code class="leafygreen-ui-1l06pbn">
$unionWith
</code>
</a>
stage
</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/unionWith/#mongodb-pipeline-pipe.-unionWith">
<code class="leafygreen-ui-1l06pbn">
$unionWith
</code>
</a>
stage's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unionWith/#std-label-unionWith-pipeline">
nested pipeline
</a>
cannot include the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage.
</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/read-concern-linearizable/#mongodb-readconcern-readconcern.-linearizable-">
<code class="leafygreen-ui-1l06pbn">
"linearizable"
</code>
</a>
read concern
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage cannot be used in conjunction with read
concern
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-linearizable/#mongodb-readconcern-readconcern.-linearizable-">
<code class="leafygreen-ui-1l06pbn">
"linearizable"
</code>
</a>
. That is, if you specify
<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 for
<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>
, you cannot include the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage in the pipeline.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-mat-view-init-creation">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
On-Demand Materialized View: Initial Creation
<a class="headerlink leafygreen-ui-1kepx47" href="#on-demand-materialized-view--initial-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="on-demand-materialized-view--initial-creation">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the output collection does not exist, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
creates
the collection.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, a collection named
<code class="leafygreen-ui-1l06pbn">
salaries
</code>
in the
<code class="leafygreen-ui-1l06pbn">
zoo
</code>
database
is populated with the employee salary and department history:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"zoo\").salaries.insertMany([\n { \"_id\" : 1, employee: \"Ant\", dept: \"A\", salary: 100000, fiscal_year: 2017 },\n { \"_id\" : 2, employee: \"Bee\", dept: \"A\", salary: 120000, fiscal_year: 2017 },\n { \"_id\" : 3, employee: \"Cat\", dept: \"Z\", salary: 115000, fiscal_year: 2017 },\n { \"_id\" : 4, employee: \"Ant\", dept: \"A\", salary: 115000, fiscal_year: 2018 },\n { \"_id\" : 5, employee: \"Bee\", dept: \"Z\", salary: 145000, fiscal_year: 2018 },\n { \"_id\" : 6, employee: \"Cat\", dept: \"Z\", salary: 135000, fiscal_year: 2018 },\n { \"_id\" : 7, employee: \"Gecko\", dept: \"A\", salary: 100000, fiscal_year: 2018 },\n { \"_id\" : 8, employee: \"Ant\", dept: \"A\", salary: 125000, fiscal_year: 2019 },\n { \"_id\" : 9, employee: \"Bee\", dept: \"Z\", salary: 160000, fiscal_year: 2019 },\n { \"_id\" : 10, employee: \"Cat\", dept: \"Z\", salary: 150000, fiscal_year: 2019 }\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">"zoo"</span>).<span class="lg-highlight-property">salaries</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-attr">employee</span>: <span class="lg-highlight-string">"Ant"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">100000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2017</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-attr">employee</span>: <span class="lg-highlight-string">"Bee"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">120000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2017</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-attr">employee</span>: <span class="lg-highlight-string">"Cat"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">115000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2017</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-attr">employee</span>: <span class="lg-highlight-string">"Ant"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">115000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2018</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-attr">employee</span>: <span class="lg-highlight-string">"Bee"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">145000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2018</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-attr">employee</span>: <span class="lg-highlight-string">"Cat"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">135000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2018</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">7</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"Gecko"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">100000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2018</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">8</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"Ant"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">125000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2019</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">9</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"Bee"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">160000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2019</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"Cat"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">150000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2019</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 use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stages to
initially create a collection named
<code class="leafygreen-ui-1l06pbn">
budgets
</code>
(in the
<code class="leafygreen-ui-1l06pbn">
reporting
</code>
database) from the data currently in the
<code class="leafygreen-ui-1l06pbn">
salaries
</code>
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">
For a replica set or a standalone deployment, if the output
database does not exist,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
also creates the
database.
</p>
<p class="leafygreen-ui-1kp3ins">
For a sharded cluster deployment, the specified output database
must already exist.
</p>
</div>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"zoo\").salaries.aggregate( [\n { $group: { _id: { fiscal_year: \"$fiscal_year\", dept: \"$dept\" }, salaries: { $sum: \"$salary\" } } },\n { $merge : { into: { db: \"reporting\", coll: \"budgets\" }, on: \"_id\", whenMatched: \"replace\", whenNotMatched: \"insert\" } }\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">"zoo"</span>).<span class="lg-highlight-property">salaries</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">$group</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <!-- -->{ <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-string">"$fiscal_year"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"$dept"</span> }<!-- -->, <span class="lg-highlight-attr">salaries</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$salary"</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->$merge<!-- --> <!-- -->: <!-- -->{ <span class="lg-highlight-attr">into</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"reporting"</span>, <span class="lg-highlight-attr">coll</span>: <span class="lg-highlight-string">"budgets"</span> }<!-- -->, <span class="lg-highlight-attr">on</span>: <span class="lg-highlight-string">"_id"</span>, <span class="lg-highlight-attr">whenMatched</span>: <span class="lg-highlight-string">"replace"</span>, <span class="lg-highlight-attr">whenNotMatched</span>: <span class="lg-highlight-string">"insert"</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>
<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/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
stage to group the salaries by the
<code class="leafygreen-ui-1l06pbn">
fiscal_year
</code>
and
<code class="leafygreen-ui-1l06pbn">
dept
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage writes the output of the preceding
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
stage to the
<code class="leafygreen-ui-1l06pbn">
budgets
</code>
collection in the
<code class="leafygreen-ui-1l06pbn">
reporting
</code>
database.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
To view the documents in the new
<code class="leafygreen-ui-1l06pbn">
budgets
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"reporting\").budgets.find().sort( { _id: 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">getSiblingDB</span>(<span class="lg-highlight-string">"reporting"</span>).<span class="lg-highlight-property">budgets</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">_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">
The
<code class="leafygreen-ui-1l06pbn">
budgets
</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\" : { \"fiscal_year\" : 2017, \"dept\" : \"A\" }, \"salaries\" : 220000 }\n{ \"_id\" : { \"fiscal_year\" : 2017, \"dept\" : \"Z\" }, \"salaries\" : 115000 }\n{ \"_id\" : { \"fiscal_year\" : 2018, \"dept\" : \"A\" }, \"salaries\" : 215000 }\n{ \"_id\" : { \"fiscal_year\" : 2018, \"dept\" : \"Z\" }, \"salaries\" : 280000 }\n{ \"_id\" : { \"fiscal_year\" : 2019, \"dept\" : \"A\" }, \"salaries\" : 125000 }\n{ \"_id\" : { \"fiscal_year\" : 2019, \"dept\" : \"Z\" }, \"salaries\" : 310000 }","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-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2017</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">220000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2017</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">115000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2018</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">215000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2018</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">280000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2019</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">125000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2019</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">310000</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/core/materialized-views/">
On-Demand Materialized Views
</a>
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-mat-view-refresh">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
On-Demand Materialized View: Update/Replace Data
<a class="headerlink leafygreen-ui-1kepx47" href="#on-demand-materialized-view--update-replace-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="on-demand-materialized-view--update-replace-data">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following example uses the collections in the previous example.
</p>
<p class="leafygreen-ui-1kp3ins">
The example
<code class="leafygreen-ui-1l06pbn">
salaries
</code>
collection contains the
employee salary and department history:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, employee: \"Ant\", dept: \"A\", salary: 100000, fiscal_year: 2017 },\n{ \"_id\" : 2, employee: \"Bee\", dept: \"A\", salary: 120000, fiscal_year: 2017 },\n{ \"_id\" : 3, employee: \"Cat\", dept: \"Z\", salary: 115000, fiscal_year: 2017 },\n{ \"_id\" : 4, employee: \"Ant\", dept: \"A\", salary: 115000, fiscal_year: 2018 },\n{ \"_id\" : 5, employee: \"Bee\", dept: \"Z\", salary: 145000, fiscal_year: 2018 },\n{ \"_id\" : 6, employee: \"Cat\", dept: \"Z\", salary: 135000, fiscal_year: 2018 },\n{ \"_id\" : 7, employee: \"Gecko\", dept: \"A\", salary: 100000, fiscal_year: 2018 },\n{ \"_id\" : 8, employee: \"Ant\", dept: \"A\", salary: 125000, fiscal_year: 2019 },\n{ \"_id\" : 9, employee: \"Bee\", dept: \"Z\", salary: 160000, fiscal_year: 2019 },\n{ \"_id\" : 10, employee: \"Cat\", dept: \"Z\", salary: 150000, fiscal_year: 2019 }","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-attr">employee</span>: <span class="lg-highlight-string">"Ant"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">100000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2017</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-attr">employee</span>: <span class="lg-highlight-string">"Bee"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">120000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2017</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-attr">employee</span>: <span class="lg-highlight-string">"Cat"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">115000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2017</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-attr">employee</span>: <span class="lg-highlight-string">"Ant"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">115000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2018</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-attr">employee</span>: <span class="lg-highlight-string">"Bee"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">145000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2018</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-attr">employee</span>: <span class="lg-highlight-string">"Cat"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">135000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2018</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">7</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"Gecko"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">100000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2018</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">8</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"Ant"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">125000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2019</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">9</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"Bee"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">160000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2019</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"Cat"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">150000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2019</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 example
<code class="leafygreen-ui-1l06pbn">
budgets
</code>
collection contains the cumulative yearly
budgets:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : { \"fiscal_year\" : 2017, \"dept\" : \"A\" }, \"salaries\" : 220000 }\n{ \"_id\" : { \"fiscal_year\" : 2017, \"dept\" : \"Z\" }, \"salaries\" : 115000 }\n{ \"_id\" : { \"fiscal_year\" : 2018, \"dept\" : \"A\" }, \"salaries\" : 215000 }\n{ \"_id\" : { \"fiscal_year\" : 2018, \"dept\" : \"Z\" }, \"salaries\" : 280000 }\n{ \"_id\" : { \"fiscal_year\" : 2019, \"dept\" : \"A\" }, \"salaries\" : 125000 }\n{ \"_id\" : { \"fiscal_year\" : 2019, \"dept\" : \"Z\" }, \"salaries\" : 310000 }","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-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2017</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">220000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2017</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">115000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2018</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">215000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2018</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">280000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2019</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">125000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2019</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">310000</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">
During the current fiscal year (
<code class="leafygreen-ui-1l06pbn">
2019
</code>
in this example), new employees
are added to the
<code class="leafygreen-ui-1l06pbn">
salaries
</code>
collection and new head counts are
pre-allocated for the next year:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"zoo\").salaries.insertMany([\n { \"_id\" : 11, employee: \"Wren\", dept: \"Z\", salary: 100000, fiscal_year: 2019 },\n { \"_id\" : 12, employee: \"Zebra\", dept: \"A\", salary: 150000, fiscal_year: 2019 },\n { \"_id\" : 13, employee: \"headcount1\", dept: \"Z\", salary: 120000, fiscal_year: 2020 },\n { \"_id\" : 14, employee: \"headcount2\", dept: \"Z\", salary: 120000, fiscal_year: 2020 }\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">"zoo"</span>).<span class="lg-highlight-property">salaries</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">11</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"Wren"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">100000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2019</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">12</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"Zebra"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">150000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2019</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">13</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"headcount1"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">120000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2020</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">14</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"headcount2"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">120000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2020</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
<code class="leafygreen-ui-1l06pbn">
budgets
</code>
collection to reflect the new
salary information, the following aggregation pipeline uses:
</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>
stage to find all documents with
<code class="leafygreen-ui-1l06pbn">
fiscal_year
</code>
greater than or equal to
<code class="leafygreen-ui-1l06pbn">
2019
</code>
.
</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>
stage to group the salaries by the
<code class="leafygreen-ui-1l06pbn">
fiscal_year
</code>
and
<code class="leafygreen-ui-1l06pbn">
dept
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
to write the result set to the
<code class="leafygreen-ui-1l06pbn">
budgets
</code>
collection,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-replace" target="_self">
<span>
replacing documents
</span>
</a>
with the same
<code class="leafygreen-ui-1l06pbn">
_id
</code>
value (in
this example, a document with the fiscal year and dept). For
documents that do not have matches in the collection,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
inserts the new documents.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"zoo\").salaries.aggregate( [\n { $match : { fiscal_year: { $gte : 2019 } } },\n { $group: { _id: { fiscal_year: \"$fiscal_year\", dept: \"$dept\" }, salaries: { $sum: \"$salary\" } } },\n { $merge : { into: { db: \"reporting\", coll: \"budgets\" }, on: \"_id\", whenMatched: \"replace\", whenNotMatched: \"insert\" } }\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">"zoo"</span>).<span class="lg-highlight-property">salaries</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->$match<!-- --> <!-- -->: <!-- -->{ <span class="lg-highlight-attr">fiscal_year</span>: <!-- -->{ <!-- -->$gte<!-- --> <!-- -->: <span class="lg-highlight-number">2019</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-attr">fiscal_year</span>: <span class="lg-highlight-string">"$fiscal_year"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"$dept"</span> }<!-- -->, <span class="lg-highlight-attr">salaries</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$salary"</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->$merge<!-- --> <!-- -->: <!-- -->{ <span class="lg-highlight-attr">into</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"reporting"</span>, <span class="lg-highlight-attr">coll</span>: <span class="lg-highlight-string">"budgets"</span> }<!-- -->, <span class="lg-highlight-attr">on</span>: <span class="lg-highlight-string">"_id"</span>, <span class="lg-highlight-attr">whenMatched</span>: <span class="lg-highlight-string">"replace"</span>, <span class="lg-highlight-attr">whenNotMatched</span>: <span class="lg-highlight-string">"insert"</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 aggregation is run, view the documents in the
<code class="leafygreen-ui-1l06pbn">
budgets
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"reporting\").budgets.find().sort( { _id: 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">getSiblingDB</span>(<span class="lg-highlight-string">"reporting"</span>).<span class="lg-highlight-property">budgets</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">_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">
The
<code class="leafygreen-ui-1l06pbn">
budgets
</code>
collection incorporates the new salary data for fiscal
year 2019 and adds new documents for fiscal year 2020:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : { \"fiscal_year\" : 2017, \"dept\" : \"A\" }, \"salaries\" : 220000 }\n{ \"_id\" : { \"fiscal_year\" : 2017, \"dept\" : \"Z\" }, \"salaries\" : 115000 }\n{ \"_id\" : { \"fiscal_year\" : 2018, \"dept\" : \"A\" }, \"salaries\" : 215000 }\n{ \"_id\" : { \"fiscal_year\" : 2018, \"dept\" : \"Z\" }, \"salaries\" : 280000 }\n{ \"_id\" : { \"fiscal_year\" : 2019, \"dept\" : \"A\" }, \"salaries\" : 275000 }\n{ \"_id\" : { \"fiscal_year\" : 2019, \"dept\" : \"Z\" }, \"salaries\" : 410000 }\n{ \"_id\" : { \"fiscal_year\" : 2020, \"dept\" : \"Z\" }, \"salaries\" : 240000 }","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-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2017</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">220000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2017</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">115000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2018</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">215000</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2018</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">280000</span> }</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2019</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">275000</span> }</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2019</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">410000</span> }</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2020</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span> }<!-- -->, <span class="lg-highlight-string">"salaries"</span> <!-- -->: <span class="lg-highlight-number">240000</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/core/materialized-views/">
On-Demand Materialized Views
</a>
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-mat-view-insert-only">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Only Insert New Data
<a class="headerlink leafygreen-ui-1kepx47" href="#only-insert-new-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="only-insert-new-data">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To ensure that the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
does not overwrite existing data
in the collection, set
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched" target="_self">
<span>
whenMatched
</span>
</a>
to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-keepExisting" target="_self">
<span>
keepExisting
</span>
</a>
or
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-fail" target="_self">
<span>
fail
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The example
<code class="leafygreen-ui-1l06pbn">
salaries
</code>
collection in the
<code class="leafygreen-ui-1l06pbn">
zoo
</code>
database contains
the employee salary and department history:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, employee: \"Ant\", dept: \"A\", salary: 100000, fiscal_year: 2017 },\n{ \"_id\" : 2, employee: \"Bee\", dept: \"A\", salary: 120000, fiscal_year: 2017 },\n{ \"_id\" : 3, employee: \"Cat\", dept: \"Z\", salary: 115000, fiscal_year: 2017 },\n{ \"_id\" : 4, employee: \"Ant\", dept: \"A\", salary: 115000, fiscal_year: 2018 },\n{ \"_id\" : 5, employee: \"Bee\", dept: \"Z\", salary: 145000, fiscal_year: 2018 },\n{ \"_id\" : 6, employee: \"Cat\", dept: \"Z\", salary: 135000, fiscal_year: 2018 },\n{ \"_id\" : 7, employee: \"Gecko\", dept: \"A\", salary: 100000, fiscal_year: 2018 },\n{ \"_id\" : 8, employee: \"Ant\", dept: \"A\", salary: 125000, fiscal_year: 2019 },\n{ \"_id\" : 9, employee: \"Bee\", dept: \"Z\", salary: 160000, fiscal_year: 2019 },\n{ \"_id\" : 10, employee: \"Cat\", dept: \"Z\", salary: 150000, fiscal_year: 2019 }","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-attr">employee</span>: <span class="lg-highlight-string">"Ant"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">100000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2017</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-attr">employee</span>: <span class="lg-highlight-string">"Bee"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">120000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2017</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-attr">employee</span>: <span class="lg-highlight-string">"Cat"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">115000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2017</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-attr">employee</span>: <span class="lg-highlight-string">"Ant"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">115000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2018</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-attr">employee</span>: <span class="lg-highlight-string">"Bee"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">145000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2018</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-attr">employee</span>: <span class="lg-highlight-string">"Cat"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">135000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2018</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">7</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"Gecko"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">100000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2018</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">8</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"Ant"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">125000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2019</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">9</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"Bee"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">160000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2019</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-attr">employee</span>: <span class="lg-highlight-string">"Cat"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-attr">salary</span>: <span class="lg-highlight-number">150000</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">2019</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 collection
<code class="leafygreen-ui-1l06pbn">
orgArchive
</code>
in the
<code class="leafygreen-ui-1l06pbn">
reporting
</code>
database
contains historical departmental organization records for the past
fiscal years. Archived records should not be modified.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : ObjectId(\"5cd8c68261baa09e9f3622be\"), \"employees\" : [ \"Ant\", \"Gecko\" ], \"dept\" : \"A\", \"fiscal_year\" : 2018 }\n{ \"_id\" : ObjectId(\"5cd8c68261baa09e9f3622bf\"), \"employees\" : [ \"Ant\", \"Bee\" ], \"dept\" : \"A\", \"fiscal_year\" : 2017 }\n{ \"_id\" : ObjectId(\"5cd8c68261baa09e9f3622c0\"), \"employees\" : [ \"Bee\", \"Cat\" ], \"dept\" : \"Z\", \"fiscal_year\" : 2018 }\n{ \"_id\" : ObjectId(\"5cd8c68261baa09e9f3622c1\"), \"employees\" : [ \"Cat\" ], \"dept\" : \"Z\", \"fiscal_year\" : 2017 }","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">"5cd8c68261baa09e9f3622be"</span>)<!-- -->, <span class="lg-highlight-string">"employees"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Ant"</span>, <span class="lg-highlight-string">"Gecko"</span> ]<!-- -->, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2018</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">"5cd8c68261baa09e9f3622bf"</span>)<!-- -->, <span class="lg-highlight-string">"employees"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Ant"</span>, <span class="lg-highlight-string">"Bee"</span> ]<!-- -->, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2017</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">"5cd8c68261baa09e9f3622c0"</span>)<!-- -->, <span class="lg-highlight-string">"employees"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Bee"</span>, <span class="lg-highlight-string">"Cat"</span> ]<!-- -->, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2018</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">"5cd8c68261baa09e9f3622c1"</span>)<!-- -->, <span class="lg-highlight-string">"employees"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Cat"</span> ]<!-- -->, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2017</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">
orgArchive
</code>
collection has a unique compound index on the
<code class="leafygreen-ui-1l06pbn">
fiscal_year
</code>
and
<code class="leafygreen-ui-1l06pbn">
dept
</code>
fields. Specifically, there should be at
most one record for the same fiscal year and department combination:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"reporting\").orgArchive.createIndex ( { fiscal_year: 1, dept: 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-title lg-highlight-function">getSiblingDB</span>(<span class="lg-highlight-string">"reporting"</span>).<span class="lg-highlight-property">orgArchive</span>.<span class="lg-highlight-property">createIndex</span> <!-- -->( <!-- -->{ <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">dept</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">
At the end of current fiscal year (
<code class="leafygreen-ui-1l06pbn">
2019
</code>
in this example), the
<code class="leafygreen-ui-1l06pbn">
salaries
</code>
collection contain the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"employee\" : \"Ant\", \"dept\" : \"A\", \"salary\" : 100000, \"fiscal_year\" : 2017 }\n{ \"_id\" : 2, \"employee\" : \"Bee\", \"dept\" : \"A\", \"salary\" : 120000, \"fiscal_year\" : 2017 }\n{ \"_id\" : 3, \"employee\" : \"Cat\", \"dept\" : \"Z\", \"salary\" : 115000, \"fiscal_year\" : 2017 }\n{ \"_id\" : 4, \"employee\" : \"Ant\", \"dept\" : \"A\", \"salary\" : 115000, \"fiscal_year\" : 2018 }\n{ \"_id\" : 5, \"employee\" : \"Bee\", \"dept\" : \"Z\", \"salary\" : 145000, \"fiscal_year\" : 2018 }\n{ \"_id\" : 6, \"employee\" : \"Cat\", \"dept\" : \"Z\", \"salary\" : 135000, \"fiscal_year\" : 2018 }\n{ \"_id\" : 7, \"employee\" : \"Gecko\", \"dept\" : \"A\", \"salary\" : 100000, \"fiscal_year\" : 2018 }\n{ \"_id\" : 8, \"employee\" : \"Ant\", \"dept\" : \"A\", \"salary\" : 125000, \"fiscal_year\" : 2019 }\n{ \"_id\" : 9, \"employee\" : \"Bee\", \"dept\" : \"Z\", \"salary\" : 160000, \"fiscal_year\" : 2019 }\n{ \"_id\" : 10, \"employee\" : \"Cat\", \"dept\" : \"Z\", \"salary\" : 150000, \"fiscal_year\" : 2019 }\n{ \"_id\" : 11, \"employee\" : \"Wren\", \"dept\" : \"Z\", \"salary\" : 100000, \"fiscal_year\" : 2019 }\n{ \"_id\" : 12, \"employee\" : \"Zebra\", \"dept\" : \"A\", \"salary\" : 150000, \"fiscal_year\" : 2019 }\n{ \"_id\" : 13, \"employee\" : \"headcount1\", \"dept\" : \"Z\", \"salary\" : 120000, \"fiscal_year\" : 2020 }\n{ \"_id\" : 14, \"employee\" : \"headcount2\", \"dept\" : \"Z\", \"salary\" : 120000, \"fiscal_year\" : 2020 }","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">"employee"</span> <!-- -->: <span class="lg-highlight-string">"Ant"</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"salary"</span> <!-- -->: <span class="lg-highlight-number">100000</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2017</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">"employee"</span> <!-- -->: <span class="lg-highlight-string">"Bee"</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"salary"</span> <!-- -->: <span class="lg-highlight-number">120000</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2017</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">"employee"</span> <!-- -->: <span class="lg-highlight-string">"Cat"</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-string">"salary"</span> <!-- -->: <span class="lg-highlight-number">115000</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2017</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">"employee"</span> <!-- -->: <span class="lg-highlight-string">"Ant"</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"salary"</span> <!-- -->: <span class="lg-highlight-number">115000</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2018</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">"employee"</span> <!-- -->: <span class="lg-highlight-string">"Bee"</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-string">"salary"</span> <!-- -->: <span class="lg-highlight-number">145000</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2018</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">"employee"</span> <!-- -->: <span class="lg-highlight-string">"Cat"</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-string">"salary"</span> <!-- -->: <span class="lg-highlight-number">135000</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2018</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">7</span>, <span class="lg-highlight-string">"employee"</span> <!-- -->: <span class="lg-highlight-string">"Gecko"</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"salary"</span> <!-- -->: <span class="lg-highlight-number">100000</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2018</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">8</span>, <span class="lg-highlight-string">"employee"</span> <!-- -->: <span class="lg-highlight-string">"Ant"</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"salary"</span> <!-- -->: <span class="lg-highlight-number">125000</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2019</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">9</span>, <span class="lg-highlight-string">"employee"</span> <!-- -->: <span class="lg-highlight-string">"Bee"</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-string">"salary"</span> <!-- -->: <span class="lg-highlight-number">160000</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2019</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-string">"employee"</span> <!-- -->: <span class="lg-highlight-string">"Cat"</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-string">"salary"</span> <!-- -->: <span class="lg-highlight-number">150000</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2019</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">11</span>, <span class="lg-highlight-string">"employee"</span> <!-- -->: <span class="lg-highlight-string">"Wren"</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-string">"salary"</span> <!-- -->: <span class="lg-highlight-number">100000</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2019</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">12</span>, <span class="lg-highlight-string">"employee"</span> <!-- -->: <span class="lg-highlight-string">"Zebra"</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"salary"</span> <!-- -->: <span class="lg-highlight-number">150000</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2019</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">13</span>, <span class="lg-highlight-string">"employee"</span> <!-- -->: <span class="lg-highlight-string">"headcount1"</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-string">"salary"</span> <!-- -->: <span class="lg-highlight-number">120000</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2020</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">14</span>, <span class="lg-highlight-string">"employee"</span> <!-- -->: <span class="lg-highlight-string">"headcount2"</span>, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-string">"salary"</span> <!-- -->: <span class="lg-highlight-number">120000</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2020</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 update the
<code class="leafygreen-ui-1l06pbn">
orgArchive
</code>
collection to include the fiscal
year
<code class="leafygreen-ui-1l06pbn">
2019
</code>
that has just ended, the following aggregation pipeline
uses:
</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>
stage to find all documents with
<code class="leafygreen-ui-1l06pbn">
fiscal_year
</code>
equal to
<code class="leafygreen-ui-1l06pbn">
2019
</code>
.
</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>
stage to group the employees by the
<code class="leafygreen-ui-1l06pbn">
fiscal_year
</code>
and
<code class="leafygreen-ui-1l06pbn">
dept
</code>
.
</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>
stage to suppress the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field and add
separate
<code class="leafygreen-ui-1l06pbn">
dept
</code>
and
<code class="leafygreen-ui-1l06pbn">
fiscal_year
</code>
field. When the documents
are passed to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
automatically
generates a new
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field for the documents.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
to write the result set to
<code class="leafygreen-ui-1l06pbn">
orgArchive
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage matches documents
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
on
</span>
</a>
the
<code class="leafygreen-ui-1l06pbn">
dept
</code>
and
<code class="leafygreen-ui-1l06pbn">
fiscal_year
</code>
fields and
<code class="leafygreen-ui-1l06pbn">
fails
</code>
when matched. That is, if a document already exists for the same
department and fiscal year, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
errors.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"zoo\").salaries.aggregate( [\n { $match: { fiscal_year: 2019 }},\n { $group: { _id: { fiscal_year: \"$fiscal_year\", dept: \"$dept\" }, employees: { $push: \"$employee\" } } },\n { $project: { _id: 0, dept: \"$_id.dept\", fiscal_year: \"$_id.fiscal_year\", employees: 1 } },\n { $merge : { into : { db: \"reporting\", coll: \"orgArchive\" }, on: [ \"dept\", \"fiscal_year\" ], whenMatched: \"fail\" } }\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">"zoo"</span>).<span class="lg-highlight-property">salaries</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-attr">fiscal_year</span>: <span class="lg-highlight-number">2019</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-attr">fiscal_year</span>: <span class="lg-highlight-string">"$fiscal_year"</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"$dept"</span> }<!-- -->, <span class="lg-highlight-attr">employees</span>: <!-- -->{ <span class="lg-highlight-attr">$push</span>: <span class="lg-highlight-string">"$employee"</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">dept</span>: <span class="lg-highlight-string">"$_id.dept"</span>, <span class="lg-highlight-attr">fiscal_year</span>: <span class="lg-highlight-string">"$_id.fiscal_year"</span>, <span class="lg-highlight-attr">employees</span>: <span class="lg-highlight-number">1</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->$merge<!-- --> <!-- -->: <!-- -->{ <!-- -->into <!-- -->: <!-- -->{ <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"reporting"</span>, <span class="lg-highlight-attr">coll</span>: <span class="lg-highlight-string">"orgArchive"</span> }<!-- -->, <span class="lg-highlight-attr">on</span>: <!-- -->[ <span class="lg-highlight-string">"dept"</span>, <span class="lg-highlight-string">"fiscal_year"</span> ]<!-- -->, <span class="lg-highlight-attr">whenMatched</span>: <span class="lg-highlight-string">"fail"</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
<code class="leafygreen-ui-1l06pbn">
orgArchive
</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\" : ObjectId(\"5caccc6a66b22dd8a8cc419f\"), \"employees\" : [ \"Ahn\", \"Bess\" ], \"dept\" : \"A\", \"fiscal_year\" : 2017 }\n{ \"_id\" : ObjectId(\"5caccc6a66b22dd8a8cc419e\"), \"employees\" : [ \"Ahn\", \"Gee\" ], \"dept\" : \"A\", \"fiscal_year\" : 2018 }\n{ \"_id\" : ObjectId(\"5caccd0b66b22dd8a8cc438e\"), \"employees\" : [ \"Ahn\", \"Zeb\" ], \"dept\" : \"A\", \"fiscal_year\" : 2019 }\n{ \"_id\" : ObjectId(\"5caccc6a66b22dd8a8cc41a0\"), \"employees\" : [ \"Carl\" ], \"dept\" : \"Z\", \"fiscal_year\" : 2017 }\n{ \"_id\" : ObjectId(\"5caccc6a66b22dd8a8cc41a1\"), \"employees\" : [ \"Bess\", \"Carl\" ], \"dept\" : \"Z\", \"fiscal_year\" : 2018 }\n{ \"_id\" : ObjectId(\"5caccd0b66b22dd8a8cc438d\"), \"employees\" : [ \"Bess\", \"Carl\", \"Wen\" ], \"dept\" : \"Z\", \"fiscal_year\" : 2019 }","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">"5caccc6a66b22dd8a8cc419f"</span>)<!-- -->, <span class="lg-highlight-string">"employees"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Ahn"</span>, <span class="lg-highlight-string">"Bess"</span> ]<!-- -->, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2017</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">"5caccc6a66b22dd8a8cc419e"</span>)<!-- -->, <span class="lg-highlight-string">"employees"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Ahn"</span>, <span class="lg-highlight-string">"Gee"</span> ]<!-- -->, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2018</span> }</td></tr><tr class="leafygreen-ui-1wr1lm6"><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">"5caccd0b66b22dd8a8cc438e"</span>)<!-- -->, <span class="lg-highlight-string">"employees"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Ahn"</span>, <span class="lg-highlight-string">"Zeb"</span> ]<!-- -->, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2019</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">"5caccc6a66b22dd8a8cc41a0"</span>)<!-- -->, <span class="lg-highlight-string">"employees"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Carl"</span> ]<!-- -->, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2017</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">"5caccc6a66b22dd8a8cc41a1"</span>)<!-- -->, <span class="lg-highlight-string">"employees"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Bess"</span>, <span class="lg-highlight-string">"Carl"</span> ]<!-- -->, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2018</span> }</td></tr><tr class="leafygreen-ui-1wr1lm6"><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">"5caccd0b66b22dd8a8cc438d"</span>)<!-- -->, <span class="lg-highlight-string">"employees"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Bess"</span>, <span class="lg-highlight-string">"Carl"</span>, <span class="lg-highlight-string">"Wen"</span> ]<!-- -->, <span class="lg-highlight-string">"dept"</span> <!-- -->: <span class="lg-highlight-string">"Z"</span>, <span class="lg-highlight-string">"fiscal_year"</span> <!-- -->: <span class="lg-highlight-number">2019</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
<code class="leafygreen-ui-1l06pbn">
orgArchive
</code>
collection already contained a document for
2019 for department
<code class="leafygreen-ui-1l06pbn">
"A"
</code>
and/or
<code class="leafygreen-ui-1l06pbn">
"B"
</code>
, the aggregation
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-unique-index-constraints" target="_self">
<span>
fails because of the duplicate key error
</span>
</a>
. However, any document inserted
before the error will not be rolled back.
</p>
<p class="leafygreen-ui-1kp3ins">
If you specify
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-replace" target="_self">
<span>
keepExisting
</span>
</a>
for the matching document, the
aggregation does not affect the matching document and does not error
with duplicate key error. Similarly, if you specify
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-replace" target="_self">
<span>
replace
</span>
</a>
, the
operation would not fail; however, the operation would replace the
existing document.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-mat-view-merge">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Merge Results from Multiple Collections
<a class="headerlink leafygreen-ui-1kepx47" href="#merge-results-from-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="merge-results-from-multiple-collections">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
By default, if a document in the aggregation results matches a
document in the collection, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-merge" target="_self">
<span>
merges
</span>
</a>
the documents.
</p>
<p class="leafygreen-ui-1kp3ins">
An example collection
<code class="leafygreen-ui-1l06pbn">
purchaseorders
</code>
is populated with the
purchase order information by quarter and regions:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.purchaseorders.insertMany( [\n { _id: 1, quarter: \"2019Q1\", region: \"A\", qty: 200, reportDate: new Date(\"2019-04-01\") },\n { _id: 2, quarter: \"2019Q1\", region: \"B\", qty: 300, reportDate: new Date(\"2019-04-01\") },\n { _id: 3, quarter: \"2019Q1\", region: \"C\", qty: 700, reportDate: new Date(\"2019-04-01\") },\n { _id: 4, quarter: \"2019Q2\", region: \"B\", qty: 300, reportDate: new Date(\"2019-07-01\") },\n { _id: 5, quarter: \"2019Q2\", region: \"C\", qty: 1000, reportDate: new Date(\"2019-07-01\") },\n { _id: 6, quarter: \"2019Q2\", region: \"A\", qty: 400, reportDate: new Date(\"2019-07-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">purchaseorders</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">quarter</span>: <span class="lg-highlight-string">"2019Q1"</span>, <span class="lg-highlight-attr">region</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">200</span>, <span class="lg-highlight-attr">reportDate</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-04-01"</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">quarter</span>: <span class="lg-highlight-string">"2019Q1"</span>, <span class="lg-highlight-attr">region</span>: <span class="lg-highlight-string">"B"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">300</span>, <span class="lg-highlight-attr">reportDate</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-04-01"</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">quarter</span>: <span class="lg-highlight-string">"2019Q1"</span>, <span class="lg-highlight-attr">region</span>: <span class="lg-highlight-string">"C"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">700</span>, <span class="lg-highlight-attr">reportDate</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-04-01"</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">quarter</span>: <span class="lg-highlight-string">"2019Q2"</span>, <span class="lg-highlight-attr">region</span>: <span class="lg-highlight-string">"B"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">300</span>, <span class="lg-highlight-attr">reportDate</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-07-01"</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">quarter</span>: <span class="lg-highlight-string">"2019Q2"</span>, <span class="lg-highlight-attr">region</span>: <span class="lg-highlight-string">"C"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">1000</span>, <span class="lg-highlight-attr">reportDate</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-07-01"</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">6</span>, <span class="lg-highlight-attr">quarter</span>: <span class="lg-highlight-string">"2019Q2"</span>, <span class="lg-highlight-attr">region</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">400</span>, <span class="lg-highlight-attr">reportDate</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-07-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">
Another example collection
<code class="leafygreen-ui-1l06pbn">
reportedsales
</code>
is populated with the
reported sales information by quarter and regions:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.reportedsales.insertMany( [\n { _id: 1, quarter: \"2019Q1\", region: \"A\", qty: 400, reportDate: new Date(\"2019-04-02\") },\n { _id: 2, quarter: \"2019Q1\", region: \"B\", qty: 550, reportDate: new Date(\"2019-04-02\") },\n { _id: 3, quarter: \"2019Q1\", region: \"C\", qty: 1000, reportDate: new Date(\"2019-04-05\") },\n { _id: 4, quarter: \"2019Q2\", region: \"B\", qty: 500, reportDate: new Date(\"2019-07-02\") },\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">reportedsales</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">quarter</span>: <span class="lg-highlight-string">"2019Q1"</span>, <span class="lg-highlight-attr">region</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">400</span>, <span class="lg-highlight-attr">reportDate</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-04-02"</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">quarter</span>: <span class="lg-highlight-string">"2019Q1"</span>, <span class="lg-highlight-attr">region</span>: <span class="lg-highlight-string">"B"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">550</span>, <span class="lg-highlight-attr">reportDate</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-04-02"</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">quarter</span>: <span class="lg-highlight-string">"2019Q1"</span>, <span class="lg-highlight-attr">region</span>: <span class="lg-highlight-string">"C"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">1000</span>, <span class="lg-highlight-attr">reportDate</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-04-05"</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">quarter</span>: <span class="lg-highlight-string">"2019Q2"</span>, <span class="lg-highlight-attr">region</span>: <span class="lg-highlight-string">"B"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">500</span>, <span class="lg-highlight-attr">reportDate</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-07-02"</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">
Assume that, for reporting purposes, you want to view the data by
quarter in the following format:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"2019Q1\", \"sales\" : 1950, \"purchased\" : 1200 }\n{ \"_id\" : \"2019Q2\", \"sales\" : 500, \"purchased\" : 1700 }","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">"2019Q1"</span>, <span class="lg-highlight-string">"sales"</span> <!-- -->: <span class="lg-highlight-number">1950</span>, <span class="lg-highlight-string">"purchased"</span> <!-- -->: <span class="lg-highlight-number">1200</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2019Q2"</span>, <span class="lg-highlight-string">"sales"</span> <!-- -->: <span class="lg-highlight-number">500</span>, <span class="lg-highlight-string">"purchased"</span> <!-- -->: <span class="lg-highlight-number">1700</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 use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
to merge in results from the
<code class="leafygreen-ui-1l06pbn">
purchaseorders
</code>
collection and the
<code class="leafygreen-ui-1l06pbn">
reportedsales
</code>
collection
to create a new collection
<code class="leafygreen-ui-1l06pbn">
quarterlyreport
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
To create the
<code class="leafygreen-ui-1l06pbn">
quarterlyreport
</code>
collection, you can use the
following pipeline:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.purchaseorders.aggregate( [\n { $group: { _id: \"$quarter\", purchased: { $sum: \"$qty\" } } }, // group purchase orders by quarter\n { $merge : { into: \"quarterlyreport\", on: \"_id\", whenMatched: \"merge\", whenNotMatched: \"insert\" } }\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">purchaseorders</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">$group</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$quarter"</span>, <span class="lg-highlight-attr">purchased</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$qty"</span> } } }<!-- -->, <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> group purchase orders </span><span class="lg-highlight-comment">by quarter</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->$merge<!-- --> <!-- -->: <!-- -->{ <span class="lg-highlight-attr">into</span>: <span class="lg-highlight-string">"quarterlyreport"</span>, <span class="lg-highlight-attr">on</span>: <span class="lg-highlight-string">"_id"</span>, <span class="lg-highlight-attr">whenMatched</span>: <span class="lg-highlight-string">"merge"</span>, <span class="lg-highlight-attr">whenNotMatched</span>: <span class="lg-highlight-string">"insert"</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>
<dl>
<dt>
First stage:
</dt>
<dd class="css-1ufxuu7">
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
stage groups by the quarter and uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum">
<code class="leafygreen-ui-1l06pbn">
$sum
</code>
</a>
to add the
<code class="leafygreen-ui-1l06pbn">
qty
</code>
fields into a new
<code class="leafygreen-ui-1l06pbn">
purchased
</code>
field. For example:
</p>
<p class="leafygreen-ui-1kp3ins">
To create the
<code class="leafygreen-ui-1l06pbn">
quarterlyreport
</code>
collection, you can use this
pipeline:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"2019Q2\", \"purchased\" : 1700 }\n{ \"_id\" : \"2019Q1\", \"purchased\" : 1200 }","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-string">"2019Q2"</span>, <span class="lg-highlight-string">"purchased"</span> <!-- -->: <span class="lg-highlight-number">1700</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2019Q1"</span>, <span class="lg-highlight-string">"purchased"</span> <!-- -->: <span class="lg-highlight-number">1200</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</dd>
<dt>
Second stage:
</dt>
<dd class="css-1ufxuu7">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage writes the documents to the
<code class="leafygreen-ui-1l06pbn">
quarterlyreport
</code>
collection in the same database. If the stage
finds an existing document in the collection that
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
matches
on
</span>
</a>
the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field, the stage merges the matching
documents. Otherwise, the stage inserts the document. For the
initial creation, no documents should match.
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
To view the documents in the collection, run the following operation:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.quarterlyreport.find().sort( { _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">db.<span class="lg-highlight-property">quarterlyreport</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">_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">
The 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\" : \"2019Q1\", \"sales\" : 1200, \"purchased\" : 1200 }\n{ \"_id\" : \"2019Q2\", \"sales\" : 1700, \"purchased\" : 1700 }","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-string">"2019Q1"</span>, <span class="lg-highlight-string">"sales"</span> <!-- -->: <span class="lg-highlight-number">1200</span>, <span class="lg-highlight-string">"purchased"</span> <!-- -->: <span class="lg-highlight-number">1200</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2019Q2"</span>, <span class="lg-highlight-string">"sales"</span> <!-- -->: <span class="lg-highlight-number">1700</span>, <span class="lg-highlight-string">"purchased"</span> <!-- -->: <span class="lg-highlight-number">1700</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Similarly, run the following aggregation pipeline against the
<code class="leafygreen-ui-1l06pbn">
reportedsales
</code>
collection to merge the sales results into the
<code class="leafygreen-ui-1l06pbn">
quarterlyreport
</code>
collection.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.reportedsales.aggregate( [\n { $group: { _id: \"$quarter\", sales: { $sum: \"$qty\" } } }, // group sales by quarter\n { $merge : { into: \"quarterlyreport\", on: \"_id\", whenMatched: \"merge\", whenNotMatched: \"insert\" } }\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">reportedsales</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">$group</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$quarter"</span>, <span class="lg-highlight-attr">sales</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-string">"$qty"</span> } } }<!-- -->, <span class="lg-highlight-comment">// group sales by quarter</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->$merge<!-- --> <!-- -->: <!-- -->{ <span class="lg-highlight-attr">into</span>: <span class="lg-highlight-string">"quarterlyreport"</span>, <span class="lg-highlight-attr">on</span>: <span class="lg-highlight-string">"_id"</span>, <span class="lg-highlight-attr">whenMatched</span>: <span class="lg-highlight-string">"merge"</span>, <span class="lg-highlight-attr">whenNotMatched</span>: <span class="lg-highlight-string">"insert"</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>
<dl>
<dt>
First stage:
</dt>
<dd class="css-1ufxuu7">
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
stage groups by the quarter and uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sum/#mongodb-group-grp.-sum">
<code class="leafygreen-ui-1l06pbn">
$sum
</code>
</a>
to add the
<code class="leafygreen-ui-1l06pbn">
qty
</code>
fields into a new
<code class="leafygreen-ui-1l06pbn">
sales
</code>
field. For example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"2019Q2\", \"sales\" : 500 }\n{ \"_id\" : \"2019Q1\", \"sales\" : 1950 }","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-string">"2019Q2"</span>, <span class="lg-highlight-string">"sales"</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-string">"2019Q1"</span>, <span class="lg-highlight-string">"sales"</span> <!-- -->: <span class="lg-highlight-number">1950</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</dd>
<dt>
Second stage:
</dt>
<dd class="css-1ufxuu7">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage writes the documents to the
<code class="leafygreen-ui-1l06pbn">
quarterlyreport
</code>
collection in the same database. If the stage
finds an existing document in the collection that
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
matches
on
</span>
</a>
the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field (the quarter), the stage merges
the matching documents. Otherwise, the stage inserts the document.
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
To view the documents in the
<code class="leafygreen-ui-1l06pbn">
quarterlyreport
</code>
collection after
the data has been merged, run the following operation:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.quarterlyreport.find().sort( { _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">db.<span class="lg-highlight-property">quarterlyreport</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">_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">
The 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\" : \"2019Q1\", \"sales\" : 1950, \"purchased\" : 1200 }\n{ \"_id\" : \"2019Q2\", \"sales\" : 500, \"purchased\" : 1700 }","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-string">"2019Q1"</span>, <span class="lg-highlight-string">"sales"</span> <!-- -->: <span class="lg-highlight-number">1950</span>, <span class="lg-highlight-string">"purchased"</span> <!-- -->: <span class="lg-highlight-number">1200</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2019Q2"</span>, <span class="lg-highlight-string">"sales"</span> <!-- -->: <span class="lg-highlight-number">500</span>, <span class="lg-highlight-string">"purchased"</span> <!-- -->: <span class="lg-highlight-number">1700</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Use the Pipeline to Customize the Merge
<a class="headerlink leafygreen-ui-1kepx47" href="#use-the-pipeline-to-customize-the-merge" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-pipeline-to-customize-the-merge">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
can use a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-pipeline" target="_self">
<span>
custom update pipeline
</span>
</a>
when documents match. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-pipeline" target="_self">
<span>
whenMatched pipeline
</span>
</a>
can have
the following 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>
<p class="leafygreen-ui-1kp3ins">
An example collection
<code class="leafygreen-ui-1l06pbn">
votes
</code>
is populated with the daily vote
tally. Create the collection with the following documents:s
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.votes.insertMany( [\n { date: new Date(\"2019-05-01\"), \"thumbsup\" : 1, \"thumbsdown\" : 1 },\n { date: new Date(\"2019-05-02\"), \"thumbsup\" : 3, \"thumbsdown\" : 1 },\n { date: new Date(\"2019-05-03\"), \"thumbsup\" : 1, \"thumbsdown\" : 1 },\n { date: new Date(\"2019-05-04\"), \"thumbsup\" : 2, \"thumbsdown\" : 2 },\n { date: new Date(\"2019-05-05\"), \"thumbsup\" : 6, \"thumbsdown\" : 10 },\n { date: new Date(\"2019-05-06\"), \"thumbsup\" : 13, \"thumbsdown\" : 16 }\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">votes</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">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-05-01"</span>)<!-- -->, <span class="lg-highlight-string">"thumbsup"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"thumbsdown"</span> <!-- -->: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-05-02"</span>)<!-- -->, <span class="lg-highlight-string">"thumbsup"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"thumbsdown"</span> <!-- -->: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-05-03"</span>)<!-- -->, <span class="lg-highlight-string">"thumbsup"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"thumbsdown"</span> <!-- -->: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-05-04"</span>)<!-- -->, <span class="lg-highlight-string">"thumbsup"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"thumbsdown"</span> <!-- -->: <span class="lg-highlight-number">2</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-05-05"</span>)<!-- -->, <span class="lg-highlight-string">"thumbsup"</span> <!-- -->: <span class="lg-highlight-number">6</span>, <span class="lg-highlight-string">"thumbsdown"</span> <!-- -->: <span class="lg-highlight-number">10</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-05-06"</span>)<!-- -->, <span class="lg-highlight-string">"thumbsup"</span> <!-- -->: <span class="lg-highlight-number">13</span>, <span class="lg-highlight-string">"thumbsdown"</span> <!-- -->: <span class="lg-highlight-number">16</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">
Another example collection
<code class="leafygreen-ui-1l06pbn">
monthlytotals
</code>
has the up-to-date
monthly vote totals. Create the 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.monthlytotals.insertOne(\n { \"_id\" : \"2019-05\", \"thumbsup\" : 26, \"thumbsdown\" : 31 }\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">monthlytotals</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-string">"2019-05"</span>, <span class="lg-highlight-string">"thumbsup"</span> <!-- -->: <span class="lg-highlight-number">26</span>, <span class="lg-highlight-string">"thumbsdown"</span> <!-- -->: <span class="lg-highlight-number">31</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">
At the end of each day, that day's votes is inserted into the
<code class="leafygreen-ui-1l06pbn">
votes
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.votes.insertOne(\n { date: new Date(\"2019-05-07\"), \"thumbsup\" : 14, \"thumbsdown\" : 10 }\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">votes</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">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-05-07"</span>)<!-- -->, <span class="lg-highlight-string">"thumbsup"</span> <!-- -->: <span class="lg-highlight-number">14</span>, <span class="lg-highlight-string">"thumbsdown"</span> <!-- -->: <span class="lg-highlight-number">10</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 use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
with an custom pipeline to update the
existing document in the collection
<code class="leafygreen-ui-1l06pbn">
monthlytotals
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.votes.aggregate([\n { $match: { date: { $gte: new Date(\"2019-05-07\"), $lt: new Date(\"2019-05-08\") } } },\n { $project: { _id: { $dateToString: { format: \"%Y-%m\", date: \"$date\" } }, thumbsup: 1, thumbsdown: 1 } },\n { $merge: {\n into: \"monthlytotals\",\n on: \"_id\",\n whenMatched: [\n { $addFields: {\n thumbsup: { $add:[ \"$thumbsup\", \"$$new.thumbsup\" ] },\n thumbsdown: { $add: [ \"$thumbsdown\", \"$$new.thumbsdown\" ] }\n } } ],\n whenNotMatched: \"insert\"\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">votes</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-attr">date</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-05-07"</span>)<!-- -->, <span class="lg-highlight-attr">$lt</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2019-05-08"</span>) } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <!-- -->{ <span class="lg-highlight-attr">$dateToString</span>: <!-- -->{ <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"%Y-%m"</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-string">"$date"</span> } }<!-- -->, <span class="lg-highlight-attr">thumbsup</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">thumbsdown</span>: <span class="lg-highlight-number">1</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$merge</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">into</span>: <span class="lg-highlight-string">"monthlytotals"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">on</span>: <span class="lg-highlight-string">"_id"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">whenMatched</span>: <!-- -->[</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"> <span class="lg-highlight-attr">thumbsup</span>: <!-- -->{ <span class="lg-highlight-attr">$add</span>:<!-- -->[ <span class="lg-highlight-string">"$thumbsup"</span>, <span class="lg-highlight-string">"$$new.thumbsup"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">thumbsdown</span>: <!-- -->{ <span class="lg-highlight-attr">$add</span>: <!-- -->[ <span class="lg-highlight-string">"$thumbsdown"</span>, <span class="lg-highlight-string">"$$new.thumbsdown"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> } } ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">whenNotMatched</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></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
</div>
</button>
</div>
</div>
</div>
</div>
<dl>
<dt>
First stage:
</dt>
<dd class="css-1ufxuu7">
<p class="leafygreen-ui-1kp3ins">
The
<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 finds the specific day's votes. For
example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : ObjectId(\"5ce6097c436eb7e1203064a6\"), \"date\" : ISODate(\"2019-05-07T00:00:00Z\"), \"thumbsup\" : 14, \"thumbsdown\" : 10 }","programmingLanguage":"JavaScript"}
</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 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">"5ce6097c436eb7e1203064a6"</span>)<!-- -->, <span class="lg-highlight-string">"date"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2019-05-07T00:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"thumbsup"</span> <!-- -->: <span class="lg-highlight-number">14</span>, <span class="lg-highlight-string">"thumbsdown"</span> <!-- -->: <span class="lg-highlight-number">10</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</dd>
<dt>
Second stage:
</dt>
<dd class="css-1ufxuu7">
<p class="leafygreen-ui-1kp3ins">
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 sets the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field to a
year-month string. For example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"thumbsup\" : 14, \"thumbsdown\" : 10, \"_id\" : \"2019-05\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"thumbsup"</span> <!-- -->: <span class="lg-highlight-number">14</span>, <span class="lg-highlight-string">"thumbsdown"</span> <!-- -->: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"2019-05"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</dd>
<dt>
Third stage:
</dt>
<dd class="css-1ufxuu7">
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage writes the documents to the
<code class="leafygreen-ui-1l06pbn">
monthlytotals
</code>
collection in the same database. If the stage
finds an existing document in the collection that
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-on" target="_self">
<span>
matches
on
</span>
</a>
the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field, the stage uses a pipeline to
add the
<code class="leafygreen-ui-1l06pbn">
thumbsup
</code>
votes and the
<code class="leafygreen-ui-1l06pbn">
thumbsdown
</code>
votes.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
This pipeline cannot directly accesses the fields from the
results document. To access the
<code class="leafygreen-ui-1l06pbn">
thumbsup
</code>
field and the
<code class="leafygreen-ui-1l06pbn">
thumbsdown
</code>
field in the results document, the pipeline uses
the
<code class="leafygreen-ui-1l06pbn">
$$new
</code>
variable; i.e.
<code class="leafygreen-ui-1l06pbn">
$$new.thumbsup
</code>
and
<code class="leafygreen-ui-1l06pbn">
$new.thumbsdown
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
This pipeline can directly accesses the
<code class="leafygreen-ui-1l06pbn">
thumbsup
</code>
field
and the
<code class="leafygreen-ui-1l06pbn">
thumbsdown
</code>
field in the existing document in the
collection; i.e.
<code class="leafygreen-ui-1l06pbn">
$thumbsup
</code>
and
<code class="leafygreen-ui-1l06pbn">
$thumbsdown
</code>
.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The resulting document replaces the existing document.
</p>
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
To view documents in the
<code class="leafygreen-ui-1l06pbn">
monthlytotals
</code>
collection after the merge
operation, run the following operation:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.monthlytotals.find()","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">monthlytotals</span>.<span class="lg-highlight-title lg-highlight-function">find</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 collection contains the following document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"2019-05\", \"thumbsup\" : 40, \"thumbsdown\" : 41 }","programmingLanguage":"JavaScript"}
</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 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">"2019-05"</span>, <span class="lg-highlight-string">"thumbsup"</span> <!-- -->: <span class="lg-highlight-number">40</span>, <span class="lg-highlight-string">"thumbsdown"</span> <!-- -->: <span class="lg-highlight-number">41</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-variables-example">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Use Variables to Customize the Merge
<a class="headerlink leafygreen-ui-1kepx47" href="#use-variables-to-customize-the-merge" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-variables-to-customize-the-merge">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can use variables in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-pipeline" target="_self">
<span>
whenMatched
</span>
</a>
field. Variables must
be defined before they can be used.
</p>
<p class="leafygreen-ui-1kp3ins">
Define variables in one or both of the following:
</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-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-let" target="_self">
<span>
let
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#mongodb-dbcommand-dbcmd.aggregate">
<code class="leafygreen-ui-1l06pbn">
aggregate
</code>
</a>
command
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#std-label-aggregate-let-option">
let
</a>
(starting in MongoDB 5.0)
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
To use variables in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-pipeline" target="_self">
<span>
whenMatched
<!-- -->
:
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Specify the double dollar sign ($$) prefix together with the variable
name in the form
<code class="leafygreen-ui-1l06pbn">
$$<variable_name>
</code>
. For example,
<code class="leafygreen-ui-1l06pbn">
$$year
</code>
. If the
variable is set to a document, you can also include a document field in
the form
<code class="leafygreen-ui-1l06pbn">
$$<variable_name>.<field>
</code>
. For example,
<code class="leafygreen-ui-1l06pbn">
$$year.month
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
The tabs below demonstrate behavior when variables are defined in the
merge stage, the aggregate command, or both.
</p>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-30">
<div aria-label="Tabs to describe usage of merge-variables-in-aggregate-command/merge-variables-in-merge-stage/merge-variables-scope" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_ckdlpr2m6" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_2yyzcnexe" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Merge Stage
</div>
</button>
<button aria-controls="_12ac8f6tc" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_g3u1bdi29" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Aggregate Command
</div>
</button>
<button aria-controls="_h74y1etww" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_u30ktbq52" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Merge and Aggregate
</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="_mm6j7rxkj" class="" id="_bbkpcvpl4" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Use Variables Defined in the Merge Stage
<a class="headerlink leafygreen-ui-1kepx47" href="#use-variables-defined-in-the-merge-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-variables-defined-in-the-merge-stage">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
You can define variables in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-let" target="_self">
<span>
let
</span>
</a>
and use the variables in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-pipeline" target="_self">
<span>
whenMatched
</span>
</a>
field.
</p>
<p class="leafygreen-ui-1kp3ins">
Example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeSales.insertOne( [\n { _id: 1, flavor: \"chocolate\", salesTotal: 1580,\n salesTrend: \"up\" }\n] )\n\ndb.runCommand( {\n aggregate: db.cakeSales.getName(),\n pipeline: [ {\n $merge: {\n into: db.cakeSales.getName(),\n let : { year: \"2020\" },\n whenMatched: [ {\n $addFields: { \"salesYear\": \"$$year\" }\n } ]\n }\n } ],\n cursor: {}\n} )\n\ndb.cakeSales.find()","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">cakeSales</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">1</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-attr">salesTotal</span>: <span class="lg-highlight-number">1580</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">salesTrend</span>: <span class="lg-highlight-string">"up"</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">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">aggregate</span>: <!-- -->db.<span class="lg-highlight-property">cakeSales</span>.<span class="lg-highlight-title lg-highlight-function">getName</span>(<!-- -->)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pipeline</span>: <!-- -->[ <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$merge</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">into</span>: <!-- -->db.<span class="lg-highlight-property">cakeSales</span>.<span class="lg-highlight-title lg-highlight-function">getName</span>(<!-- -->)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">let</span> <!-- -->: <!-- -->{ <span class="lg-highlight-attr">year</span>: <span class="lg-highlight-string">"2020"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">whenMatched</span>: <!-- -->[ <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$addFields</span>: <!-- -->{ <span class="lg-highlight-string">"salesYear"</span>: <span class="lg-highlight-string">"$$year"</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">cursor</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">db.<span class="lg-highlight-property">cakeSales</span>.<span class="lg-highlight-title lg-highlight-function">find</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 example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
creates a collection named
<code class="leafygreen-ui-1l06pbn">
cakeSales
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
runs an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#mongodb-dbcommand-dbcmd.aggregate">
<code class="leafygreen-ui-1l06pbn">
aggregate
</code>
</a>
command that defines a
<code class="leafygreen-ui-1l06pbn">
year
</code>
variable in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-let" target="_self">
<span>
let
</span>
</a>
and adds the year to
<code class="leafygreen-ui-1l06pbn">
cakeSales
</code>
using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-pipeline" target="_self">
<span>
whenMatched
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
retrieves the
<code class="leafygreen-ui-1l06pbn">
cakeSales
</code>
document
</p>
</li>
</ul>
<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":"{ \"_id\" : 1, \"flavor\" : \"chocolate\", \"salesTotal\" : 1580,\n \"salesTrend\" : \"up\", \"salesYear\" : \"2020\" }","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">"flavor"</span> <!-- -->: <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"salesTotal"</span> <!-- -->: <span class="lg-highlight-number">1580</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"salesTrend"</span> <!-- -->: <span class="lg-highlight-string">"up"</span>, <span class="lg-highlight-string">"salesYear"</span> <!-- -->: <span class="lg-highlight-string">"2020"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
<div>
<div aria-labelledby="_4d5vsbv1h" class="leafygreen-ui-38lglc" id="_ul9p89cd2" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Use Variables Defined in the Aggregate Command
<a class="headerlink leafygreen-ui-1kepx47" href="#use-variables-defined-in-the-aggregate-command" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-variables-defined-in-the-aggregate-command">
</div>
</a>
</h4>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
You can define variables in 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
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#std-label-aggregate-let-option">
let
</a>
and use the
variables in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-pipeline" target="_self">
<span>
whenMatched
</span>
</a>
field.
</p>
<p class="leafygreen-ui-1kp3ins">
Example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeSales.insertOne(\n { _id: 1, flavor: \"chocolate\", salesTotal: 1580,\n salesTrend: \"up\" }\n)\n\ndb.runCommand( {\n aggregate: db.cakeSales.getName(),\n pipeline: [ {\n $merge: {\n into: db.cakeSales.getName(),\n whenMatched: [ {\n $addFields: { \"salesYear\": \"$$year\" } }\n ] }\n }\n ],\n cursor: {},\n let : { year: \"2020\" }\n} )\n\ndb.cakeSales.find()","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">cakeSales</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">1</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-attr">salesTotal</span>: <span class="lg-highlight-number">1580</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">salesTrend</span>: <span class="lg-highlight-string">"up"</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">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">aggregate</span>: <!-- -->db.<span class="lg-highlight-property">cakeSales</span>.<span class="lg-highlight-title lg-highlight-function">getName</span>(<!-- -->)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pipeline</span>: <!-- -->[ <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$merge</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">into</span>: <!-- -->db.<span class="lg-highlight-property">cakeSales</span>.<span class="lg-highlight-title lg-highlight-function">getName</span>(<!-- -->)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">whenMatched</span>: <!-- -->[ <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$addFields</span>: <!-- -->{ <span class="lg-highlight-string">"salesYear"</span>: <span class="lg-highlight-string">"$$year"</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">cursor</span>: <!-- -->{<!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">let</span> <!-- -->: <!-- -->{ <span class="lg-highlight-attr">year</span>: <span class="lg-highlight-string">"2020"</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">db.<span class="lg-highlight-property">cakeSales</span>.<span class="lg-highlight-title lg-highlight-function">find</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 example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
creates a collection named
<code class="leafygreen-ui-1l06pbn">
cakeSales
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
runs an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#mongodb-dbcommand-dbcmd.aggregate">
<code class="leafygreen-ui-1l06pbn">
aggregate
</code>
</a>
command that defines a
<code class="leafygreen-ui-1l06pbn">
year
</code>
variable in 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
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#std-label-aggregate-let-option">
let
</a>
and adds the year to
<code class="leafygreen-ui-1l06pbn">
cakeSales
</code>
using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-whenMatched-pipeline" target="_self">
<span>
whenMatched
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
retrieves the
<code class="leafygreen-ui-1l06pbn">
cakeSales
</code>
document
</p>
</li>
</ul>
<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":"{ \"_id\" : 1, \"flavor\" : \"chocolate\", \"salesTotal\" : 1580,\n \"salesTrend\" : \"up\", \"salesYear\" : \"2020\" }","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">"flavor"</span> <!-- -->: <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"salesTotal"</span> <!-- -->: <span class="lg-highlight-number">1580</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"salesTrend"</span> <!-- -->: <span class="lg-highlight-string">"up"</span>, <span class="lg-highlight-string">"salesYear"</span> <!-- -->: <span class="lg-highlight-string">"2020"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
<div>
<div aria-labelledby="_1beimsr0c" class="leafygreen-ui-38lglc" id="_zium0upsi" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Use Variables Defined in the Merge Stage and Aggregate Command
<a class="headerlink leafygreen-ui-1kepx47" href="#use-variables-defined-in-the-merge-stage-and-aggregate-command" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-variables-defined-in-the-merge-stage-and-aggregate-command">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
You can define variables in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage
and, starting in MongoDB 5.0, 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>
<p class="leafygreen-ui-1kp3ins">
If two variables with the same name are defined in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage and 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="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage variable is used.
</p>
<p class="leafygreen-ui-1kp3ins">
In this example, the
<code class="leafygreen-ui-1l06pbn">
year: "2020"
</code>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-merge" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</span>
</a>
stage variable is used instead of the
<code class="leafygreen-ui-1l06pbn">
year: "2019"
</code>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#mongodb-dbcommand-dbcmd.aggregate">
<code class="leafygreen-ui-1l06pbn">
aggregate
</code>
</a>
command variable:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeSales.insertOne(\n { _id: 1, flavor: \"chocolate\", salesTotal: 1580,\n salesTrend: \"up\" }\n)\n\ndb.runCommand( {\n aggregate: db.cakeSales.getName(),\n pipeline: [ {\n $merge: {\n into: db.cakeSales.getName(),\n let : { year: \"2020\" },\n whenMatched: [ {\n $addFields: { \"salesYear\": \"$$year\" }\n } ]\n }\n } ],\n cursor: {},\n let : { year: \"2019\" }\n} )\n\ndb.cakeSales.find()","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">cakeSales</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">1</span>, <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-attr">salesTotal</span>: <span class="lg-highlight-number">1580</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">salesTrend</span>: <span class="lg-highlight-string">"up"</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">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">aggregate</span>: <!-- -->db.<span class="lg-highlight-property">cakeSales</span>.<span class="lg-highlight-title lg-highlight-function">getName</span>(<!-- -->)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pipeline</span>: <!-- -->[ <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$merge</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">into</span>: <!-- -->db.<span class="lg-highlight-property">cakeSales</span>.<span class="lg-highlight-title lg-highlight-function">getName</span>(<!-- -->)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">let</span> <!-- -->: <!-- -->{ <span class="lg-highlight-attr">year</span>: <span class="lg-highlight-string">"2020"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">whenMatched</span>: <!-- -->[ <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$addFields</span>: <!-- -->{ <span class="lg-highlight-string">"salesYear"</span>: <span class="lg-highlight-string">"$$year"</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">cursor</span>: <!-- -->{<!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">let</span> <!-- -->: <!-- -->{ <span class="lg-highlight-attr">year</span>: <span class="lg-highlight-string">"2019"</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">db.<span class="lg-highlight-property">cakeSales</span>.<span class="lg-highlight-title lg-highlight-function">find</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">
Output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: 1,\n flavor: 'chocolate',\n salesTotal: 1580,\n salesTrend: 'up',\n salesYear: '2020'\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">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">flavor</span>: <span class="lg-highlight-string">'chocolate'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">salesTotal</span>: <span class="lg-highlight-number">1580</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">salesTrend</span>: <span class="lg-highlight-string">'up'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">salesYear</span>: <span class="lg-highlight-string">'2020'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</div>
</div>
</div>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/applications/indexes/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Indexing Strategies
<a class="headerlink leafygreen-ui-1kepx47" href="#indexing-strategies" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="indexing-strategies">
</div>
</a>
</h1>
<p class="leafygreen-ui-1kp3ins">
The best indexes for your application must take a number
of factors into account, including the kinds of queries you expect,
the ratio of reads to writes, and the amount of free memory on your
system.
</p>
<p class="leafygreen-ui-1kp3ins">
When developing your indexing strategy you should have a deep
understanding of your application's queries. Before you build indexes,
map out the types of queries you will run so that you can build
indexes that reference those fields. Indexes come with a performance
cost, but are more than worth the cost for frequent queries on large
data sets. Consider the relative frequency of each query in the
application and whether the query justifies an index.
</p>
<p class="leafygreen-ui-1kp3ins">
The best overall strategy for designing indexes is to profile a
variety of index configurations with data sets similar to the ones
you'll be running in production to see which configurations perform
best. Inspect the current indexes created for your collections to
ensure they are supporting your current and planned queries. If an
index is no longer used, drop the index.
</p>
<p class="leafygreen-ui-1kp3ins">
Generally, MongoDB only uses
<em>
one
</em>
index to fulfill most queries.
However, each clause of an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/or/#mongodb-query-op.-or">
<code class="leafygreen-ui-1l06pbn">
$or
</code>
</a>
query may use a different
index.
</p>
<p class="leafygreen-ui-1kp3ins">
The following documents introduce indexing strategies:
</p>
<dl>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/equality-sort-range-rule/#std-label-esr-indexing-rule">
Use the ESR (Equality, Sort, Range) Rule
</a>
</dt>
<dd class="css-1ufxuu7">
The ESR (Equality, Sort, Range) Rule is a guide to creating indexes
that support your queries efficiently.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/data-modeling/schema-design-process/create-indexes/#std-label-create-indexes-to-support-queries">
Create Indexes to Support Your Queries
</a>
</dt>
<dd class="css-1ufxuu7">
An index supports a query when the index contains all the fields
scanned by the query. Creating indexes that support queries
results in greatly increased query performance.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/sort-results-with-indexes/#std-label-sorting-with-indexes">
Use Indexes to Sort Query Results
</a>
</dt>
<dd class="css-1ufxuu7">
To support efficient queries, use the strategies here when you
specify the sequential order and sort order of index fields.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/create-queries-that-ensure-selectivity/#std-label-index-selectivity">
Create Indexes to Ensure Query Selectivity
</a>
</dt>
<dd class="css-1ufxuu7">
Selectivity is the ability of a query to narrow results using the
index. Selectivity allows MongoDB to use the index for a larger
portion of the work associated with fulfilling the query.
</dd>
</dl>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/not/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$not (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-not--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="-not--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">
$not
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-not" 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.-not">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Evaluates a boolean and returns the opposite boolean value; i.e.
when passed an expression that evaluates to
<code class="leafygreen-ui-1l06pbn">
true
</code>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-not" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$not
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
false
</code>
; when passed an expression that
evaluates to
<code class="leafygreen-ui-1l06pbn">
false
</code>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-not" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$not
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-not" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$not
</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":"{ $not: [ <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">$not</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">
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">
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.-not" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$not
</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.-not" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$not
</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: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>
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">
{ $not: [ 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">
{ $not: [ [ false ] ] }
</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">
{ $not: [ false ] }
</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">
{ $not: [ null ] }
</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">
{ $not: [ 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>
</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">
Consider 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":"{ \"_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 }","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">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></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-not" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$not
</code>
</span>
</a>
operator to
determine if
<code class="leafygreen-ui-1l06pbn">
qty
</code>
is not greater 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 result: { $not: [ { $gt: [ \"$qty\", 250 ] } ] }\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">result</span>: <!-- -->{ <span class="lg-highlight-attr">$not</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$gt</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 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\" : \"abc1\", \"result\" : false }\n{ \"_id\" : 2, \"item\" : \"abc2\", \"result\" : true }\n{ \"_id\" : 3, \"item\" : \"xyz1\", \"result\" : true }\n{ \"_id\" : 4, \"item\" : \"VWZ1\", \"result\" : false }\n{ \"_id\" : 5, \"item\" : \"VWZ2\", \"result\" : true }","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">"item"</span> <!-- -->: <span class="lg-highlight-string">"abc1"</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">"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">"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">4</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"VWZ1"</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">"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/operator/aggregation/dateSubtract/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$dateSubtract (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-datesubtract--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="-datesubtract--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">
$dateSubtract
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-dateSubtract" 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.-dateSubtract">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Decrements a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Date/#mongodb-method-Date">
<code class="leafygreen-ui-1l06pbn">
Date()
</code>
</a>
object by a
specified number of time units.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-dateSubtract" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$dateSubtract
</code>
</span>
</a>
expression has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $dateSubtract: {\n startDate: <expression>,\n unit: <expression>,\n amount: <expression>,\n timezone: <tzexpression>\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">$dateSubtract</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">startDate</span>: <<span class="lg-highlight-title lg-highlight-class">Expression</span>><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">unit</span>: <<span class="lg-highlight-title lg-highlight-class">Expression</span>><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">amount</span>: <<span class="lg-highlight-title lg-highlight-class">Expression</span>><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timezone</span>: <<!-- -->tzExpression<!-- -->></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">
Returns a
<code class="leafygreen-ui-1l06pbn">
Date()
</code>
. The
<code class="leafygreen-ui-1l06pbn">
startDate
</code>
can be any expression that
resolves to type Date, Timestamp or ObjectId. No matter which data
type is used as input, the value returned will be a
<code class="leafygreen-ui-1l06pbn">
Date()
</code>
object.
</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>
Required/Optional
</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">
startDate
</code>
</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>
The beginning date, in UTC, for the subtraction operation.
The
<code class="leafygreen-ui-1l06pbn">
startDate
</code>
can be any
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to
a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-date">
Date
</a>
, a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-timestamp">
Timestamp
</a>
,
or an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-object-id">
ObjectID
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
unit
</code>
</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>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
unit
</code>
used to measure the
<code class="leafygreen-ui-1l06pbn">
amount
</code>
of time
subtracted from the
<code class="leafygreen-ui-1l06pbn">
startDate
</code>
. The
<code class="leafygreen-ui-1l06pbn">
unit
</code>
is an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to
one of the following strings:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
year
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
quarter
</code>
</p>
</li>
<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">
month
</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>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
amount
</code>
</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>
The number of
<code class="leafygreen-ui-1l06pbn">
units
</code>
subtracted from the
<code class="leafygreen-ui-1l06pbn">
startDate
</code>
.
The
<code class="leafygreen-ui-1l06pbn">
amount
</code>
is an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to
an integer or long. The
<code class="leafygreen-ui-1l06pbn">
amount
</code>
can also resolve to an
integral decimal and or a double if that value can be
converted to a long without loss of precision.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
timezone
</code>
</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">
The timezone to carry out the operation.
<code class="leafygreen-ui-1l06pbn">
<tzExpression>
</code>
must be a
valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to a
string formatted as either an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones" rel="noopener noreferrer">
<span>
Olson Timezone Identifier
</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>
or a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/wiki/List_of_UTC_time_offsets" rel="noopener noreferrer">
<span>
UTC Offset
</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>
.
If no
<code class="leafygreen-ui-1l06pbn">
timezone
</code>
is provided, the result is displayed in
<code class="leafygreen-ui-1l06pbn">
UTC
</code>
.
</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>
Format
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Examples
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Olson Timezone Identifier
</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":"\"America/New_York\"\n\"Europe/London\"\n\"GMT\"","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"><span class="lg-highlight-string">"America/New_York"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">"Europe/London"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">"GMT"</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>
UTC Offset
</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":"+/-[hh]:[mm], e.g. \"+04:45\"\n+/-[hh][mm], e.g. \"-0530\"\n+/-[hh], e.g. \"+03\"","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">+<!-- -->/-<!-- -->[hh]:[mm], e.g. <span class="lg-highlight-string">"+04:45"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">+<!-- -->/-<!-- -->[hh][mm], e.g. <span class="lg-highlight-string">"-0530"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">+<!-- -->/-<!-- -->[hh], e.g. <span class="lg-highlight-string">"+03"</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>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on expressions and types see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
Expression Operators
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-bson-types">
BSON Types
<!-- -->
.
</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">
Time Measurement
<a class="headerlink leafygreen-ui-1kepx47" href="#time-measurement" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="time-measurement">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB follows prevaling database usage and works with time in UTC. The
<code class="leafygreen-ui-1l06pbn">
dateSubtract
</code>
expression always takes a
<code class="leafygreen-ui-1l06pbn">
startDate
</code>
in UTC and
returns a result in UTC. If the
<code class="leafygreen-ui-1l06pbn">
timezone
</code>
is specified, the
calculation will be done using the specified
<code class="leafygreen-ui-1l06pbn">
timezone
</code>
. The timezone
is especially important when a calculation involves Daylight Savings
Time (DST).
</p>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
unit
</code>
is a
<code class="leafygreen-ui-1l06pbn">
month
</code>
, or larger the operation adjusts to
account for the last day of the month. Subtracting one
<code class="leafygreen-ui-1l06pbn">
month
</code>
on the
last day of March, for example, demonstrates the
"last-day-of-the-month" adjustment.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $dateSubtract:\n {\n startDate: ISODate(\"2021-03-31T12:10:05Z\"),\n unit: \"month\",\n amount: 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">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$dateSubtract</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">startDate</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-31T12:10:05Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">unit</span>: <span class="lg-highlight-string">"month"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">amount</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>
<p class="leafygreen-ui-1kp3ins">
Notice that the date returned,
<code class="leafygreen-ui-1l06pbn">
ISODate("2021-02-28T12:10:05Z")
</code>
, is
the 28th and not the 31st since February has fewer days than March.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Time Zone
<a class="headerlink leafygreen-ui-1kepx47" href="#time-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="time-zone">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When using an Olson Timezone Identifier in the
<code class="leafygreen-ui-1l06pbn">
<timezone>
</code>
field, MongoDB applies the
<span class="leafygreen-ui-vm4wms" tabindex="0">
DST
</span>
offset
if applicable for the specified timezone.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, consider a
<code class="leafygreen-ui-1l06pbn">
sales
</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":"{\n \"_id\" : 1,\n \"item\" : \"abc\",\n \"price\" : 20,\n \"quantity\" : 5,\n \"date\" : ISODate(\"2017-05-20T10:24:51.303Z\")\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">"item"</span> <!-- -->: <span class="lg-highlight-string">"abc"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">20</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">5</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"date"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2017-05-20T10:24:51.303Z"</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 illustrates how MongoDB handles the DST
offset for the Olson Timezone Identifier. The example uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/hour/#mongodb-expression-exp.-hour">
<code class="leafygreen-ui-1l06pbn">
$hour
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/minute/#mongodb-expression-exp.-minute">
<code class="leafygreen-ui-1l06pbn">
$minute
</code>
</a>
operators to return the
corresponding portions of the
<code class="leafygreen-ui-1l06pbn">
date
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sales.aggregate([\n{\n $project: {\n \"nycHour\": {\n $hour: { date: \"$date\", timezone: \"-05:00\" }\n },\n \"nycMinute\": {\n $minute: { date: \"$date\", timezone: \"-05:00\" }\n },\n \"gmtHour\": {\n $hour: { date: \"$date\", timezone: \"GMT\" }\n },\n \"gmtMinute\": {\n $minute: { date: \"$date\", timezone: \"GMT\" } },\n \"nycOlsonHour\": {\n $hour: { date: \"$date\", timezone: \"America/New_York\" }\n },\n \"nycOlsonMinute\": {\n $minute: { date: \"$date\", timezone: \"America/New_York\" }\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">sales</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"> <span class="lg-highlight-string">"nycHour"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$hour</span>: <!-- -->{ <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-string">"$date"</span>, <span class="lg-highlight-attr">timezone</span>: <span class="lg-highlight-string">"-05:00"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nycMinute"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$minute</span>: <!-- -->{ <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-string">"$date"</span>, <span class="lg-highlight-attr">timezone</span>: <span class="lg-highlight-string">"-05:00"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"gmtHour"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$hour</span>: <!-- -->{ <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-string">"$date"</span>, <span class="lg-highlight-attr">timezone</span>: <span class="lg-highlight-string">"GMT"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"gmtMinute"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$minute</span>: <!-- -->{ <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-string">"$date"</span>, <span class="lg-highlight-attr">timezone</span>: <span class="lg-highlight-string">"GMT"</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nycOlsonHour"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$hour</span>: <!-- -->{ <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-string">"$date"</span>, <span class="lg-highlight-attr">timezone</span>: <span class="lg-highlight-string">"America/New_York"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nycOlsonMinute"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$minute</span>: <!-- -->{ <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-string">"$date"</span>, <span class="lg-highlight-attr">timezone</span>: <span class="lg-highlight-string">"America/New_York"</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 result:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\": 1,\n \"nycHour\" : 5,\n \"nycMinute\" : 24,\n \"gmtHour\" : 10,\n \"gmtMinute\" : 24,\n \"nycOlsonHour\" : 6,\n \"nycOlsonMinute\" : 24\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">"nycHour"</span> <!-- -->: <span class="lg-highlight-number">5</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nycMinute"</span> <!-- -->: <span class="lg-highlight-number">24</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"gmtHour"</span> <!-- -->: <span class="lg-highlight-number">10</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"gmtMinute"</span> <!-- -->: <span class="lg-highlight-number">24</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nycOlsonHour"</span> <!-- -->: <span class="lg-highlight-number">6</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nycOlsonMinute"</span> <!-- -->: <span class="lg-highlight-number">24</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>
<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">
Subtract A Fixed Amount
<a class="headerlink leafygreen-ui-1kepx47" href="#subtract-a-fixed-amount" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="subtract-a-fixed-amount">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Consider a collection of system connection times like these:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.connectionTime.insertMany(\n [\n {\n custId: 457,\n login: ISODate(\"2020-12-25T19:04:00\"),\n logout: ISODate(\"2020-12-28T09:04:00\")\n },\n {\n custId: 457,\n login: ISODate(\"2021-01-27T05:12:00\"),\n logout: ISODate(\"2021-01-28T13:05:00\")\n },\n {\n custId: 458,\n login: ISODate(\"2021-01-22T06:27:00\"),\n logout: ISODate(\"2021-01-31T11:00:00\")\n },\n {\n custId: 459,\n login: ISODate(\"2021-02-14T20:14:00\"),\n logout: ISODate(\"2021-02-17T16:05:00\")\n },\n {\n custId: 460,\n login: ISODate(\"2021-02-26T02:44:00\"),\n logout: ISODate(\"2021-02-18T14:13:00\")\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">connectionTime</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"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">custId</span>: <span class="lg-highlight-number">457</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">login</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-12-25T19:04:00"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">logout</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-12-28T09:04:00"</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">custId</span>: <span class="lg-highlight-number">457</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">login</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-01-27T05:12:00"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">logout</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-01-28T13:05:00"</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">custId</span>: <span class="lg-highlight-number">458</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">login</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-01-22T06:27:00"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">logout</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-01-31T11:00:00"</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">custId</span>: <span class="lg-highlight-number">459</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">login</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-02-14T20:14:00"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">logout</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-02-17T16:05:00"</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">custId</span>: <span class="lg-highlight-number">460</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">login</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-02-26T02:44:00"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">logout</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-02-18T14:13:00"</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">
Due to a service issue you need to subtract 3 hours from each of the
January 2021 logout times. You can use
<code class="leafygreen-ui-1l06pbn">
$dateSubtract
</code>
in an
aggregation pipeline to decrement the
<code class="leafygreen-ui-1l06pbn">
logoutTime
</code>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.connectionTime.aggregate(\n [\n {\n $match:\n {\n $expr:\n {\n $eq:\n [\n { $year: \"$logout\" },\n 2021\n ]\n },\n $expr:\n {\n $eq:\n [\n { $month: \"$logout\" },\n 1\n ]\n }\n }\n },\n {\n $project:\n {\n logoutTime:\n {\n $dateSubtract:\n {\n startDate: \"$logout\",\n unit: \"hour\",\n amount: 3\n }\n }\n }\n },\n {\n $merge: \"connectionTime\"\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">connectionTime</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">$match</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$expr</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$eq</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$year</span>: <span class="lg-highlight-string">"$logout"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-number">2021</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">$expr</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$eq</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$month</span>: <span class="lg-highlight-string">"$logout"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <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><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">logoutTime</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">$dateSubtract</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"> <span class="lg-highlight-attr">startDate</span>: <span class="lg-highlight-string">"$logout"</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">unit</span>: <span class="lg-highlight-string">"hour"</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">amount</span>: <span class="lg-highlight-number">3</span></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><tr 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">$merge</span>: <span class="lg-highlight-string">"connectionTime"</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">
Two similar comparisons are made in the
<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. First
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/year/#mongodb-expression-exp.-year">
<code class="leafygreen-ui-1l06pbn">
$year
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/month/#mongodb-expression-exp.-month">
<code class="leafygreen-ui-1l06pbn">
$month
</code>
</a>
operators extract the
year and month, respectively, from the
<code class="leafygreen-ui-1l06pbn">
logoutTime
</code>
Date object. Then
the month and year are checked to see if they match the selection
targets. Since "January" is encoded as "1",
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
is true when
the year and month are equal (
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/eq/#mongodb-expression-exp.-eq">
<code class="leafygreen-ui-1l06pbn">
$eq
</code>
</a>
) to "2021" and "1".
</p>
<p class="leafygreen-ui-1kp3ins">
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 uses
<code class="leafygreen-ui-1l06pbn">
$dateSubtract
</code>
to subtract 3
hours from the
<code class="leafygreen-ui-1l06pbn">
logoutTime
</code>
of each selected dcoument.
</p>
<p class="leafygreen-ui-1kp3ins">
Finaly, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</a>
stage updates the collection, writing
the new
<code class="leafygreen-ui-1l06pbn">
logoutTime
</code>
for the modified documents.
</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">
Unlike
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
<code class="leafygreen-ui-1l06pbn">
$out
</code>
</a>
, the
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
stage only updates the
matched documents and preserves the rest of the collection. For more
details see:
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#std-label-out-merge-comparison">
$out compared with $merge
<!-- -->
.
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The resulting documents look like this:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : ObjectId(\"603dd94b044b995ad331c0b5\"),\n \"custId\" : 457,\n \"login\" : ISODate(\"2020-12-25T19:04:00Z\"),\n \"logout\" : ISODate(\"2020-12-28T09:04:00Z\")\n}\n{\n \"_id\" : ObjectId(\"603dd94b044b995ad331c0b6\"),\n \"custId\" : 457,\n \"login\" : ISODate(\"2021-01-27T05:12:00Z\"),\n \"logout\" : ISODate(\"2021-01-28T13:05:00Z\"),\n \"logoutTime\" : ISODate(\"2021-01-28T10:05:00Z\")\n}\n{\n \"_id\" : ObjectId(\"603dd94b044b995ad331c0b7\"),\n \"custId\" : 458,\n \"login\" : ISODate(\"2021-01-22T06:27:00Z\"),\n \"logout\" : ISODate(\"2021-01-31T11:00:00Z\"),\n \"logoutTime\" : ISODate(\"2021-01-31T08:00:00Z\")\n}\n{\n \"_id\" : ObjectId(\"603dd94b044b995ad331c0b8\"),\n \"custId\" : 459,\n \"login\" : ISODate(\"2021-02-14T20:14:00Z\"),\n \"logout\" : ISODate(\"2021-02-17T16:05:00Z\")\n}\n{\n \"_id\" : ObjectId(\"603dd94b044b995ad331c0b9\"),\n \"custId\" : 460,\n \"login\" : ISODate(\"2021-02-26T02:44:00Z\"),\n \"logout\" : ISODate(\"2021-02-18T14:13:00Z\")\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">"603dd94b044b995ad331c0b5"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"custId"</span> <!-- -->: <span class="lg-highlight-number">457</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"login"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-12-25T19:04:00Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"logout"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-12-28T09:04:00Z"</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-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"603dd94b044b995ad331c0b6"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"custId"</span> <!-- -->: <span class="lg-highlight-number">457</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"login"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-01-27T05:12:00Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"logout"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-01-28T13:05:00Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"logoutTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-01-28T10:05:00Z"</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-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"603dd94b044b995ad331c0b7"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"custId"</span> <!-- -->: <span class="lg-highlight-number">458</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"login"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-01-22T06:27:00Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"logout"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-01-31T11:00:00Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"logoutTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-01-31T08:00:00Z"</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-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"603dd94b044b995ad331c0b8"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"custId"</span> <!-- -->: <span class="lg-highlight-number">459</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"login"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-02-14T20:14:00Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"logout"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-02-17T16:05:00Z"</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-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"603dd94b044b995ad331c0b9"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"custId"</span> <!-- -->: <span class="lg-highlight-number">460</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"login"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-02-26T02:44:00Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"logout"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-02-18T14:13:00Z"</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">
Filter by Relative Dates
<a class="headerlink leafygreen-ui-1kepx47" href="#filter-by-relative-dates" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="filter-by-relative-dates">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You want to send a survey to clients who have used your service in the
past week. The
<code class="leafygreen-ui-1l06pbn">
$dateSubtract
</code>
expression can create a range filter
relative to the time the query is executed.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.connectionTime.aggregate(\n [\n {\n $match:\n {\n $expr:\n {\n $gt:\n [\n \"$logoutTime\",\n {\n $dateSubtract:\n {\n startDate: \"$$NOW\",\n unit: \"week\",\n amount: 1\n }\n }\n ]\n }\n }\n },\n {\n $project:\n {\n _id: 0,\n custId: 1,\n loggedOut:\n {\n $dateToString:\n {\n format: \"%Y-%m-%d\",\n date: \"$logoutTime\"\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">connectionTime</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">$match</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$expr</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$gt</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$logoutTime"</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">$dateSubtract</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"> <span class="lg-highlight-attr">startDate</span>: <span class="lg-highlight-string">"$$NOW"</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">unit</span>: <span class="lg-highlight-string">"week"</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">amount</span>: <span class="lg-highlight-number">1</span></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><tr 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"> <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">_id</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">custId</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">loggedOut</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$dateToString</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"%Y-%m-%d"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-string">"$logoutTime"</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">
The built in 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>
returns the
current datetime in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-ISODate">
ISODate
</a>
format. The
<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 uses the value in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/aggregation-variables/#mongodb-variable-variable.NOW">
<code class="leafygreen-ui-1l06pbn">
$$NOW
</code>
</a>
to get today's date.
Then the comparison expression (
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
) filters the collection
using greater than (
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/gt/#mongodb-expression-exp.-gt">
<code class="leafygreen-ui-1l06pbn">
$gt
</code>
</a>
) and
<code class="leafygreen-ui-1l06pbn">
$dateSubtract
</code>
to match
documents that have a
<code class="leafygreen-ui-1l06pbn">
logoutTime
</code>
in the past week.
</p>
<p class="leafygreen-ui-1kp3ins">
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 uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/dateToString/#mongodb-expression-exp.-dateToString">
<code class="leafygreen-ui-1l06pbn">
$dateToString
</code>
</a>
expression to convert the dates to a more readable format. Without the
conversion, MongoDB returns the date in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-ISODate">
ISODate
</a>
format and
assumes a UTC timezone.
</p>
<p class="leafygreen-ui-1kp3ins">
The output shows two customers have logged out in the last week.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"custId\" : 459, \"loggedOut\" : \"2021-02-17\" }\n{ \"custId\" : 460, \"loggedOut\" : \"2021-02-18\" }","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">"custId"</span> <!-- -->: <span class="lg-highlight-number">459</span>, <span class="lg-highlight-string">"loggedOut"</span> <!-- -->: <span class="lg-highlight-string">"2021-02-17"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"custId"</span> <!-- -->: <span class="lg-highlight-number">460</span>, <span class="lg-highlight-string">"loggedOut"</span> <!-- -->: <span class="lg-highlight-string">"2021-02-18"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Adjust for Daylight Savings Time
<a class="headerlink leafygreen-ui-1kepx47" href="#adjust-for-daylight-savings-time" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="adjust-for-daylight-savings-time">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
All dates are stored internally in UTC time. When a
<code class="leafygreen-ui-1l06pbn">
timezone
</code>
is
specified,
<code class="leafygreen-ui-1l06pbn">
$dateSubtract
</code>
uses local time to carry out the
calculations. The results are displayed in UTC.
</p>
<p class="leafygreen-ui-1kp3ins">
You have customers in several timezones and you want to see what effect
daylight savings time might have on your billing periods if you bill by
<code class="leafygreen-ui-1l06pbn">
day
</code>
or by
<code class="leafygreen-ui-1l06pbn">
hour
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
Create this collection of connection times:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.billing.insertMany(\n [\n {\n location: \"America/New_York\",\n login: ISODate(\"2021-03-14T10:00:00-0500\"),\n logout: ISODate(\"2021-03-14T18:00:00-0500\")\n },\n {\n location: \"America/Mexico_City\",\n login: ISODate(\"2021-03-14T10:00:00-00:00\"),\n logout: ISODate(\"2021-03-15T08:00:00-0500\")\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">billing</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"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">location</span>: <span class="lg-highlight-string">"America/New_York"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">login</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-14T10:00:00-0500"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">logout</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-14T18:00:00-0500"</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">location</span>: <span class="lg-highlight-string">"America/Mexico_City"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">login</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-14T10:00:00-00:00"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">logout</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-15T08:00:00-0500"</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">
First subtract 1 day, then subtract 24 hours from the
<code class="leafygreen-ui-1l06pbn">
login
</code>
dates
in each document.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.billing.aggregate(\n [\n {\n $project:\n {\n _id: 0,\n location: 1,\n start:\n {\n $dateToString:\n {\n format: \"%Y-%m-%d %H:%M\",\n date: \"$login\"\n }\n },\n days:\n {\n $dateToString:\n {\n format: \"%Y-%m-%d %H:%M\",\n date:\n {\n $dateSubtract:\n {\n startDate: \"$login\",\n unit: \"day\",\n amount: 1,\n timezone: \"$location\"\n }\n }\n }\n },\n hours:\n {\n $dateToString:\n {\n format: \"%Y-%m-%d %H:%M\",\n date:\n {\n $dateSubtract:\n {\n startDate: \"$login\",\n unit: \"hour\",\n amount: 24,\n timezone: \"$location\"\n }\n }\n }\n },\n startTZInfo:\n {\n $dateToString:\n {\n format: \"%Y-%m-%d %H:%M\",\n date: \"$login\",\n timezone: \"$location\"\n }\n },\n daysTZInfo:\n {\n $dateToString:\n {\n format: \"%Y-%m-%d %H:%M\",\n date:\n {\n $dateSubtract:\n {\n startDate: \"$login\",\n unit: \"day\",\n amount: 1,\n timezone: \"$location\"\n }\n },\n timezone: \"$location\"\n }\n },\n hoursTZInfo:\n {\n $dateToString:\n {\n format: \"%Y-%m-%d %H:%M\",\n date:\n {\n $dateSubtract:\n {\n startDate: \"$login\",\n unit: \"hour\",\n amount: 24,\n timezone: \"$location\"\n }\n },\n timezone: \"$location\"\n }\n },\n }\n }\n ]\n).pretty()","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">billing</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">_id</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">location</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">start</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$dateToString</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"%Y-%m-%d %H:%M"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-string">"$login"</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">days</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$dateToString</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"%Y-%m-%d %H:%M"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">date</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$dateSubtract</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">startDate</span>: <span class="lg-highlight-string">"$login"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">unit</span>: <span class="lg-highlight-string">"day"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">amount</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timezone</span>: <span class="lg-highlight-string">"$location"</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"> <span class="lg-highlight-attr">hours</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$dateToString</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"%Y-%m-%d %H:%M"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">date</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$dateSubtract</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">startDate</span>: <span class="lg-highlight-string">"$login"</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"> <span class="lg-highlight-attr">amount</span>: <span class="lg-highlight-number">24</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timezone</span>: <span class="lg-highlight-string">"$location"</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"> <span class="lg-highlight-attr">startTZInfo</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$dateToString</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"%Y-%m-%d %H:%M"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-string">"$login"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timezone</span>: <span class="lg-highlight-string">"$location"</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">daysTZInfo</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$dateToString</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"%Y-%m-%d %H:%M"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">date</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$dateSubtract</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">startDate</span>: <span class="lg-highlight-string">"$login"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">unit</span>: <span class="lg-highlight-string">"day"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">amount</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timezone</span>: <span class="lg-highlight-string">"$location"</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">timezone</span>: <span class="lg-highlight-string">"$location"</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">hoursTZInfo</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$dateToString</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"%Y-%m-%d %H:%M"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">date</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$dateSubtract</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">startDate</span>: <span class="lg-highlight-string">"$login"</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"> <span class="lg-highlight-attr">amount</span>: <span class="lg-highlight-number">24</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timezone</span>: <span class="lg-highlight-string">"$location"</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">timezone</span>: <span class="lg-highlight-string">"$location"</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">).<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>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/dateToString/#mongodb-expression-exp.-dateToString">
<code class="leafygreen-ui-1l06pbn">
$dateToString
</code>
</a>
expression reformats the output for
readability. Results are summarized here:
</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>
Field
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
New York
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Mexico City
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Start
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2021-03-14 15:00
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2021-03-14 15:00
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Start, TZ Info
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2021-03-14 11:00
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2021-03-14 04:00
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
1 Day
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2021-03-13 16:00
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2021-03-13 15:00
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
1 Day, TZInfo
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2021-03-13 11:00
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2021-03-13 09:00
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
24 Hours
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2021-03-13 15:00
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2021-03-13 15:00
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
24 Hours, TZInfo
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2021-03-13 10:00
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2021-03-13 09:00
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The chart highlights several points:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Unformatted dates are returned in UTC. The
<code class="leafygreen-ui-1l06pbn">
$login
</code>
for New York is
UTC -5, however the
<code class="leafygreen-ui-1l06pbn">
start
</code>
,
<code class="leafygreen-ui-1l06pbn">
days
</code>
, and
<code class="leafygreen-ui-1l06pbn">
hours
</code>
rows display
the time in UTC.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
March 14th is the start of DST in New York, but not in Mexico. The
calculated time is adjusted when a location switches to DST and
crosses from one
<code class="leafygreen-ui-1l06pbn">
day
</code>
to the next.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
DST modifies the length of the
<code class="leafygreen-ui-1l06pbn">
day
</code>
, not the
<code class="leafygreen-ui-1l06pbn">
hour
</code>
. There is no
DST change for
<code class="leafygreen-ui-1l06pbn">
hours
</code>
. There is an only an adjustment for DST when
the measurement
<code class="leafygreen-ui-1l06pbn">
unit
</code>
is
<code class="leafygreen-ui-1l06pbn">
day
</code>
or larger and the computation
crosses a clock change in the specified
<code class="leafygreen-ui-1l06pbn">
timezone
</code>
.
</p>
</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">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/dateAdd/#mongodb-expression-exp.-dateAdd">
<code class="leafygreen-ui-1l06pbn">
$dateAdd
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/dateDiff/#mongodb-expression-exp.-dateDiff">
<code class="leafygreen-ui-1l06pbn">
$dateDiff
</code>
</a>
</p>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/aggregation/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Aggregation Reference
<a class="headerlink leafygreen-ui-1kepx47" href="#aggregation-reference" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-reference">
</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="#aggregation-resources" target="_self">
<span>
Aggregation 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>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Aggregation Resources
<a class="headerlink leafygreen-ui-1kepx47" href="#aggregation-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="aggregation-resources">
</div>
</a>
</h2>
<dl>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/interface/">
Aggregation Commands
</a>
</dt>
<dd class="css-1ufxuu7">
The reference for the data aggregation commands, which provide the
interfaces to MongoDB's aggregation capability.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation-pipeline/">
Aggregation Stages
</a>
</dt>
<dd class="css-1ufxuu7">
Reference for aggregation pipeline stages. Aggregation pipelines are
made up of an array of stages. Documents pass through each stage in
sequence.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/">
Aggregation Operators
</a>
</dt>
<dd class="css-1ufxuu7">
Reference for aggregation pipeline operators. Use operators in stages
to modify documents and perform calculations.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/aggregation-variables/">
Variables in Aggregation Expressions
</a>
</dt>
<dd class="css-1ufxuu7">
Use of variables in aggregation pipeline expressions.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/sql-aggregation-comparison/">
SQL to Aggregation Mapping Chart
</a>
</dt>
<dd class="css-1ufxuu7">
An overview common aggregation operations in SQL and MongoDB using
the aggregation pipeline and operators in MongoDB and common SQL
statements.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/aggregation-commands-comparison/">
Aggregation Commands Comparison
</a>
</dt>
<dd class="css-1ufxuu7">
A comparison of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mapReduce/#mongodb-dbcommand-dbcmd.mapReduce">
<code class="leafygreen-ui-1l06pbn">
mapReduce
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#mongodb-dbcommand-dbcmd.aggregate">
<code class="leafygreen-ui-1l06pbn">
aggregate
</code>
</a>
commands.
</dd>
<dt>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.practical-mongodb-aggregations.com" rel="noopener noreferrer">
<span>
Practical MongoDB Aggregations (e-book)
</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>
</dt>
<dd class="css-1ufxuu7">
For more information on aggregations, read the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.practical-mongodb-aggregations.com" 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.
</dd>
</dl>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/index-creation/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Index Builds on Populated Collections
<a class="headerlink leafygreen-ui-1kepx47" href="#index-builds-on-populated-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="index-builds-on-populated-collections">
</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="#index-build-impact-on-database-performance" target="_self">
<span>
Index Build Impact on Database Performance
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#index-builds-in-replicated-environments" target="_self">
<span>
Index Builds in Replicated Environments
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#build-failure-and-recovery" target="_self">
<span>
Build Failure and Recovery
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#monitor-in-progress-index-builds" target="_self">
<span>
Monitor In Progress Index Builds
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#terminate-in-progress-index-builds" target="_self">
<span>
Terminate In Progress Index Builds
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#index-build-process" target="_self">
<span>
Index Build Process
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
Index builds use an optimized build process that holds an exclusive lock on the
collection at the beginning and end of the index build. The rest of the build
process yields to interleaving read and write operations. For a detailed
description of index build process and locking behavior, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-index-build-process" target="_self">
<span>
Index Build Process
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Index builds on a replica set or sharded cluster build simultaneously across
all data-bearing replica set members. The primary requires a minimum number of
data-bearing voting members (i.e. commit quorum), including itself, that must
complete the build before marking the index as ready for use. A "voting" member
is any replica set member where
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-.votes">
<code class="leafygreen-ui-1l06pbn">
members[n].votes
</code>
</a>
is greater than
<code class="leafygreen-ui-1l06pbn">
0
</code>
. See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-index-operations-replicated-build" target="_self">
<span>
Index Builds in Replicated Environments
</span>
</a>
for more
information.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 7.1, index builds are improved with faster error
reporting and increased failure resilience. You can also set the minimum
available disk space required for index builds using the new
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.indexBuildMinAvailableDiskSpaceMB">
<code class="leafygreen-ui-1l06pbn">
indexBuildMinAvailableDiskSpaceMB
</code>
</a>
parameter, which stops
index builds if disk space is too low.
</p>
<p class="leafygreen-ui-1kp3ins">
The following table compares the index build behavior starting in
MongoDB 7.1 with earlier versions.
</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>
Behavior Starting in MongoDB 7.1
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Behavior in Earlier MongoDB Versions
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Index errors found during the collection scan phase, except
duplicate key errors, are returned immediately and then the index
build stops. Earlier MongoDB versions return errors in the commit
phase, which occurs near the end of the index build. MongoDB 7.1
helps you to rapidly diagnose index errors. For example, if an
incompatible index value format is found, the error is returned to
you immediately.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Index build errors can take a long time to be returned compared to
MongoDB 7.1 because the errors are returned near the end of the
index build in the commit phase.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Increased resilience for your deployment. If an index build error
occurs, a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary">
secondary
</a>
member can request that the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
member stop an index build and the secondary
member does not crash. A request to stop an index build is not
always possible: if a member has already voted to commit the
index, then the secondary cannot request that the index build stop
and the secondary crashes (similar to MongoDB 7.0 and earlier).
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
An index build error can cause a secondary member to crash.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Improved disk space management for index builds. An index build
may be automatically stopped if the available disk space is below
the minimum specified in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.indexBuildMinAvailableDiskSpaceMB">
<code class="leafygreen-ui-1l06pbn">
indexBuildMinAvailableDiskSpaceMB
</code>
</a>
parameter. If a
member has already voted to commit the index, then the index build
is not stopped.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
An index build is not stopped if there is insufficient available
disk space.
</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">
For information about creating indexes in Atlas, refer to the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-ui/indexes/" target="_self">
<span>
index management
</span>
</a>
page in the Atlas
documentation.
</p>
</div>
</div>
<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">
Comparison to Foreground and Background Builds
<a class="headerlink leafygreen-ui-1kepx47" href="#comparison-to-foreground-and-background-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="comparison-to-foreground-and-background-builds">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Previous versions of MongoDB supported building indexes either in the
foreground or background. Foreground index builds were fast and produced
more efficient index data structures, but required blocking all
read-write access to the parent database of the collection being indexed
for the duration of the build. Background index builds were slower and
had less efficient results, but allowed read-write access to the
database and its collections during the build process.
</p>
<p class="leafygreen-ui-1kp3ins">
Index builds now obtain an exclusive lock on only the collection being indexed
during the start and end of the build process to protect metadata changes.
The rest of the build process uses the yielding behavior of background index
builds to maximize read-write access to the collection during the build.
Index builds still produce efficient index data structures despite the more
permissive locking behavior.
</p>
<p class="leafygreen-ui-1kp3ins">
The optimized index build performance is at least on par with background
index builds. For workloads with few or no updates received during the
build process, optimized index builds can be as fast as a foreground
index build on that same data.
</p>
<p class="leafygreen-ui-1kp3ins">
Use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.currentOp/#mongodb-method-db.currentOp">
<code class="leafygreen-ui-1l06pbn">
db.currentOp()
</code>
</a>
to monitor the progress of ongoing index
builds.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB ignores the
<code class="leafygreen-ui-1l06pbn">
background
</code>
index build option if specified to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#mongodb-dbcommand-dbcmd.createIndexes">
<code class="leafygreen-ui-1l06pbn">
createIndexes
</code>
</a>
or its shell helpers
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.createIndex/#mongodb-method-db.collection.createIndex">
<code class="leafygreen-ui-1l06pbn">
createIndex()
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.createIndexes/#mongodb-method-db.collection.createIndexes">
<code class="leafygreen-ui-1l06pbn">
createIndexes()
</code>
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-index-build-constraint-violation">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Constraint Violations During Index Build
<a class="headerlink leafygreen-ui-1kepx47" href="#constraint-violations-during-index-build" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="constraint-violations-during-index-build">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
For indexes that enforce constraints on the collection, such as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-unique/#std-label-index-type-unique">
unique
</a>
indexes, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
checks all pre-existing and concurrently-written documents for
violations of those constraints
<em>
after
</em>
the index build completes.
Documents that violate the index constraints can exist during the index
build. If any documents violate the index constraints at the end of the
build, the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
terminates the build and throws an
error.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, consider a populated collection
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
. An
administrator wants to create a unique index on the
<code class="leafygreen-ui-1l06pbn">
product_sku
</code>
field. If any documents in the collection have duplicate values for
<code class="leafygreen-ui-1l06pbn">
product_sku
</code>
, the index build can still start successfully.
If any violations still exist at the end of the build,
the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
terminates the build and throws an error.
</p>
<p class="leafygreen-ui-1kp3ins">
Similarly, an application can successfully write documents to the
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection with duplicate values of
<code class="leafygreen-ui-1l06pbn">
product_sku
</code>
while
the index build is in progress. If any violations still exist at the end
of the build, the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
terminates the build and throws
an error.
</p>
<p class="leafygreen-ui-1kp3ins">
To mitigate the risk of index build failure due to constraint
violations:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Validate that no documents in the collection violate the index
constraints.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Stop all writes to the collection from applications that cannot
guarantee violation-free write operations.
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-index-build-constraint-violation-sharded">
</span>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
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>
</h4>
<p class="leafygreen-ui-1kp3ins">
For a sharded collection distributed across multiple shards, one or
more shards may contain a chunk with duplicate documents. As such, the
create index operation may succeed on some of the shards (i.e. the ones
without duplicates) but not on others (i.e. the ones with duplicates).
To avoid leaving inconsistent indexes across shards, you can issue 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>
from a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
to
drop the index from the collection.
</p>
<p class="leafygreen-ui-1kp3ins">
To mitigate the risk of this occurrence, before creating the index:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Validate that no documents in the collection violate the index
constraints.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Stop all writes to the collection from applications that cannot
guarantee violation-free write operations.
</p>
</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">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-index-creation-index-consistency" target="_self">
<span>
Index Consistency Checks for Sharded Collections
</span>
</a>
</p>
</div>
</div>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Maximum Concurrent Index Builds
<a class="headerlink leafygreen-ui-1kepx47" href="#maximum-concurrent-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="maximum-concurrent-index-builds">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
By default, the server allows up to three concurrent index builds. To
change the number of allowed concurrent index builds, modify the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.maxNumActiveUserIndexBuilds">
<code class="leafygreen-ui-1l06pbn">
maxNumActiveUserIndexBuilds
</code>
</a>
parameter.
</p>
<p class="leafygreen-ui-1kp3ins">
If the number of concurrent index builds reaches the limit specified by
<code class="leafygreen-ui-1l06pbn">
maxNumActiveUserIndexBuilds
</code>
, the server blocks additional index
builds until the number of concurrent index builds drops below the
limit.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Index Build Impact on Database Performance
<a class="headerlink leafygreen-ui-1kepx47" href="#index-build-impact-on-database-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="index-build-impact-on-database-performance">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Index Builds During Write-Heavy Workloads
<a class="headerlink leafygreen-ui-1kepx47" href="#index-builds-during-write-heavy-workloads" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-builds-during-write-heavy-workloads">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Building indexes during time periods where the target collection
is under heavy write load can result in reduced write
performance and longer index builds.
</p>
<p class="leafygreen-ui-1kp3ins">
Consider designating a maintenance window during which applications
stop or reduce write operations against the collection. Start the
index build during this maintenance window to mitigate the
potential negative impact of the build process.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Insufficient Available System Memory (RAM)
<a class="headerlink leafygreen-ui-1kepx47" href="#insufficient-available-system-memory--ram-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="insufficient-available-system-memory--ram-">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#mongodb-dbcommand-dbcmd.createIndexes">
<code class="leafygreen-ui-1l06pbn">
createIndexes
</code>
</a>
supports building one or more indexes on a
collection.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#mongodb-dbcommand-dbcmd.createIndexes">
<code class="leafygreen-ui-1l06pbn">
createIndexes
</code>
</a>
uses a combination of memory and
temporary files on disk to complete index builds. The default limit on
memory usage for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#mongodb-dbcommand-dbcmd.createIndexes">
<code class="leafygreen-ui-1l06pbn">
createIndexes
</code>
</a>
is 200 megabytes,
shared between all indexes built using a single
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#mongodb-dbcommand-dbcmd.createIndexes">
<code class="leafygreen-ui-1l06pbn">
createIndexes
</code>
</a>
command. Once the memory limit is reached,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#mongodb-dbcommand-dbcmd.createIndexes">
<code class="leafygreen-ui-1l06pbn">
createIndexes
</code>
</a>
uses temporary disk files in a subdirectory
named
<code class="leafygreen-ui-1l06pbn">
_tmp
</code>
within the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--dbpath">
<code class="leafygreen-ui-1l06pbn">
--dbpath
</code>
</a>
directory to complete the build.
</p>
<p class="leafygreen-ui-1kp3ins">
You can override the memory limit by setting the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.maxIndexBuildMemoryUsageMegabytes">
<code class="leafygreen-ui-1l06pbn">
maxIndexBuildMemoryUsageMegabytes
</code>
</a>
server parameter.
Setting a higher memory limit may result in faster completion of index
builds. However, setting this limit too high relative to the unused RAM
on your system can result in memory exhaustion and server shutdown.
</p>
<p class="leafygreen-ui-1kp3ins">
If the host machine has limited available free RAM, you may need
to schedule a maintenance period to increase the total system RAM
before you can modify the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
RAM usage.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-index-operations-replicated-build">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-index-operations-simultaneous-build">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Index Builds in Replicated Environments
<a class="headerlink leafygreen-ui-1kepx47" href="#index-builds-in-replicated-environments" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-builds-in-replicated-environments">
</div>
</a>
</h2>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Requires featureCompatibilityVersion 4.4+
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Each
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
in the replica set or sharded cluster
<em>
must
</em>
have
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setFeatureCompatibilityVersion/#std-label-set-fcv">
featureCompatibilityVersion
</a>
set to at
least
<code class="leafygreen-ui-1l06pbn">
4.4
</code>
to start index builds simultaneously across
replica set members.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Index builds on a replica set or sharded cluster build simultaneously across
all data-bearing replica set members. For sharded clusters, the index build
occurs only on shards containing data for the collection being indexed. The
primary requires a minimum number of data-bearing
<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>
members (i.e commit quorum), including itself,
that must complete the build before marking the index as ready for
use.
</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">
If a data-bearing voting node becomes unreachable and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#std-label-createIndexes-cmd-commitQuorum">
commitQuorum
</a>
is set to the
default
<code class="leafygreen-ui-1l06pbn">
votingMembers
</code>
, index builds can hang until that node
comes back online.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The build process is summarized as follows:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The primary receives the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#mongodb-dbcommand-dbcmd.createIndexes">
<code class="leafygreen-ui-1l06pbn">
createIndexes
</code>
</a>
command and
immediately creates a "startIndexBuild" oplog entry associated with
the index build.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The secondaries start the index build after they replicate the
"startIndexBuild" oplog entry.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Each member "votes" to commit the build once it finishes indexing
data in the collection.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Secondary members continue to process any new write operations into
the index while waiting for the primary to confirm a quorum of votes.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
When the primary has a quorum of votes, it checks for any key
constraint violations such as duplicate key errors.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If there are no key constraint violations, the primary completes
the index build, marks the index as ready for use, and creates an
associated "commitIndexBuild" oplog entry.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If there are any key constraint violations, the index build
fails. The primary aborts the index build and creates an
associated "abortIndexBuild" oplog entry.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The secondaries replicate the "commitIndexBuild" oplog entry and
complete the index build.
</p>
<p class="leafygreen-ui-1kp3ins">
If the secondaries instead replicate an "abortIndexBuild" oplog
entry, they abort the index build and discard the build job.
</p>
</li>
</ol>
<p class="leafygreen-ui-1kp3ins">
For sharded clusters, the index build occurs only on shards
containing data for the collection being indexed.
</p>
<p class="leafygreen-ui-1kp3ins">
For a more detailed description of the index build process,
see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-index-build-process" target="_self">
<span>
Index Build Process
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
By default, index builds use a commit quorum of
<code class="leafygreen-ui-1l06pbn">
"votingMembers"
</code>
, or
all data-bearing voting members. To start an index build with a
non-default commit quorum, specify the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#std-label-createIndexes-cmd-commitQuorum">
commitQuorum
</a>
parameter to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#mongodb-dbcommand-dbcmd.createIndexes">
<code class="leafygreen-ui-1l06pbn">
createIndexes
</code>
</a>
or its shell helpers
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.createIndex/#mongodb-method-db.collection.createIndex">
<code class="leafygreen-ui-1l06pbn">
db.collection.createIndex()
</code>
</a>
and
<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>
</p>
<p class="leafygreen-ui-1kp3ins">
To modify the commit quorum required for an in-progress simultaneous
index build, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setIndexCommitQuorum/#mongodb-dbcommand-dbcmd.setIndexCommitQuorum">
<code class="leafygreen-ui-1l06pbn">
setIndexCommitQuorum
</code>
</a>
command.
</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">
Index builds can impact replica set performance. For workloads which
cannot tolerate performance decrease due to index builds, consider
performing a rolling index build process. Rolling index builds take
at most one replica set member out at a time, starting with the
secondary members, and builds the index on that member as a
standalone. Rolling index builds require at least one replica set
election.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For rolling index builds on replica sets, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/build-indexes-on-replica-sets/">
Rolling Index Builds on Replica Sets
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For rolling index builds on sharded clusters, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/build-indexes-on-sharded-clusters/">
Rolling Index Builds on Sharded Clusters
<!-- -->
.
</a>
</p>
</li>
</ul>
</div>
</div>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Commit Quorum Contrasted with Write Concern
<a class="headerlink leafygreen-ui-1kepx47" href="#commit-quorum-contrasted-with-write-concern" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-quorum-contrasted-with-write-concern">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
There are important differences between
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#std-label-createIndexes-cmd-commitQuorum">
commit quorums
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-write-concern">
write concerns
<!-- -->
:
</a>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Index builds use commit quorums.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Write operations use write concerns.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Each data-bearing node in a cluster is a voting member.
</p>
<p class="leafygreen-ui-1kp3ins">
The commit quorum specifies how many data-bearing voting members, or
which voting members, including the primary, must be prepared to commit
a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-index-operations-simultaneous-build" target="_self">
<span>
simultaneous index build
</span>
</a>
before the primary will execute the commit.
</p>
<p class="leafygreen-ui-1kp3ins">
The write concern is the level of acknowledgment that the write has
propagated to the specified number of instances.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
8.0
</em>
:
<!-- -->
<!-- -->
The commit quorum specifies how many
nodes must be ready to finish the index build before the
primary commits the index build. In contrast, when the
primary has committed the index build, the write concern
specifies how many nodes must replicate the index build oplog
entry before the command returns success.
</p>
<p class="leafygreen-ui-1kp3ins">
In previous releases, when the primary committed the index
build, the write concern specified how many nodes must finish
the index build before the command returned success.
</p>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-index-operations-build-failure">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Build Failure and Recovery
<a class="headerlink leafygreen-ui-1kepx47" href="#build-failure-and-recovery" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="build-failure-and-recovery">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Interrupted Index Builds on a Primary
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#interrupted-index-builds-on-a-primary-mongod" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="interrupted-index-builds-on-a-primary-mongod">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.0, if the primary
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
performs a clean
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/shutdown/#mongodb-dbcommand-dbcmd.shutdown">
<code class="leafygreen-ui-1l06pbn">
shutdown
</code>
</a>
with
<code class="leafygreen-ui-1l06pbn">
"force" : true
</code>
or
receives a
<code class="leafygreen-ui-1l06pbn">
SIGTERM
</code>
signal during an index build and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#std-label-createIndexes-cmd-commitQuorum">
commitQuorum
</a>
is set to the
default
<code class="leafygreen-ui-1l06pbn">
votingMembers
</code>
, then the index build progress is saved to
disk. The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
automatically recovers the index
build when it is restarted and continues from the saved checkpoint.
In earlier versions, if the index build is interrupted, it has to
be restarted from the beginning.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Interrupted Index Builds on a Secondary
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#interrupted-index-builds-on-a-secondary-mongod" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="interrupted-index-builds-on-a-secondary-mongod">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.0, if a secondary
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
performs
a clean
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/shutdown/#mongodb-dbcommand-dbcmd.shutdown">
<code class="leafygreen-ui-1l06pbn">
shutdown
</code>
</a>
with
<code class="leafygreen-ui-1l06pbn">
"force" : true
</code>
or receives a
<code class="leafygreen-ui-1l06pbn">
SIGTERM
</code>
signal during an index build and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/createIndexes/#std-label-createIndexes-cmd-commitQuorum">
commitQuorum
</a>
is set to the
default
<code class="leafygreen-ui-1l06pbn">
votingMembers
</code>
, then the index build progress is saved to
disk. The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
automatically recovers the index build
when it is restarted and continues from the saved checkpoint. In earlier
versions, if the index build is interrupted, it has to be restarted from
the beginning.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
can perform the startup process while the
recovering index builds.
</p>
<p class="leafygreen-ui-1kp3ins">
If you restart the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
as a standalone (i.e. removing
or commenting out
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-replication.replSetName">
<code class="leafygreen-ui-1l06pbn">
replication.replSetName
</code>
</a>
or omitting
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--replSet">
<code class="leafygreen-ui-1l06pbn">
--replSetName
</code>
</a>
), the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
cannot restart the index build. The build remains in a paused
state until it is manually
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dropIndexes/#mongodb-dbcommand-dbcmd.dropIndexes">
<code class="leafygreen-ui-1l06pbn">
dropped
</code>
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Interrupted Index Builds on Standalone
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#interrupted-index-builds-on-standalone-mongod" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="interrupted-index-builds-on-standalone-mongod">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
shuts down during the index build, the
index build job and all progress is lost. Restarting the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
does not restart the index build. You must
re-issue the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.createIndex/#mongodb-method-db.collection.createIndex">
<code class="leafygreen-ui-1l06pbn">
createIndex()
</code>
</a>
operation to restart
the index build.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Rollbacks during Build Process
<a class="headerlink leafygreen-ui-1kepx47" href="#rollbacks-during-build-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="rollbacks-during-build-process">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.0, if a node is rolled back to a prior state
during the index build, the index build progress is saved to disk. If
there is still work to be done when the rollback concludes, the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
automatically recovers the index build and
continues from the saved checkpoint.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB can pause an in-progress index build to perform a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-rollbacks/#std-label-replica-set-rollbacks">
rollback
<!-- -->
.
</a>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the rollback does not revert the index build, MongoDB restarts
the index build after completing the rollback.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the rollback reverts the index build, you must re-create the
index or indexes after the rollback completes.
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-index-creation-index-consistency">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Index Consistency Checks for Sharded Collections
<a class="headerlink leafygreen-ui-1kepx47" href="#index-consistency-checks-for-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="index-consistency-checks-for-sharded-collections">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
A sharded collection has an inconsistent index if the collection does
not have the exact same indexes (including the index options) on each
shard that contains chunks for the collection. Although inconsistent
indexes should not occur during normal operations, inconsistent indexes
can occur, such as:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
When a user is creating an index with a
<code class="leafygreen-ui-1l06pbn">
unique
</code>
key constraint and
one shard contains a chunk with duplicate documents. In such cases,
the create index operation may succeed on the shards without
duplicates but not on the shard with duplicates.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
When a user is creating an index across the shards in a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/build-indexes-on-sharded-clusters/">
rolling
manner (i.e. manually building the index one by one across the
shards)
</a>
but either
fails to build the index for an associated shard or incorrectly
builds an index with different specification.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-config-servers/#std-label-sharding-config-server">
config server
</a>
primary periodically checks
for index inconsistencies across the shards for sharded collections. To
configure these periodic checks, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.enableShardedIndexConsistencyCheck">
<code class="leafygreen-ui-1l06pbn">
enableShardedIndexConsistencyCheck
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.shardedIndexConsistencyCheckIntervalMS">
<code class="leafygreen-ui-1l06pbn">
shardedIndexConsistencyCheckIntervalMS
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The command
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/serverStatus/#mongodb-dbcommand-dbcmd.serverStatus">
<code class="leafygreen-ui-1l06pbn">
serverStatus
</code>
</a>
returns the field
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/serverStatus/#mongodb-serverstatus-serverstatus.shardedIndexConsistency">
<code class="leafygreen-ui-1l06pbn">
shardedIndexConsistency
</code>
</a>
to report on index
inconsistencies when run on the config server primary.
</p>
<p class="leafygreen-ui-1kp3ins">
To check if a sharded collection has inconsistent indexes, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-indexes/#std-label-manage-indexes-find-inconsistent-indexes">
Find Inconsistent Indexes Across Shards
<!-- -->
.
</a>
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Monitor In Progress Index Builds
<a class="headerlink leafygreen-ui-1kepx47" href="#monitor-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="monitor-in-progress-index-builds">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To see the status of an index build operation, you can use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.currentOp/#mongodb-method-db.currentOp">
<code class="leafygreen-ui-1l06pbn">
db.currentOp()
</code>
</a>
method 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
filter the current operations for index creation operations, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.currentOp/#std-label-currentOp-index-creation">
Active Indexing Operations
</a>
for an example.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-data-currentOp.msg">
<code class="leafygreen-ui-1l06pbn">
msg
</code>
</a>
field includes a percentage-complete
measurement of the current stage in the index build process.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Observe Stopped and Resumed Index Builds in the Logs
<a class="headerlink leafygreen-ui-1kepx47" href="#observe-stopped-and-resumed-index-builds-in-the-logs" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="observe-stopped-and-resumed-index-builds-in-the-logs">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
While an index is being built, progress is written to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/log-messages/#std-label-log-messages-ref">
MongoDB log
</a>
. If an index build is
stopped and resumed there will be log messages with fields like these:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":" \"msg\":\"Index build: wrote resumable state to disk\",\n\n \"msg\":\"Found index from unfinished build\",","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"> <span class="lg-highlight-string">"msg"</span>:<span class="lg-highlight-string">"Index build: wrote resumable state to disk"</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-string">"msg"</span>:<span class="lg-highlight-string">"Found index from unfinished build"</span>,</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Terminate In Progress Index Builds
<a class="headerlink leafygreen-ui-1kepx47" href="#terminate-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="terminate-in-progress-index-builds">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dropIndexes/#mongodb-dbcommand-dbcmd.dropIndexes">
<code class="leafygreen-ui-1l06pbn">
dropIndexes
</code>
</a>
command or its shell helpers
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.dropIndex/#mongodb-method-db.collection.dropIndex">
<code class="leafygreen-ui-1l06pbn">
dropIndex()
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.dropIndexes/#mongodb-method-db.collection.dropIndexes">
<code class="leafygreen-ui-1l06pbn">
dropIndexes()
</code>
</a>
to terminate an in-progress index
build. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dropIndexes/#std-label-dropIndexes-cmd-index-builds">
Stop In-Progress Index Builds
</a>
for more information.
</p>
<p class="leafygreen-ui-1kp3ins">
Do
<em>
not
</em>
use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/killOp/#mongodb-dbcommand-dbcmd.killOp">
<code class="leafygreen-ui-1l06pbn">
killOp
</code>
</a>
to terminate an in-progress index
builds in replica sets or sharded clusters.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-index-build-process">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Index Build Process
<a class="headerlink leafygreen-ui-1kepx47" href="#index-build-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="index-build-process">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following table describes each stage of the index build
process:
</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>
Stage
</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>
Lock
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
obtains an exclusive
<code class="leafygreen-ui-1l06pbn">
X
</code>
lock on the
the collection being indexed. This blocks all read and write
operations on the collection, including the application
of any replicated write operations or metadata commands that
target the collection. The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
does not yield
this lock.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Initialization
</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">
mongod
</code>
creates three data structures
at this initial state:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The initial index metadata entry.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A temporary table ("side writes table") that stores keys
generated from writes to the collection being indexed
during the build process.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A temporary table ("constraint violation table") for all
documents that may cause a key generation error. Key
generation errors occur when a document has invalid keys for the
indexed fields. For example, a document with duplicate field
values when building a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-unique/#std-label-index-type-unique">
unique index
</a>
<em>
or
</em>
malformed
<a class="leafygreen-ui-tqgtui" href="/docs/manual/geospatial-queries/#std-label-geospatial-geojson">
GeoJSON objects
</a>
when
building a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2dsphere/#std-label-2dsphere-index">
2dsphere index
<!-- -->
.
</a>
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Lock
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
downgrades the exclusive
<code class="leafygreen-ui-1l06pbn">
X
</code>
collection lock to an intent exclusive
<code class="leafygreen-ui-1l06pbn">
IX
</code>
lock. The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
periodically yields
this lock to interleaving read and write operations.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Scan Collection
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
For each document in the collection, the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
generates a key for that document and dumps the
key into an external sorter.
</p>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
encounters a key generation
error while generating a key during the collection scan,
it stores that key in the constraint violation table for later
processing.
</p>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
encounters any other error while
generating a key, the build fails with an error.
</p>
<p class="leafygreen-ui-1kp3ins">
Once the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
completes the collection
scan, it dumps the sorted keys into the index.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Process Side Writes Table
</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">
mongod
</code>
drains the side write table using
first-in-first-out priority.
</p>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
encounters a key generation error
while processing a key in the side write table, it stores that key
in the constraint violation table for later processing.
</p>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
encounters any other error while
processing a key, the build fails with an error.
</p>
<p class="leafygreen-ui-1kp3ins">
For each document written to the collection during the build
process, the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
generates a key for that
document and stores it in the side write table for later
processing. The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
uses a snapshot system to
set a limit to the number of keys to process.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Vote and Wait for Commit Quorum
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
A
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
that is
<em>
not
</em>
part of a replica set
skips this stage.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
submits a "vote" to the primary to commit the
index. Specifically, it writes the "vote" to an internal replicated
collection on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
is the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
, it waits
until it has a commit quorum of votes (all voting data-bearing
members by default) before continuing the index build process.
</p>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
is a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary">
secondary
</a>
, it waits
until it replicates either a "commitIndexBuild" or
"abortIndexBuild" oplog entry:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
replicates a "commitIndexBuild"
oplog entry, it finishes draining the side writes table and
moves to the next stage in the index build process.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
replicates an "abortIndexBuild"
oplog entry, it aborts the index build and discards the build
job.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
While waiting for commit quorum, the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
adds
any additional keys generated from write operations to the
collection being indexed to the side writes table and periodically
drains the table.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Lock
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
upgrades the intent exclusive
<code class="leafygreen-ui-1l06pbn">
IX
</code>
lock on the collection to a shared
<code class="leafygreen-ui-1l06pbn">
S
</code>
lock. This
blocks all write operations to the collection, including the
application of any replicated write operations or metadata
commands that target the collection.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Finish Processing Temporary Side Writes Table
</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">
mongod
</code>
continues draining remaining
records in the side writes table. The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
may
pause replication during this stage.
</p>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
encounters a key generation error
while processing a key in the side write table, it stores that key
in the constraint violation table for later processing.
</p>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
encounters any other error while
processing a key, the build fails with an error.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Lock
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
upgrades the shared
<code class="leafygreen-ui-1l06pbn">
S
</code>
lock on the
collection to an exclusive
<code class="leafygreen-ui-1l06pbn">
X
</code>
lock on the collection. This
blocks all read and write operations on the collection, including
the application of any replicated write operations or metadata
commands that target the collection. The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
does not yield this lock.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Drop Side Write Table
</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">
mongod
</code>
applies any remaining
operations in the side writes table before dropping it.
</p>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
encounters a key generation error
while processing a key in the side write table, it stores that key
in the constraint violation table for later processing.
</p>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
encounters any other error while
processing a key, the build fails with an error.
</p>
<p class="leafygreen-ui-1kp3ins">
At this point, the index includes all data written to
the collection.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Process Constraint Violation Table
</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">
mongod
</code>
is the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
, it drains the
constraint violation table using first-in-first-out priority.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If no keys in the constraint violation table produce a key
generation error
<em>
or
</em>
if the table is empty, the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
drops the table and creates a
"commitIndexBuild" oplog entry. Secondaries can complete the
associated index build after replicating the oplog entry.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If any key in the constraint violation table still produces a
key generation error, the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
aborts the build
and throws an error. The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
creates an
associated "abortIndexBuild" oplog entry to indicate that
secondaries should abort and discard the index build job.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
is a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary">
secondary
</a>
, it drops
the constraint violation table. Since the primary
<em>
must
</em>
successfully drain the constraint violation table prior to
creating the "commitOplogEntry" oplog entry, the secondary
can safely assume that no violations exist.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Mark the Index as Ready
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-index-build-process-ready">
</span>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
updates the index metadata to
mark the index as ready for use.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Lock
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
releases the
<code class="leafygreen-ui-1l06pbn">
X
</code>
lock on the
collection.
</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/faq/concurrency/">
FAQ: Concurrency
</a>
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$lookup (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-lookup--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="-lookup--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-x9koo9" href="#equality-match-with-a-single-join-condition" target="_self">
<span>
Equality Match with a Single Join Condition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#join-conditions-and-subqueries-on-a-joined-collection" target="_self">
<span>
Join Conditions and Subqueries on a Joined Collection
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#correlated-subqueries-using-concise-syntax" target="_self">
<span>
Correlated Subqueries Using Concise 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-x9koo9" href="#views-and-collation" target="_self">
<span>
Views and Collation
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#restrictions" target="_self">
<span>
Restrictions
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#atlas-search-support" target="_self">
<span>
Atlas Search Support
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#sharded-collections" target="_self">
<span>
Sharded Collections
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#sbe-title" target="_self">
<span>
Slot-Based Query Execution Engine
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#performance-considerations" target="_self">
<span>
Performance 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-x9koo9" href="#perform-a-single-equality-join-with--lookup" target="_self">
<span>
Perform a Single Equality Join with
<code class="css-1kekw28 e1wawog0">
$lookup
</code>
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#use--lookup-with-an-array" target="_self">
<span>
Use
<code class="css-1kekw28 e1wawog0">
$lookup
</code>
with an Array
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#use--lookup-with--mergeobjects" target="_self">
<span>
Use
<code class="css-1kekw28 e1wawog0">
$lookup
</code>
with
<code class="css-1kekw28 e1wawog0">
$mergeObjects
</code>
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#use-multiple-join-conditions-and-a-correlated-subquery" target="_self">
<span>
Use Multiple Join Conditions and a Correlated Subquery
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#perform-an-uncorrelated-subquery-with--lookup" target="_self">
<span>
Perform an Uncorrelated Subquery with
<code class="css-1kekw28 e1wawog0">
$lookup
</code>
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#perform-a-concise-correlated-subquery-with--lookup" target="_self">
<span>
Perform a Concise Correlated Subquery with
<code class="css-1kekw28 e1wawog0">
$lookup
</code>
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#namespaces-in-subpipelines" target="_self">
<span>
Namespaces in Subpipelines
</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">
$lookup
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-pipeline-pipe.-lookup" 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.-lookup">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
8.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Performs a left outer join to a collection in the
<em>
same
</em>
database to
filter in documents from the "joined" collection for processing. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage adds a new array field to each input
document. The new array field contains the matching documents from
the "joined" collection. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage passes these
reshaped documents to the next stage.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.1, you can use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
with sharded
collections.
</p>
<p class="leafygreen-ui-1kp3ins">
To combine elements from two different collections, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unionWith/#mongodb-pipeline-pipe.-unionWith">
<code class="leafygreen-ui-1l06pbn">
$unionWith
</code>
</a>
pipeline stage.
</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">
$lookup
</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-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage has the syntax variations shown in the
following sections.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-single-equality">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Equality Match with a Single Join Condition
<a class="headerlink leafygreen-ui-1kepx47" href="#equality-match-with-a-single-join-condition" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="equality-match-with-a-single-join-condition">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To perform an equality match between a field from the input documents
with a field from the documents of the "joined" collection, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage has this syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $lookup:\n {\n from: <collection>,\n localField: <field>,\n foreignField: <field>,\n as: <output>\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">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> $lookup:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> from: <collection to join>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> localField: <field from the input documents>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> foreignField: <field from the documents of the "from" collection>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> as: <output array field></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-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</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: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-lookup-eq-from" target="_self">
<span>
from
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-eq-from">
</span>
<p class="leafygreen-ui-1kp3ins">
Specifies the collection in the
<em>
same
</em>
database to perform the
join with.
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
from
</code>
is optional, you can use a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/documents/#mongodb-pipeline-pipe.-documents">
<code class="leafygreen-ui-1l06pbn">
$documents
</code>
</a>
stage in a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage instead. For an example, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/documents/#std-label-documents-lookup-example">
Use a
<code class="leafygreen-ui-1l06pbn">
$documents
</code>
Stage in a
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
Stage
<!-- -->
.
</a>
</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="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-eq-localField" target="_self">
<span>
localField
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-eq-localField">
</span>
<p class="leafygreen-ui-1kp3ins">
Specifies the field from the documents input to the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
performs an
equality match on the
<code class="leafygreen-ui-1l06pbn">
localField
</code>
to the
<code class="leafygreen-ui-1l06pbn">
foreignField
</code>
from the documents of the
<code class="leafygreen-ui-1l06pbn">
from
</code>
collection. If an input document does not contain the
<code class="leafygreen-ui-1l06pbn">
localField
</code>
, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
treats the
field as having a value of
<code class="leafygreen-ui-1l06pbn">
null
</code>
for matching purposes.
</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-lookup-eq-foreignField" target="_self">
<span>
foreignField
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-eq-foreignField">
</span>
<p class="leafygreen-ui-1kp3ins">
Specifies the field from the documents in the
<code class="leafygreen-ui-1l06pbn">
from
</code>
collection.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
performs an equality match on
the
<code class="leafygreen-ui-1l06pbn">
foreignField
</code>
to the
<code class="leafygreen-ui-1l06pbn">
localField
</code>
from the input
documents. If a document in the
<code class="leafygreen-ui-1l06pbn">
from
</code>
collection does not
contain the
<code class="leafygreen-ui-1l06pbn">
foreignField
</code>
, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
treats
the value as
<code class="leafygreen-ui-1l06pbn">
null
</code>
for matching purposes.
</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-lookup-eq-as" target="_self">
<span>
as
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-eq-as">
</span>
<p class="leafygreen-ui-1kp3ins">
Specifies the name of the new array field to add to the input
documents. The new array field contains the matching
documents from the
<code class="leafygreen-ui-1l06pbn">
from
</code>
collection. If the specified name
already exists in the input document, the existing field is
<em>
overwritten
</em>
.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation corresponds to this pseudo-SQL statement:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"SELECT *, (\n SELECT ARRAY_AGG(*)\n FROM <collection>\n WHERE <foreignfield> = <collection.localfield>\n) AS <output>\nFROM collection;","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> <span class="lg-highlight-operator">*</span>, (</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">SELECT</span> <span class="lg-highlight-built_in">ARRAY_AGG</span>(<span class="lg-highlight-operator">*</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">FROM</span> <span class="lg-highlight-operator"><</span>collection <span class="lg-highlight-keyword">to</span> <span class="lg-highlight-keyword">join</span><span class="lg-highlight-operator">></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">WHERE</span> <span class="lg-highlight-operator"><</span>foreignField<span class="lg-highlight-operator">></span> <span class="lg-highlight-operator">=</span> <span class="lg-highlight-operator"><</span>collection.localField<span class="lg-highlight-operator">></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">) <span class="lg-highlight-keyword">AS</span> <span class="lg-highlight-operator"><</span>output <span class="lg-highlight-keyword">array</span> field<span class="lg-highlight-operator">></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> collection;</td></tr></tbody></table></code></pre>
</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 SQL statements on this page are included for comparison to the
MongoDB aggregation pipeline syntax. The SQL statements aren't
runnable.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For MongoDB examples, see these pages:
</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="#std-label-lookup-single-equality-example" target="_self">
<span>
Perform a Single Equality Join with
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-unwind-example" target="_self">
<span>
Use
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
with an Array
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-mergeObjects" target="_self">
<span>
Use
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
with
<code class="leafygreen-ui-1l06pbn">
$mergeObjects
</code>
</span>
</a>
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-syntax-let-pipeline">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Join Conditions and Subqueries on a Joined Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#join-conditions-and-subqueries-on-a-joined-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="join-conditions-and-subqueries-on-a-joined-collection">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB supports:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Executing a pipeline on a joined collection.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Multiple join conditions.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Correlated and uncorrelated subqueries.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
In MongoDB, a correlated subquery is a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-pipeline" target="_self">
<span>
pipeline
</span>
</a>
in a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage that references
document fields from a joined collection. An uncorrelated subquery does
not reference joined 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">
Starting in MongoDB 5.0, for an uncorrelated subquery in a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
pipeline stage containing a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sample/#mongodb-pipeline-pipe.-sample">
<code class="leafygreen-ui-1l06pbn">
$sample
</code>
</a>
stage, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sampleRate/#mongodb-expression-exp.-sampleRate">
<code class="leafygreen-ui-1l06pbn">
$sampleRate
</code>
</a>
operator, or the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/rand/#mongodb-expression-exp.-rand">
<code class="leafygreen-ui-1l06pbn">
$rand
</code>
</a>
operator, the subquery is always run again if
repeated. Previously, depending on the subquery output size, either the
subquery output was cached or the subquery was run again.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
MongoDB correlated subqueries are comparable to SQL correlated
subqueries, where the inner query references outer query values. An
SQL uncorrelated subquery does not reference outer query values.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB 5.0 also supports
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-syntax-concise-correlated-subquery" target="_self">
<span>
concise correlated subqueries
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To perform correlated and uncorrelated subqueries with two collections,
and perform other join conditions besides a single equality match, use
this
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $lookup:\n {\n from: <joined>,\n let: { <var_1>: <expression>, …, <var_n>: <expression> },\n pipeline: [ <pipeline> ],\n as: <output>\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">$lookup</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <!-- --><joined collection>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> let: { <var_1>: <expression>, …, <var_n>: <expression> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> pipeline: [ <pipeline to run on joined collection> ],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> as: <output array field></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
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage accepts a document with these 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>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-from" target="_self">
<span>
from
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-join-from">
</span>
<p class="leafygreen-ui-1kp3ins">
Specifies the collection in the
<em>
same
</em>
database to perform the
join operation.
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
from
</code>
is optional, you can use a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/documents/#mongodb-pipeline-pipe.-documents">
<code class="leafygreen-ui-1l06pbn">
$documents
</code>
</a>
stage in a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage instead. For an example, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/documents/#std-label-documents-lookup-example">
Use a
<code class="leafygreen-ui-1l06pbn">
$documents
</code>
Stage in a
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
Stage
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.1, the
<code class="leafygreen-ui-1l06pbn">
from
</code>
collection can be sharded.
</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-lookup-join-let" target="_self">
<span>
let
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-join-let">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. Specifies variables to use in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-pipeline" target="_self">
<span>
pipeline
</span>
</a>
stages. Use the variable expressions to
access the fields from the joined collection's documents that are
input to the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
To reference variables in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-pipeline" target="_self">
<span>
pipeline
</span>
</a>
stages, use the
<code class="leafygreen-ui-1l06pbn">
"$$<variable>"
</code>
syntax.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-let" target="_self">
<span>
let
</span>
</a>
variables can be accessed by the
stages in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-pipeline" target="_self">
<span>
pipeline
</span>
</a>
, including
additional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stages nested in the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A
<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 requires the use of an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator to access the variables. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator allows the use of aggregation expressions inside of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
syntax.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/eq/#mongodb-expression-exp.-eq">
<code class="leafygreen-ui-1l06pbn">
$eq
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lt/#mongodb-expression-exp.-lt">
<code class="leafygreen-ui-1l06pbn">
$lt
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lte/#mongodb-expression-exp.-lte">
<code class="leafygreen-ui-1l06pbn">
$lte
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/gt/#mongodb-expression-exp.-gt">
<code class="leafygreen-ui-1l06pbn">
$gt
</code>
</a>
, and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/gte/#mongodb-expression-exp.-gte">
<code class="leafygreen-ui-1l06pbn">
$gte
</code>
</a>
comparison operators placed in
an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator can use an index on the
<code class="leafygreen-ui-1l06pbn">
from
</code>
collection
referenced in a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage. Limitations:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Indexes can only be used for comparisons between fields and constants, so the
<code class="leafygreen-ui-1l06pbn">
let
</code>
operand must resolve to a constant.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, a comparison between
<code class="leafygreen-ui-1l06pbn">
$a
</code>
and a constant
value can use an index, but a comparison between
<code class="leafygreen-ui-1l06pbn">
$a
</code>
and
<code class="leafygreen-ui-1l06pbn">
$b
</code>
cannot.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Indexes are not used for comparisons where the
<code class="leafygreen-ui-1l06pbn">
let
</code>
operand resolves to an
empty or missing value.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-multikey/#std-label-index-type-multikey">
Multikey indexes
</a>
are not used.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Other (non-
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
) stages in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-pipeline" target="_self">
<span>
pipeline
</span>
</a>
do not
require an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator to access the variables.
</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-lookup-join-pipeline" target="_self">
<span>
pipeline
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-join-pipeline">
</span>
<p class="leafygreen-ui-1kp3ins">
Specifies the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
to run on the joined collection. The
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
determines the resulting documents from the joined
collection. To return all documents, specify an empty
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
<code class="leafygreen-ui-1l06pbn">
[]
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
cannot include 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 or the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</a>
stage. Starting in v6.0, the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
can contain the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/" target="_self">
<span>
Atlas Search
</span>
</a>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/search/#mongodb-pipeline-pipe.-search" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$search
</code>
</span>
</a>
stage as the first stage inside the
pipeline. To learn more, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-fts-lookup-behavior" target="_self">
<span>
Atlas Search Support
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
cannot directly access the joined document
fields. Instead, define variables for the joined document fields
using the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-let" target="_self">
<span>
let
</span>
</a>
option and then reference
the variables in the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
stages.
</p>
<p class="leafygreen-ui-1kp3ins">
To reference variables in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-pipeline" target="_self">
<span>
pipeline
</span>
</a>
stages, use the
<code class="leafygreen-ui-1l06pbn">
"$$<variable>"
</code>
syntax.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-let" target="_self">
<span>
let
</span>
</a>
variables can be accessed by the
stages in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-pipeline" target="_self">
<span>
pipeline
</span>
</a>
, including
additional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stages nested in the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A
<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 requires the use of an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator to access the variables. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator allows the use of aggregation expressions inside of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
syntax.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/eq/#mongodb-expression-exp.-eq">
<code class="leafygreen-ui-1l06pbn">
$eq
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lt/#mongodb-expression-exp.-lt">
<code class="leafygreen-ui-1l06pbn">
$lt
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lte/#mongodb-expression-exp.-lte">
<code class="leafygreen-ui-1l06pbn">
$lte
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/gt/#mongodb-expression-exp.-gt">
<code class="leafygreen-ui-1l06pbn">
$gt
</code>
</a>
, and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/gte/#mongodb-expression-exp.-gte">
<code class="leafygreen-ui-1l06pbn">
$gte
</code>
</a>
comparison operators placed in
an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator can use an index on the
<code class="leafygreen-ui-1l06pbn">
from
</code>
collection
referenced in a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage. Limitations:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Indexes can only be used for comparisons between fields and constants, so the
<code class="leafygreen-ui-1l06pbn">
let
</code>
operand must resolve to a constant.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, a comparison between
<code class="leafygreen-ui-1l06pbn">
$a
</code>
and a constant
value can use an index, but a comparison between
<code class="leafygreen-ui-1l06pbn">
$a
</code>
and
<code class="leafygreen-ui-1l06pbn">
$b
</code>
cannot.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Indexes are not used for comparisons where the
<code class="leafygreen-ui-1l06pbn">
let
</code>
operand resolves to an
empty or missing value.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-multikey/#std-label-index-type-multikey">
Multikey indexes
</a>
are not used.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Other (non-
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
) stages in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-pipeline" target="_self">
<span>
pipeline
</span>
</a>
do not
require an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator to access the variables.
</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-lookup-join-as" target="_self">
<span>
as
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-join-as">
</span>
<p class="leafygreen-ui-1kp3ins">
Specifies the name of the new array field to add to the joined
documents. The new array field contains the matching documents
from the joined collection. If the specified name already exists
in the joined document, the existing field is overwritten.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation corresponds to this pseudo-SQL statement:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"SELECT *, <output>\nFROM collection\nWHERE <output> IN (\n SELECT <documents>\n FROM <collection>\n WHERE <pipeline>\n);","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> <span class="lg-highlight-operator">*</span>, <span class="lg-highlight-operator"><</span>output <span class="lg-highlight-keyword">array</span> field<span class="lg-highlight-operator">></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> collection</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> <span class="lg-highlight-operator"><</span>output <span class="lg-highlight-keyword">array</span> field<span class="lg-highlight-operator">></span> <span class="lg-highlight-keyword">IN</span> (</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">SELECT</span> <span class="lg-highlight-operator"><</span>documents <span class="lg-highlight-keyword">as</span> determined <span class="lg-highlight-keyword">from</span> the pipeline<span class="lg-highlight-operator">></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">FROM</span> <span class="lg-highlight-operator"><</span>collection <span class="lg-highlight-keyword">to</span> <span class="lg-highlight-keyword">join</span><span class="lg-highlight-operator">></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">WHERE</span> <span class="lg-highlight-operator"><</span>pipeline<span class="lg-highlight-operator">></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">);</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
See the following examples:
</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="#std-label-lookup-multiple-joins" target="_self">
<span>
Use Multiple Join Conditions and a Correlated Subquery
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-uncorrelated-subquery" target="_self">
<span>
Perform an Uncorrelated Subquery with
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-syntax-concise-correlated-subquery">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Correlated Subqueries Using Concise Syntax
<a class="headerlink leafygreen-ui-1kepx47" href="#correlated-subqueries-using-concise-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="correlated-subqueries-using-concise-syntax">
</div>
</a>
</h3>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.0, you can use a concise syntax for a correlated
subquery. Correlated subqueries reference document fields from a joined
"foreign" collection
<em>
and
</em>
the "local" collection on which 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>
method was run.
</p>
<p class="leafygreen-ui-1kp3ins">
The following new concise syntax removes the requirement for an equality
match on the foreign and local fields inside of an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $lookup:\n {\n from: <foreign>,\n localField: <field>,\n foreignField: <field>,\n let: { <var_1>: <expression>, …, <var_n>: <expression> },\n pipeline: [ <pipeline> ],\n as: <output>\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">$lookup</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <!-- --><foreign collection>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> localField: <field from local collection's documents>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> foreignField: <field from foreign collection's documents>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> let: { <var_1>: <expression>, …, <var_n>: <expression> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> pipeline: [ <pipeline to run> ],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> as: <output array field></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
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
accepts a document with these 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>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-concise-correlated-subquery-from" target="_self">
<span>
from
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-concise-correlated-subquery-from">
</span>
<p class="leafygreen-ui-1kp3ins">
Specifies the foreign collection in the
<em>
same
</em>
database to join
to the local collection.
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
from
</code>
is optional, you can use a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/documents/#mongodb-pipeline-pipe.-documents">
<code class="leafygreen-ui-1l06pbn">
$documents
</code>
</a>
stage in a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage instead. For an example, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/documents/#std-label-documents-lookup-example">
Use a
<code class="leafygreen-ui-1l06pbn">
$documents
</code>
Stage in a
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
Stage
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.1, the
<code class="leafygreen-ui-1l06pbn">
from
</code>
collection can be sharded.
</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="#std-label-lookup-concise-correlated-subquery-localField" target="_self">
<span>
localField
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-concise-correlated-subquery-localField">
</span>
<p class="leafygreen-ui-1kp3ins">
Specifies the local documents'
<code class="leafygreen-ui-1l06pbn">
localField
</code>
to perform an
equality match with the foreign documents'
<code class="leafygreen-ui-1l06pbn">
foreignField
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If a local document does not contain a
<code class="leafygreen-ui-1l06pbn">
localField
</code>
value, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
uses a
<code class="leafygreen-ui-1l06pbn">
null
</code>
value for the match.
</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="#std-label-lookup-concise-correlated-subquery-foreignField" target="_self">
<span>
foreignField
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-concise-correlated-subquery-foreignField">
</span>
<p class="leafygreen-ui-1kp3ins">
Specifies the foreign documents'
<code class="leafygreen-ui-1l06pbn">
foreignField
</code>
to perform an
equality match with the local documents'
<code class="leafygreen-ui-1l06pbn">
localField
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If a foreign document does not contain a
<code class="leafygreen-ui-1l06pbn">
foreignField
</code>
value,
the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
uses a
<code class="leafygreen-ui-1l06pbn">
null
</code>
value for the match.
</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="#std-label-lookup-concise-correlated-subquery-let" target="_self">
<span>
let
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-concise-correlated-subquery-let">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. Specifies the variables to use in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-concise-correlated-subquery-pipeline" target="_self">
<span>
pipeline
</span>
</a>
stages. Use the
variable expressions to access the document fields that are input
to the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
To reference variables in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-pipeline" target="_self">
<span>
pipeline
</span>
</a>
stages, use the
<code class="leafygreen-ui-1l06pbn">
"$$<variable>"
</code>
syntax.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-let" target="_self">
<span>
let
</span>
</a>
variables can be accessed by the
stages in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-pipeline" target="_self">
<span>
pipeline
</span>
</a>
, including
additional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stages nested in the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A
<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 requires the use of an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator to access the variables. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator allows the use of aggregation expressions inside of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
syntax.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/eq/#mongodb-expression-exp.-eq">
<code class="leafygreen-ui-1l06pbn">
$eq
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lt/#mongodb-expression-exp.-lt">
<code class="leafygreen-ui-1l06pbn">
$lt
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lte/#mongodb-expression-exp.-lte">
<code class="leafygreen-ui-1l06pbn">
$lte
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/gt/#mongodb-expression-exp.-gt">
<code class="leafygreen-ui-1l06pbn">
$gt
</code>
</a>
, and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/gte/#mongodb-expression-exp.-gte">
<code class="leafygreen-ui-1l06pbn">
$gte
</code>
</a>
comparison operators placed in
an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator can use an index on the
<code class="leafygreen-ui-1l06pbn">
from
</code>
collection
referenced in a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage. Limitations:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Indexes can only be used for comparisons between fields and constants, so the
<code class="leafygreen-ui-1l06pbn">
let
</code>
operand must resolve to a constant.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, a comparison between
<code class="leafygreen-ui-1l06pbn">
$a
</code>
and a constant
value can use an index, but a comparison between
<code class="leafygreen-ui-1l06pbn">
$a
</code>
and
<code class="leafygreen-ui-1l06pbn">
$b
</code>
cannot.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Indexes are not used for comparisons where the
<code class="leafygreen-ui-1l06pbn">
let
</code>
operand resolves to an
empty or missing value.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-multikey/#std-label-index-type-multikey">
Multikey indexes
</a>
are not used.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Other (non-
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
) stages in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-pipeline" target="_self">
<span>
pipeline
</span>
</a>
do not
require an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator to access the variables.
</p>
</li>
</ul>
</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="#std-label-lookup-concise-correlated-subquery-pipeline" target="_self">
<span>
pipeline
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-concise-correlated-subquery-pipeline">
</span>
<p class="leafygreen-ui-1kp3ins">
Specifies the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
to run on the foreign collection. The
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
returns documents from the foreign collection. To
return all documents, specify an empty
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
<code class="leafygreen-ui-1l06pbn">
[]
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
cannot include 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>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</a>
stages. Starting in v6.0, the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
can contain the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/" target="_self">
<span>
Atlas Search
</span>
</a>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/search/#mongodb-pipeline-pipe.-search" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$search
</code>
</span>
</a>
stage as the first stage inside the
pipeline. To learn more, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-fts-lookup-behavior" target="_self">
<span>
Atlas Search Support
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
cannot directly access the document fields.
Instead, define variables for the document fields using the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-concise-correlated-subquery-let" target="_self">
<span>
let
</span>
</a>
option and
then reference the variables in the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
stages.
</p>
<p class="leafygreen-ui-1kp3ins">
To reference variables in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-pipeline" target="_self">
<span>
pipeline
</span>
</a>
stages, use the
<code class="leafygreen-ui-1l06pbn">
"$$<variable>"
</code>
syntax.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-let" target="_self">
<span>
let
</span>
</a>
variables can be accessed by the
stages in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-pipeline" target="_self">
<span>
pipeline
</span>
</a>
, including
additional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stages nested in the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A
<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 requires the use of an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator to access the variables. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator allows the use of aggregation expressions inside of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
syntax.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/eq/#mongodb-expression-exp.-eq">
<code class="leafygreen-ui-1l06pbn">
$eq
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lt/#mongodb-expression-exp.-lt">
<code class="leafygreen-ui-1l06pbn">
$lt
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lte/#mongodb-expression-exp.-lte">
<code class="leafygreen-ui-1l06pbn">
$lte
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/gt/#mongodb-expression-exp.-gt">
<code class="leafygreen-ui-1l06pbn">
$gt
</code>
</a>
, and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/gte/#mongodb-expression-exp.-gte">
<code class="leafygreen-ui-1l06pbn">
$gte
</code>
</a>
comparison operators placed in
an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator can use an index on the
<code class="leafygreen-ui-1l06pbn">
from
</code>
collection
referenced in a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage. Limitations:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Indexes can only be used for comparisons between fields and constants, so the
<code class="leafygreen-ui-1l06pbn">
let
</code>
operand must resolve to a constant.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, a comparison between
<code class="leafygreen-ui-1l06pbn">
$a
</code>
and a constant
value can use an index, but a comparison between
<code class="leafygreen-ui-1l06pbn">
$a
</code>
and
<code class="leafygreen-ui-1l06pbn">
$b
</code>
cannot.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Indexes are not used for comparisons where the
<code class="leafygreen-ui-1l06pbn">
let
</code>
operand resolves to an
empty or missing value.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-multikey/#std-label-index-type-multikey">
Multikey indexes
</a>
are not used.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Other (non-
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
) stages in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-join-pipeline" target="_self">
<span>
pipeline
</span>
</a>
do not
require an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator to access the variables.
</p>
</li>
</ul>
</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="#std-label-lookup-concise-correlated-subquery-as" target="_self">
<span>
as
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-concise-correlated-subquery-as">
</span>
<p class="leafygreen-ui-1kp3ins">
Specifies the name of the new array field to add to the foreign
documents. The new array field contains the matching documents
from the foreign collection. If the specified name already exists
in the foreign document, the existing field is overwritten.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The operation corresponds to this pseudo-SQL statement:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"SELECT *, <output>\nFROM localCollection\nWHERE <output> IN (\n SELECT <documents>\n FROM <foreigncollection>\n WHERE <foreigncollection.foreignfield> = <localcollection.localfield>\n AND <pipeline>\n);","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> <span class="lg-highlight-operator">*</span>, <span class="lg-highlight-operator"><</span>output <span class="lg-highlight-keyword">array</span> field<span class="lg-highlight-operator">></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> localCollection</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> <span class="lg-highlight-operator"><</span>output <span class="lg-highlight-keyword">array</span> field<span class="lg-highlight-operator">></span> <span class="lg-highlight-keyword">IN</span> (</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">SELECT</span> <span class="lg-highlight-operator"><</span>documents <span class="lg-highlight-keyword">as</span> determined <span class="lg-highlight-keyword">from</span> the pipeline<span class="lg-highlight-operator">></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">FROM</span> <span class="lg-highlight-operator"><</span>foreignCollection<span class="lg-highlight-operator">></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">WHERE</span> <span class="lg-highlight-operator"><</span>foreignCollection.foreignField<span class="lg-highlight-operator">></span> <span class="lg-highlight-operator">=</span> <span class="lg-highlight-operator"><</span>localCollection.localField<span class="lg-highlight-operator">></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">AND</span> <span class="lg-highlight-operator"><</span>pipeline <span class="lg-highlight-keyword">match</span> <span class="lg-highlight-keyword">condition</span><span class="lg-highlight-operator">></span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">);</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
See this example:
</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="#std-label-lookup-concise-correlated-subquery" target="_self">
<span>
Perform a Concise Correlated Subquery with
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
</p>
</li>
</ul>
</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">
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="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</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>
</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 include 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>
or the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</a>
stage in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage. That is, when specifying a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-syntax-let-pipeline" target="_self">
<span>
pipeline for the joined collection
</span>
</a>
, you cannot include either stage in
the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
field.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $lookup:\n {\n from: <collection>,\n let: { <var_1>: <expression>, …, <var_n>: <expression> },\n pipeline: [ <pipeline> ], // Cannot include $out or $merge\n as: <output>\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">$lookup</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <!-- --><collection to join>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> let: { <var_1>: <expression>, …, <var_n>: <expression> },</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> pipeline: [ <pipeline to execute on the joined collection> ], // Cannot include $out or $merge</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> as: <output array field></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-fts-lookup-behavior">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Atlas Search Support
<a class="headerlink leafygreen-ui-1kepx47" href="#atlas-search-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="atlas-search-support">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0, you can specify the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/" target="_self">
<span>
Atlas Search
</span>
</a>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/search/#mongodb-pipeline-pipe.-search" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$search
</code>
</span>
</a>
or
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/searchMeta/#mongodb-pipeline-pipe.-searchMeta" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$searchMeta
</code>
</span>
</a>
stage
in the
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
pipeline to search collections on the Atlas
cluster. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/search/#mongodb-pipeline-pipe.-search" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$search
</code>
</span>
</a>
or the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/searchMeta/#mongodb-pipeline-pipe.-searchMeta" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$searchMeta
</code>
</span>
</a>
stage
must be the first stage inside the
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
pipeline.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, when you
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-syntax-let-pipeline" target="_self">
<span>
Join Conditions and Subqueries on a Joined Collection
</span>
</a>
or run
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-syntax-concise-correlated-subquery" target="_self">
<span>
Correlated Subqueries Using Concise Syntax
</span>
</a>
, you can specify
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/search/#mongodb-pipeline-pipe.-search" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$search
</code>
</span>
</a>
or
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/searchMeta/#mongodb-pipeline-pipe.-searchMeta" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$searchMeta
</code>
</span>
</a>
inside the pipeline as
shown below:
</p>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-15">
<div aria-label="Tabs to describe usage of search/searchmeta" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_ysvptmomr" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_fm3jy4964" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
$search
</div>
</button>
<button aria-controls="_713frdz9w" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_2a5qidnqu" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
$searchMeta
</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="_gi5gx1uzl" class="" id="_sp175jrag" 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 \"$lookup\": {\n \"from\": <joined>,\n localField: <field>,\n foreignField: <field>,\n \"as\": <output>,\n \"pipeline\": [{\n \"$search\": {\n \"<operator>\": {\n <operator-specification>\n }\n },\n ...\n }]\n }\n}]"}
</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"> "$lookup": {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "from": <joined collection>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> localField: <field from the input documents>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> foreignField: <field from the documents of the "from" collection>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "as": <output array field>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> "pipeline": [{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> "$search": {</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> "<operator>": {</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <operator-specification></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><tr 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>
</div>
<div>
<div aria-labelledby="_rhubhqa4i" class="leafygreen-ui-38lglc" id="_hvrrqvbed" 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 \"$lookup\": {\n \"from\": <joined>,\n localField: <field>,\n foreignField: <field>,\n \"as\": <output>,\n \"pipeline\": [{\n \"$searchMeta\": {\n \"<collector>\": {\n <collector-specification>\n }\n },\n ...\n }]\n }\n}]"}
</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"> "$lookup": {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "from": <joined collection>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> localField: <field from the input documents>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> foreignField: <field from the documents of the "from" collection>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "as": <output array field>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> "pipeline": [{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> "$searchMeta": {</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> "<collector>": {</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <collector-specification></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><tr 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>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To see an example of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
with
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/search/#mongodb-pipeline-pipe.-search" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$search
</code>
</span>
</a>
,
see the Atlas Search tutorial
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/tutorial/lookup-with-search/" target="_self">
<span>
Run an Atlas Search $search Query
Using $lookup
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-sharded-collections">
</span>
</section>
<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.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stages.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, you can use the
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
stage within a
transaction while targeting a sharded collection.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Slot-Based Query Execution Engine
<a class="headerlink leafygreen-ui-1kepx47" href="#sbe-title" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sbe-title">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in version 6.0, MongoDB can use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/sbe/#std-label-sbe-landing">
slot-based execution
query engine
</a>
to execute
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stages
if
<em>
all
</em>
preceding stages in the pipeline can also be executed by the
<!-- -->
slot-based execution engine
<!-- -->
and none of the following conditions are true:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
operation executes a pipeline on a joined collection.
To see an example of this kind of operation, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-syntax-let-pipeline" target="_self">
<span>
Join Conditions and Subqueries on a Joined Collection
<!-- -->
.
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
's
<code class="leafygreen-ui-1l06pbn">
localField
</code>
or
<code class="leafygreen-ui-1l06pbn">
foreignField
</code>
specify numeric
components. For example:
<code class="leafygreen-ui-1l06pbn">
{ localField: "restaurant.0.review" }
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
from
</code>
field of any
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
in the pipeline specifies a view
or sharded collection.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For more information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline-optimization/#std-label-agg-lookup-optimization-sbe">
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
Optimization
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-performance-considerations">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Performance Considerations
<a class="headerlink leafygreen-ui-1kepx47" href="#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="performance-considerations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
performance depends on the type of operation performed.
Refer to the following table for performance considerations for
different
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
operations.
</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>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
Operation
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Performance Considerations
</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-lookup-single-equality-example" target="_self">
<span>
Equality Match with a Single Join
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-equality-match-performance">
</span>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
operations that perform equality matches with a
single join perform better when the foreign collection contains
an index on the
<code class="leafygreen-ui-1l06pbn">
foreignField
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
<strong class="css-rws738">
IMPORTANT:
</strong>
If a supporting index on the
<code class="leafygreen-ui-1l06pbn">
foreignField
</code>
does
not exist, a
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
operation that performs an equality match with
a single join will likely have poor performance.
</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-lookup-uncorrelated-subquery" target="_self">
<span>
Uncorrelated Subqueries
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-uncorrelated-subqueries-performance">
</span>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
operations that contain uncorrelated subqueries
perform better when the inner pipeline can reference an
index of the foreign collection.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
MongoDB only needs to run the
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
subquery once before
caching the query because there is no relationship between the
source and foreign collections. The subquery is not based on
any value in the source collection. This behavior improves
performance for subsequent executions of the
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
operation.
</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-lookup-concise-correlated-subquery" target="_self">
<span>
Correlated Subqueries
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-correlated-subqueries-performance">
</span>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
operations that contain correlated subqueries
perform better when the following conditions apply:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The foreign collection contains an index on the
<code class="leafygreen-ui-1l06pbn">
foreignField
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The foreign collection contains an index that references the
inner pipline.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If your pipeline passes a large number of documents to the
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
query, the following strategies may improve
performance:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Reduce the number of documents that MongoDB passes to the
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
query. For example, set a stricter filter
during the
<code class="leafygreen-ui-1l06pbn">
$match
</code>
stage.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Run the inner pipeline of the
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
subquery as a
separate query and use
<code class="leafygreen-ui-1l06pbn">
$out
</code>
to create a temporary
collection. Then, run an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-single-equality" target="_self">
<span>
equality match with a single
join
<!-- -->
.
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Reconsider the data's schema to ensure it is optimal for the
use case.
</p>
</li>
</ul>
</li>
</ul>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
For general performance strategies, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/applications/indexes/#std-label-manual-indexing-strategies">
Indexing Strategies
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-optimization/#std-label-read-operations-indexing">
Query Optimization
<!-- -->
.
</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">
Excessive use of
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
within a query may slow down
performance. To avoid multiple
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
stages, consider an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/data-modeling/concepts/embedding-vs-references/#std-label-data-modeling-embedding">
embedded data model
</a>
to optimize
query performance.
</p>
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-single-equality-example">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Perform a Single Equality Join with
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#perform-a-single-equality-join-with--lookup" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-single-equality-join-with--lookup">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Create a collection
<code class="leafygreen-ui-1l06pbn">
orders
</code>
with these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.insertMany( [\n { \"_id\" : 1, \"item\" : \"almonds\", \"price\" : 12, \"quantity\" : 2 },\n { \"_id\" : 2, \"item\" : \"pecans\", \"price\" : 20, \"quantity\" : 1 },\n { \"_id\" : 3 }\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">orders</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">"almonds"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">12</span>, <span class="lg-highlight-string">"quantity"</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>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"pecans"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">20</span>, <span class="lg-highlight-string">"quantity"</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">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">
Create another collection
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
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, \"sku\" : \"almonds\", \"description\": \"product 1\", \"instock\" : 120 },\n { \"_id\" : 2, \"sku\" : \"bread\", \"description\": \"product 2\", \"instock\" : 80 },\n { \"_id\" : 3, \"sku\" : \"cashews\", \"description\": \"product 3\", \"instock\" : 60 },\n { \"_id\" : 4, \"sku\" : \"pecans\", \"description\": \"product 4\", \"instock\" : 70 },\n { \"_id\" : 5, \"sku\": null, \"description\": \"Incomplete\" },\n { \"_id\" : 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">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">"sku"</span> <!-- -->: <span class="lg-highlight-string">"almonds"</span>, <span class="lg-highlight-string">"description"</span>: <span class="lg-highlight-string">"product 1"</span>, <span class="lg-highlight-string">"instock"</span> <!-- -->: <span class="lg-highlight-number">120</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">"sku"</span> <!-- -->: <span class="lg-highlight-string">"bread"</span>, <span class="lg-highlight-string">"description"</span>: <span class="lg-highlight-string">"product 2"</span>, <span class="lg-highlight-string">"instock"</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">3</span>, <span class="lg-highlight-string">"sku"</span> <!-- -->: <span class="lg-highlight-string">"cashews"</span>, <span class="lg-highlight-string">"description"</span>: <span class="lg-highlight-string">"product 3"</span>, <span class="lg-highlight-string">"instock"</span> <!-- -->: <span class="lg-highlight-number">60</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">"sku"</span> <!-- -->: <span class="lg-highlight-string">"pecans"</span>, <span class="lg-highlight-string">"description"</span>: <span class="lg-highlight-string">"product 4"</span>, <span class="lg-highlight-string">"instock"</span> <!-- -->: <span class="lg-highlight-number">70</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">"sku"</span>: <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-string">"description"</span>: <span class="lg-highlight-string">"Incomplete"</span> }<!-- -->,</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">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 on the
<code class="leafygreen-ui-1l06pbn">
orders
</code>
collection
joins the documents from
<code class="leafygreen-ui-1l06pbn">
orders
</code>
with the documents from the
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection using the fields
<code class="leafygreen-ui-1l06pbn">
item
</code>
from the
<code class="leafygreen-ui-1l06pbn">
orders
</code>
collection and the
<code class="leafygreen-ui-1l06pbn">
sku
</code>
field from 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.orders.aggregate( [\n {\n $lookup:\n {\n from: \"inventory\",\n localField: \"item\",\n foreignField: \"sku\",\n as: \"inventory_docs\"\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">orders</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">$lookup</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <span class="lg-highlight-string">"inventory"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">localField</span>: <span class="lg-highlight-string">"item"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">foreignField</span>: <span class="lg-highlight-string">"sku"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">as</span>: <span class="lg-highlight-string">"inventory_docs"</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 these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : 1,\n \"item\" : \"almonds\",\n \"price\" : 12,\n \"quantity\" : 2,\n \"inventory_docs\" : [\n { \"_id\" : 1, \"sku\" : \"almonds\", \"description\" : \"product 1\", \"instock\" : 120 }\n ]\n}\n{\n \"_id\" : 2,\n \"item\" : \"pecans\",\n \"price\" : 20,\n \"quantity\" : 1,\n \"inventory_docs\" : [\n { \"_id\" : 4, \"sku\" : \"pecans\", \"description\" : \"product 4\", \"instock\" : 70 }\n ]\n}\n{\n \"_id\" : 3,\n \"inventory_docs\" : [\n { \"_id\" : 5, \"sku\" : null, \"description\" : \"Incomplete\" },\n { \"_id\" : 6 }\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">"item"</span> <!-- -->: <span class="lg-highlight-string">"almonds"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">12</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"inventory_docs"</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">"sku"</span> <!-- -->: <span class="lg-highlight-string">"almonds"</span>, <span class="lg-highlight-string">"description"</span> <!-- -->: <span class="lg-highlight-string">"product 1"</span>, <span class="lg-highlight-string">"instock"</span> <!-- -->: <span class="lg-highlight-number">120</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">"item"</span> <!-- -->: <span class="lg-highlight-string">"pecans"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">20</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"inventory_docs"</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">"sku"</span> <!-- -->: <span class="lg-highlight-string">"pecans"</span>, <span class="lg-highlight-string">"description"</span> <!-- -->: <span class="lg-highlight-string">"product 4"</span>, <span class="lg-highlight-string">"instock"</span> <!-- -->: <span class="lg-highlight-number">70</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">"inventory_docs"</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">"sku"</span> <!-- -->: <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-string">"description"</span> <!-- -->: <span class="lg-highlight-string">"Incomplete"</span> }<!-- -->,</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"> ]</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 corresponds to this pseudo-SQL statement:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"SELECT *, inventory_docs\nFROM orders\nWHERE inventory_docs IN (\n SELECT *\n FROM inventory\n WHERE sku = orders.item\n);","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> <span class="lg-highlight-operator">*</span>, inventory_docs</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> orders</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> inventory_docs <span class="lg-highlight-keyword">IN</span> (</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">SELECT</span> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">FROM</span> inventory</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">WHERE</span> sku <span class="lg-highlight-operator">=</span> orders.item</td></tr><tr class=""><td class="leafygreen-ui-7razhx">);</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-equality-match-performance" target="_self">
<span>
Equality Match Performance Considerations
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-unwind-example">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Use
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
with an Array
<a class="headerlink leafygreen-ui-1kepx47" href="#use--lookup-with-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="use--lookup-with-an-array">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
localField
</code>
is an array, you can match the array elements
against a scalar
<code class="leafygreen-ui-1l06pbn">
foreignField
</code>
without an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unwind/#mongodb-pipeline-pipe.-unwind">
<code class="leafygreen-ui-1l06pbn">
$unwind
</code>
</a>
stage.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, create an example collection
<code class="leafygreen-ui-1l06pbn">
classes
</code>
with these
documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.classes.insertMany( [\n { _id: 1, title: \"Reading is ...\", enrollmentlist: [ \"giraffe2\", \"pandabear\", \"artie\" ], days: [\"M\", \"W\", \"F\"] },\n { _id: 2, title: \"But Writing ...\", enrollmentlist: [ \"giraffe1\", \"artie\" ], days: [\"T\", \"F\"] }\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">classes</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">title</span>: <span class="lg-highlight-string">"Reading is ..."</span>, <span class="lg-highlight-attr">enrollmentlist</span>: <!-- -->[ <span class="lg-highlight-string">"giraffe2"</span>, <span class="lg-highlight-string">"pandabear"</span>, <span class="lg-highlight-string">"artie"</span> ]<!-- -->, <span class="lg-highlight-attr">days</span>: <!-- -->[<span class="lg-highlight-string">"M"</span>, <span class="lg-highlight-string">"W"</span>, <span class="lg-highlight-string">"F"</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">title</span>: <span class="lg-highlight-string">"But Writing ..."</span>, <span class="lg-highlight-attr">enrollmentlist</span>: <!-- -->[ <span class="lg-highlight-string">"giraffe1"</span>, <span class="lg-highlight-string">"artie"</span> ]<!-- -->, <span class="lg-highlight-attr">days</span>: <!-- -->[<span class="lg-highlight-string">"T"</span>, <span class="lg-highlight-string">"F"</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">
Create another collection
<code class="leafygreen-ui-1l06pbn">
members
</code>
with these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.members.insertMany( [\n { _id: 1, name: \"artie\", joined: new Date(\"2016-05-01\"), status: \"A\" },\n { _id: 2, name: \"giraffe\", joined: new Date(\"2017-05-01\"), status: \"D\" },\n { _id: 3, name: \"giraffe1\", joined: new Date(\"2017-10-01\"), status: \"A\" },\n { _id: 4, name: \"panda\", joined: new Date(\"2018-10-11\"), status: \"A\" },\n { _id: 5, name: \"pandabear\", joined: new Date(\"2018-12-01\"), status: \"A\" },\n { _id: 6, name: \"giraffe2\", joined: new Date(\"2018-12-01\"), status: \"D\" }\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">members</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">"artie"</span>, <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">"2016-05-01"</span>)<!-- -->, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</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">"giraffe"</span>, <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">"2017-05-01"</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">_id</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"giraffe1"</span>, <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">"2017-10-01"</span>)<!-- -->, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</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">"panda"</span>, <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">"2018-10-11"</span>)<!-- -->, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</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">"pandabear"</span>, <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">"2018-12-01"</span>)<!-- -->, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">6</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"giraffe2"</span>, <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">"2018-12-01"</span>)<!-- -->, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"D"</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 joins documents in the
<code class="leafygreen-ui-1l06pbn">
classes
</code>
collection with the
<code class="leafygreen-ui-1l06pbn">
members
</code>
collection, matching on the
<code class="leafygreen-ui-1l06pbn">
enrollmentlist
</code>
field to 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.classes.aggregate( [\n {\n $lookup:\n {\n from: \"members\",\n localField: \"enrollmentlist\",\n foreignField: \"name\",\n as: \"enrollee_info\"\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">classes</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">$lookup</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <span class="lg-highlight-string">"members"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">localField</span>: <span class="lg-highlight-string">"enrollmentlist"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">foreignField</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">"enrollee_info"</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:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : 1,\n \"title\" : \"Reading is ...\",\n \"enrollmentlist\" : [ \"giraffe2\", \"pandabear\", \"artie\" ],\n \"days\" : [ \"M\", \"W\", \"F\" ],\n \"enrollee_info\" : [\n { \"_id\" : 1, \"name\" : \"artie\", \"joined\" : ISODate(\"2016-05-01T00:00:00Z\"), \"status\" : \"A\" },\n { \"_id\" : 5, \"name\" : \"pandabear\", \"joined\" : ISODate(\"2018-12-01T00:00:00Z\"), \"status\" : \"A\" },\n { \"_id\" : 6, \"name\" : \"giraffe2\", \"joined\" : ISODate(\"2018-12-01T00:00:00Z\"), \"status\" : \"D\" }\n ]\n}\n{\n \"_id\" : 2,\n \"title\" : \"But Writing ...\",\n \"enrollmentlist\" : [ \"giraffe1\", \"artie\" ],\n \"days\" : [ \"T\", \"F\" ],\n \"enrollee_info\" : [\n { \"_id\" : 1, \"name\" : \"artie\", \"joined\" : ISODate(\"2016-05-01T00:00:00Z\"), \"status\" : \"A\" },\n { \"_id\" : 3, \"name\" : \"giraffe1\", \"joined\" : ISODate(\"2017-10-01T00:00:00Z\"), \"status\" : \"A\" }\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">"title"</span> <!-- -->: <span class="lg-highlight-string">"Reading is ..."</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"enrollmentlist"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"giraffe2"</span>, <span class="lg-highlight-string">"pandabear"</span>, <span class="lg-highlight-string">"artie"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"days"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"M"</span>, <span class="lg-highlight-string">"W"</span>, <span class="lg-highlight-string">"F"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"enrollee_info"</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">"artie"</span>, <span class="lg-highlight-string">"joined"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2016-05-01T00:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"A"</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">"pandabear"</span>, <span class="lg-highlight-string">"joined"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2018-12-01T00:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"A"</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">"giraffe2"</span>, <span class="lg-highlight-string">"joined"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2018-12-01T00:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"D"</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">"title"</span> <!-- -->: <span class="lg-highlight-string">"But Writing ..."</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"enrollmentlist"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"giraffe1"</span>, <span class="lg-highlight-string">"artie"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"days"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"T"</span>, <span class="lg-highlight-string">"F"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"enrollee_info"</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">"artie"</span>, <span class="lg-highlight-string">"joined"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2016-05-01T00:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"A"</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">"giraffe1"</span>, <span class="lg-highlight-string">"joined"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2017-10-01T00:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"status"</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-mergeObjects">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Use
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
with
<code class="leafygreen-ui-1l06pbn">
$mergeObjects
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#use--lookup-with--mergeobjects" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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--lookup-with--mergeobjects">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/mergeObjects/#mongodb-expression-exp.-mergeObjects">
<code class="leafygreen-ui-1l06pbn">
$mergeObjects
</code>
</a>
operator combines multiple documents
into a single document.
</p>
<p class="leafygreen-ui-1kp3ins">
Create a collection
<code class="leafygreen-ui-1l06pbn">
orders
</code>
with these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.insertMany( [\n { \"_id\" : 1, \"item\" : \"almonds\", \"price\" : 12, \"quantity\" : 2 },\n { \"_id\" : 2, \"item\" : \"pecans\", \"price\" : 20, \"quantity\" : 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">orders</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">"almonds"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">12</span>, <span class="lg-highlight-string">"quantity"</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>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"pecans"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">20</span>, <span class="lg-highlight-string">"quantity"</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">
Create another collection
<code class="leafygreen-ui-1l06pbn">
items
</code>
with these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.items.insertMany( [\n { \"_id\" : 1, \"item\" : \"almonds\", description: \"almond clusters\", \"instock\" : 120 },\n { \"_id\" : 2, \"item\" : \"bread\", description: \"raisin and nut bread\", \"instock\" : 80 },\n { \"_id\" : 3, \"item\" : \"pecans\", description: \"candied pecans\", \"instock\" : 60 }\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">items</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">"almonds"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"almond clusters"</span>, <span class="lg-highlight-string">"instock"</span> <!-- -->: <span class="lg-highlight-number">120</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">"bread"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"raisin and nut bread"</span>, <span class="lg-highlight-string">"instock"</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">3</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"pecans"</span>, <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"candied pecans"</span>, <span class="lg-highlight-string">"instock"</span> <!-- -->: <span class="lg-highlight-number">60</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 first uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage to
join the two collections by the
<code class="leafygreen-ui-1l06pbn">
item
</code>
fields and then uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/mergeObjects/#mongodb-expression-exp.-mergeObjects">
<code class="leafygreen-ui-1l06pbn">
$mergeObjects
</code>
</a>
in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
<code class="leafygreen-ui-1l06pbn">
$replaceRoot
</code>
</a>
to merge
the joined documents from
<code class="leafygreen-ui-1l06pbn">
items
</code>
and
<code class="leafygreen-ui-1l06pbn">
orders
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.aggregate( [\n {\n $lookup: {\n from: \"items\",\n localField: \"item\", // field in the orders collection\n foreignField: \"item\", // field in the items collection\n as: \"fromItems\"\n }\n },\n {\n $replaceRoot: { newRoot: { $mergeObjects: [ { $arrayElemAt: [ \"$fromItems\", 0 ] }, \"$$ROOT\" ] } }\n },\n { $project: { fromItems: 0 } }\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">orders</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">$lookup</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <span class="lg-highlight-string">"items"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">localField</span>: <span class="lg-highlight-string">"item"</span>, <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> field in the </span><span class="lg-highlight-comment">orders collection</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">foreignField</span>: <span class="lg-highlight-string">"item"</span>, <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> field in the </span><span class="lg-highlight-comment">items collection</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">as</span>: <span class="lg-highlight-string">"fromItems"</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">$replaceRoot</span>: <!-- -->{ <span class="lg-highlight-attr">newRoot</span>: <!-- -->{ <span class="lg-highlight-attr">$mergeObjects</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$arrayElemAt</span>: <!-- -->[ <span class="lg-highlight-string">"$fromItems"</span>, <span class="lg-highlight-number">0</span> ] }<!-- -->, <span class="lg-highlight-string">"$$ROOT"</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>: <!-- -->{ <span class="lg-highlight-attr">fromItems</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">
The operation returns these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: 1,\n item: 'almonds',\n description: 'almond clusters',\n instock: 120,\n price: 12,\n quantity: 2\n},\n{\n _id: 2,\n item: 'pecans',\n description: 'candied pecans',\n instock: 60,\n price: 20,\n quantity: 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-attr">_id</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">'almonds'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">'almond clusters'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">instock</span>: <span class="lg-highlight-number">120</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <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">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"> <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">item</span>: <span class="lg-highlight-string">'pecans'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">'candied pecans'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">instock</span>: <span class="lg-highlight-number">60</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">20</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">quantity</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-lookup-multiple-joins">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Use Multiple Join Conditions and a Correlated Subquery
<a class="headerlink leafygreen-ui-1kepx47" href="#use-multiple-join-conditions-and-a-correlated-subquery" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-join-conditions-and-a-correlated-subquery">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Pipelines can execute on a joined collection and include multiple join
conditions. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator enables more complex join
conditions including conjunctions and non-equality matches.
</p>
<p class="leafygreen-ui-1kp3ins">
A join condition can reference a field in the local collection on which
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>
method was run and reference a
field in the joined collection. This allows a correlated subquery
between the two collections.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB 5.0 supports
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-concise-correlated-subquery" target="_self">
<span>
concise correlated subqueries
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Create a collection
<code class="leafygreen-ui-1l06pbn">
orders
</code>
with these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.insertMany( [\n { \"_id\" : 1, \"item\" : \"almonds\", \"price\" : 12, \"ordered\" : 2 },\n { \"_id\" : 2, \"item\" : \"pecans\", \"price\" : 20, \"ordered\" : 1 },\n { \"_id\" : 3, \"item\" : \"cookies\", \"price\" : 10, \"ordered\" : 60 }\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">orders</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">"almonds"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">12</span>, <span class="lg-highlight-string">"ordered"</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>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"pecans"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">20</span>, <span class="lg-highlight-string">"ordered"</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">3</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"cookies"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-string">"ordered"</span> <!-- -->: <span class="lg-highlight-number">60</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">
Create another collection
<code class="leafygreen-ui-1l06pbn">
warehouses
</code>
with these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.warehouses.insertMany( [\n { \"_id\" : 1, \"stock_item\" : \"almonds\", warehouse: \"A\", \"instock\" : 120 },\n { \"_id\" : 2, \"stock_item\" : \"pecans\", warehouse: \"A\", \"instock\" : 80 },\n { \"_id\" : 3, \"stock_item\" : \"almonds\", warehouse: \"B\", \"instock\" : 60 },\n { \"_id\" : 4, \"stock_item\" : \"cookies\", warehouse: \"B\", \"instock\" : 40 },\n { \"_id\" : 5, \"stock_item\" : \"cookies\", warehouse: \"A\", \"instock\" : 80 }\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">warehouses</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">"stock_item"</span> <!-- -->: <span class="lg-highlight-string">"almonds"</span>, <span class="lg-highlight-attr">warehouse</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"instock"</span> <!-- -->: <span class="lg-highlight-number">120</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">"stock_item"</span> <!-- -->: <span class="lg-highlight-string">"pecans"</span>, <span class="lg-highlight-attr">warehouse</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"instock"</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">3</span>, <span class="lg-highlight-string">"stock_item"</span> <!-- -->: <span class="lg-highlight-string">"almonds"</span>, <span class="lg-highlight-attr">warehouse</span>: <span class="lg-highlight-string">"B"</span>, <span class="lg-highlight-string">"instock"</span> <!-- -->: <span class="lg-highlight-number">60</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">"stock_item"</span> <!-- -->: <span class="lg-highlight-string">"cookies"</span>, <span class="lg-highlight-attr">warehouse</span>: <span class="lg-highlight-string">"B"</span>, <span class="lg-highlight-string">"instock"</span> <!-- -->: <span class="lg-highlight-number">40</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">"stock_item"</span> <!-- -->: <span class="lg-highlight-string">"cookies"</span>, <span class="lg-highlight-attr">warehouse</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"instock"</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>
<p class="leafygreen-ui-1kp3ins">
The following example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Uses a correlated subquery with a join on the
<code class="leafygreen-ui-1l06pbn">
orders.item
</code>
and
<code class="leafygreen-ui-1l06pbn">
warehouse.stock_item
</code>
fields.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Ensures the quantity of the item in stock can fulfill the
ordered quantity.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.aggregate( [\n {\n $lookup:\n {\n from: \"warehouses\",\n let: { order_item: \"$item\", order_qty: \"$ordered\" },\n pipeline: [\n { $match:\n { $expr:\n { $and:\n [\n { $eq: [ \"$stock_item\", \"$$order_item\" ] },\n { $gte: [ \"$instock\", \"$$order_qty\" ] }\n ]\n }\n }\n },\n { $project: { stock_item: 0, _id: 0 } }\n ],\n as: \"stockdata\"\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">orders</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">$lookup</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <span class="lg-highlight-string">"warehouses"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">let</span>: <!-- -->{ <span class="lg-highlight-attr">order_item</span>: <span class="lg-highlight-string">"$item"</span>, <span class="lg-highlight-attr">order_qty</span>: <span class="lg-highlight-string">"$ordered"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pipeline</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$match</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$expr</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"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$eq</span>: <!-- -->[ <span class="lg-highlight-string">"$stock_item"</span>, <span class="lg-highlight-string">"$$order_item"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <!-- -->[ <span class="lg-highlight-string">"$instock"</span>, <span class="lg-highlight-string">"$$order_qty"</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"> <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">stock_item</span>: <span class="lg-highlight-number">0</span>, <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"> <span class="lg-highlight-attr">as</span>: <span class="lg-highlight-string">"stockdata"</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 these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: 1,\n item: 'almonds',\n price: 12,\n ordered: 2,\n stockdata: [\n { warehouse: 'A', instock: 120 },\n { warehouse: 'B', instock: 60 }\n ]\n},\n{\n _id: 2,\n item: 'pecans',\n price: 20,\n ordered: 1,\n stockdata: [ { warehouse: 'A', instock: 80 } ]\n},\n{\n _id: 3,\n item: 'cookies',\n price: 10,\n ordered: 60,\n stockdata: [ { warehouse: 'A', instock: 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">{</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">item</span>: <span class="lg-highlight-string">'almonds'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <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">ordered</span>: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">stockdata</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">warehouse</span>: <span class="lg-highlight-string">'A'</span>, <span class="lg-highlight-attr">instock</span>: <span class="lg-highlight-number">120</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">warehouse</span>: <span class="lg-highlight-string">'B'</span>, <span class="lg-highlight-attr">instock</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">{</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">item</span>: <span class="lg-highlight-string">'pecans'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">20</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ordered</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">stockdata</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">warehouse</span>: <span class="lg-highlight-string">'A'</span>, <span class="lg-highlight-attr">instock</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">{</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">item</span>: <span class="lg-highlight-string">'cookies'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">10</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ordered</span>: <span class="lg-highlight-number">60</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">stockdata</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">warehouse</span>: <span class="lg-highlight-string">'A'</span>, <span class="lg-highlight-attr">instock</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>
<p class="leafygreen-ui-1kp3ins">
The operation corresponds to this pseudo-SQL statement:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"SELECT *, stockdata\nFROM orders\nWHERE stockdata IN (\n SELECT warehouse, instock\n FROM warehouses\n WHERE stock_item = orders.item\n AND instock >= orders.ordered\n);","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> <span class="lg-highlight-operator">*</span>, stockdata</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> orders</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> stockdata <span class="lg-highlight-keyword">IN</span> (</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">SELECT</span> warehouse, instock</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">FROM</span> warehouses</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">WHERE</span> stock_item <span class="lg-highlight-operator">=</span> orders.item</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">AND</span> instock <span class="lg-highlight-operator">>=</span> orders.ordered</td></tr><tr class=""><td class="leafygreen-ui-7razhx">);</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/eq/#mongodb-expression-exp.-eq">
<code class="leafygreen-ui-1l06pbn">
$eq
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lt/#mongodb-expression-exp.-lt">
<code class="leafygreen-ui-1l06pbn">
$lt
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lte/#mongodb-expression-exp.-lte">
<code class="leafygreen-ui-1l06pbn">
$lte
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/gt/#mongodb-expression-exp.-gt">
<code class="leafygreen-ui-1l06pbn">
$gt
</code>
</a>
, and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/gte/#mongodb-expression-exp.-gte">
<code class="leafygreen-ui-1l06pbn">
$gte
</code>
</a>
comparison operators placed in
an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator can use an index on the
<code class="leafygreen-ui-1l06pbn">
from
</code>
collection
referenced in a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage. Limitations:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Indexes can only be used for comparisons between fields and constants, so the
<code class="leafygreen-ui-1l06pbn">
let
</code>
operand must resolve to a constant.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, a comparison between
<code class="leafygreen-ui-1l06pbn">
$a
</code>
and a constant
value can use an index, but a comparison between
<code class="leafygreen-ui-1l06pbn">
$a
</code>
and
<code class="leafygreen-ui-1l06pbn">
$b
</code>
cannot.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Indexes are not used for comparisons where the
<code class="leafygreen-ui-1l06pbn">
let
</code>
operand resolves to an
empty or missing value.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-multikey/#std-label-index-type-multikey">
Multikey indexes
</a>
are not used.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For example, if the index
<code class="leafygreen-ui-1l06pbn">
{ stock_item: 1, instock: 1 }
</code>
exists on
the
<code class="leafygreen-ui-1l06pbn">
warehouses
</code>
collection:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The equality match on the
<code class="leafygreen-ui-1l06pbn">
warehouses.stock_item
</code>
field uses the
index.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The range part of the query on the
<code class="leafygreen-ui-1l06pbn">
warehouses.instock
</code>
field
also uses the indexed field in the compound index.
</p>
</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/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/aggregation-variables/">
Variables in Aggregation Expressions
</a>
</p>
</li>
</ul>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-uncorrelated-subquery">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Perform an Uncorrelated Subquery with
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#perform-an-uncorrelated-subquery-with--lookup" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-an-uncorrelated-subquery-with--lookup">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
An aggregation pipeline
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage can execute a pipeline
on the joined collection, which allows uncorrelated subqueries. An
uncorrelated subquery does not reference the joined document 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">
Starting in MongoDB 5.0, for an uncorrelated subquery in a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
pipeline stage containing a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sample/#mongodb-pipeline-pipe.-sample">
<code class="leafygreen-ui-1l06pbn">
$sample
</code>
</a>
stage, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sampleRate/#mongodb-expression-exp.-sampleRate">
<code class="leafygreen-ui-1l06pbn">
$sampleRate
</code>
</a>
operator, or the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/rand/#mongodb-expression-exp.-rand">
<code class="leafygreen-ui-1l06pbn">
$rand
</code>
</a>
operator, the subquery is always run again if
repeated. Previously, depending on the subquery output size, either the
subquery output was cached or the subquery was run again.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Create a collection
<code class="leafygreen-ui-1l06pbn">
absences
</code>
with these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.absences.insertMany( [\n { \"_id\" : 1, \"student\" : \"Ann Aardvark\", sickdays: [ new Date (\"2018-05-01\"),new Date (\"2018-08-23\") ] },\n { \"_id\" : 2, \"student\" : \"Zoe Zebra\", sickdays: [ new Date (\"2018-02-01\"),new Date (\"2018-05-23\") ] },\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">absences</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">"student"</span> <!-- -->: <span class="lg-highlight-string">"Ann Aardvark"</span>, <span class="lg-highlight-attr">sickdays</span>: <!-- -->[ <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span> <!-- -->(<span class="lg-highlight-string">"2018-05-01"</span>)<!-- -->,<span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span> <!-- -->(<span class="lg-highlight-string">"2018-08-23"</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">"student"</span> <!-- -->: <span class="lg-highlight-string">"Zoe Zebra"</span>, <span class="lg-highlight-attr">sickdays</span>: <!-- -->[ <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span> <!-- -->(<span class="lg-highlight-string">"2018-02-01"</span>)<!-- -->,<span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span> <!-- -->(<span class="lg-highlight-string">"2018-05-23"</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">
Create another collection
<code class="leafygreen-ui-1l06pbn">
holidays
</code>
with these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.holidays.insertMany( [\n { \"_id\" : 1, year: 2018, name: \"New Years\", date: new Date(\"2018-01-01\") },\n { \"_id\" : 2, year: 2018, name: \"Pi Day\", date: new Date(\"2018-03-14\") },\n { \"_id\" : 3, year: 2018, name: \"Ice Cream Day\", date: new Date(\"2018-07-15\") },\n { \"_id\" : 4, year: 2017, name: \"New Years\", date: new Date(\"2017-01-01\") },\n { \"_id\" : 5, year: 2017, name: \"Ice Cream Day\", date: new Date(\"2017-07-16\") }\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">holidays</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-attr">year</span>: <span class="lg-highlight-number">2018</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"New Years"</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2018-01-01"</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-attr">year</span>: <span class="lg-highlight-number">2018</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Pi Day"</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2018-03-14"</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-attr">year</span>: <span class="lg-highlight-number">2018</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Ice Cream Day"</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2018-07-15"</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-attr">year</span>: <span class="lg-highlight-number">2017</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"New Years"</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2017-01-01"</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-attr">year</span>: <span class="lg-highlight-number">2017</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Ice Cream Day"</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2017-07-16"</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 joins the
<code class="leafygreen-ui-1l06pbn">
absences
</code>
collection with 2018
holiday information from the
<code class="leafygreen-ui-1l06pbn">
holidays
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.absences.aggregate( [\n {\n $lookup:\n {\n from: \"holidays\",\n pipeline: [\n { $match: { year: 2018 } },\n { $project: { _id: 0, date: { name: \"$name\", date: \"$date\" } } },\n { $replaceRoot: { newRoot: \"$date\" } }\n ],\n as: \"holidays\"\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">absences</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">$lookup</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <span class="lg-highlight-string">"holidays"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pipeline</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$match</span>: <!-- -->{ <span class="lg-highlight-attr">year</span>: <span class="lg-highlight-number">2018</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">date</span>: <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"$name"</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-string">"$date"</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$replaceRoot</span>: <!-- -->{ <span class="lg-highlight-attr">newRoot</span>: <span class="lg-highlight-string">"$date"</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">as</span>: <span class="lg-highlight-string">"holidays"</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:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: 1,\n student: 'Ann Aardvark',\n sickdays: [\n ISODate(\"2018-05-01T00:00:00.000Z\"),\n ISODate(\"2018-08-23T00:00:00.000Z\")\n ],\n holidays: [\n { name: 'New Years', date: ISODate(\"2018-01-01T00:00:00.000Z\") },\n { name: 'Pi Day', date: ISODate(\"2018-03-14T00:00:00.000Z\") },\n { name: 'Ice Cream Day', date: ISODate(\"2018-07-15T00:00:00.000Z\")\n }\n ]\n},\n{\n _id: 2,\n student: 'Zoe Zebra',\n sickdays: [\n ISODate(\"2018-02-01T00:00:00.000Z\"),\n ISODate(\"2018-05-23T00:00:00.000Z\")\n ],\n holidays: [\n { name: 'New Years', date: ISODate(\"2018-01-01T00:00:00.000Z\") },\n { name: 'Pi Day', date: ISODate(\"2018-03-14T00:00:00.000Z\") },\n { name: 'Ice Cream Day', date: ISODate(\"2018-07-15T00:00:00.000Z\")\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">student</span>: <span class="lg-highlight-string">'Ann Aardvark'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sickdays</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2018-05-01T00:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2018-08-23T00:00:00.000Z"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">holidays</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'New Years'</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2018-01-01T00:00:00.000Z"</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'Pi Day'</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2018-03-14T00:00:00.000Z"</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'Ice Cream Day'</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2018-07-15T00: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">}<!-- -->,</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">student</span>: <span class="lg-highlight-string">'Zoe Zebra'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sickdays</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2018-02-01T00:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2018-05-23T00:00:00.000Z"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">holidays</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'New Years'</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2018-01-01T00:00:00.000Z"</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'Pi Day'</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2018-03-14T00:00:00.000Z"</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'Ice Cream Day'</span>, <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2018-07-15T00: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">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 corresponds to this pseudo-SQL statement:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"SELECT *, holidays\nFROM absences\nWHERE holidays IN (\n SELECT name, date\n FROM holidays\n WHERE year = 2018\n);","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> <span class="lg-highlight-operator">*</span>, holidays</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> absences</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> holidays <span class="lg-highlight-keyword">IN</span> (</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">SELECT</span> name, <span class="lg-highlight-type">date</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">FROM</span> holidays</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">WHERE</span> <span class="lg-highlight-keyword">year</span> <span class="lg-highlight-operator">=</span> <span class="lg-highlight-number">2018</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 more information, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-uncorrelated-subqueries-performance" target="_self">
<span>
Uncorrelated Subquery Performance Considerations
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-concise-correlated-subquery">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Perform a Concise Correlated Subquery with
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#perform-a-concise-correlated-subquery-with--lookup" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-concise-correlated-subquery-with--lookup">
</div>
</a>
</h3>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.0, an aggregation pipeline
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage supports a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-syntax-concise-correlated-subquery" target="_self">
<span>
concise correlated subquery syntax
</span>
</a>
that improves joins between
collections. The new concise syntax removes the requirement for an
equality match on the foreign and local fields inside of an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator in a
<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.
</p>
<p class="leafygreen-ui-1kp3ins">
Create a collection
<code class="leafygreen-ui-1l06pbn">
restaurants
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.restaurants.insertMany( [\n {\n _id: 1,\n name: \"American Steak House\",\n food: [ \"filet\", \"sirloin\" ],\n beverages: [ \"beer\", \"wine\" ]\n },\n {\n _id: 2,\n name: \"Honest John Pizza\",\n food: [ \"cheese pizza\", \"pepperoni pizza\" ],\n beverages: [ \"soda\" ]\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">restaurants</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>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"American Steak House"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">food</span>: <!-- -->[ <span class="lg-highlight-string">"filet"</span>, <span class="lg-highlight-string">"sirloin"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">beverages</span>: <!-- -->[ <span class="lg-highlight-string">"beer"</span>, <span class="lg-highlight-string">"wine"</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">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Honest John Pizza"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">food</span>: <!-- -->[ <span class="lg-highlight-string">"cheese pizza"</span>, <span class="lg-highlight-string">"pepperoni pizza"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">beverages</span>: <!-- -->[ <span class="lg-highlight-string">"soda"</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">
Create another collection
<code class="leafygreen-ui-1l06pbn">
orders
</code>
with food and optional drink
orders:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.insertMany( [\n {\n _id: 1,\n item: \"filet\",\n restaurant_name: \"American Steak House\"\n },\n {\n _id: 2,\n item: \"cheese pizza\",\n restaurant_name: \"Honest John Pizza\",\n drink: \"lemonade\"\n },\n {\n _id: 3,\n item: \"cheese pizza\",\n restaurant_name: \"Honest John Pizza\",\n drink: \"soda\"\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">orders</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>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"filet"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">restaurant_name</span>: <span class="lg-highlight-string">"American Steak House"</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">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"cheese pizza"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">restaurant_name</span>: <span class="lg-highlight-string">"Honest John Pizza"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">drink</span>: <span class="lg-highlight-string">"lemonade"</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">item</span>: <span class="lg-highlight-string">"cheese pizza"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">restaurant_name</span>: <span class="lg-highlight-string">"Honest John Pizza"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">drink</span>: <span class="lg-highlight-string">"soda"</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 example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Joins the
<code class="leafygreen-ui-1l06pbn">
orders
</code>
and
<code class="leafygreen-ui-1l06pbn">
restaurants
</code>
collections by matching the
<code class="leafygreen-ui-1l06pbn">
orders.restaurant_name
</code>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-concise-correlated-subquery-localField" target="_self">
<span>
localField
</span>
</a>
with the
<code class="leafygreen-ui-1l06pbn">
restaurants.name
</code>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-concise-correlated-subquery-foreignField" target="_self">
<span>
foreignField
</span>
</a>
. The match is
performed before the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
is run.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Performs an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/in/#mongodb-expression-exp.-in">
<code class="leafygreen-ui-1l06pbn">
$in
</code>
</a>
array match between the
<code class="leafygreen-ui-1l06pbn">
orders.drink
</code>
and
<code class="leafygreen-ui-1l06pbn">
restaurants.beverages
</code>
fields that are accessed using
<code class="leafygreen-ui-1l06pbn">
$$orders_drink
</code>
and
<code class="leafygreen-ui-1l06pbn">
$beverages
</code>
respectively.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.aggregate( [\n {\n $lookup: {\n from: \"restaurants\",\n localField: \"restaurant_name\",\n foreignField: \"name\",\n let: { orders_drink: \"$drink\" },\n pipeline: [ {\n $match: {\n $expr: { $in: [ \"$$orders_drink\", \"$beverages\" ] }\n }\n } ],\n as: \"matches\"\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">orders</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">$lookup</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <span class="lg-highlight-string">"restaurants"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">localField</span>: <span class="lg-highlight-string">"restaurant_name"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">foreignField</span>: <span class="lg-highlight-string">"name"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">let</span>: <!-- -->{ <span class="lg-highlight-attr">orders_drink</span>: <span class="lg-highlight-string">"$drink"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pipeline</span>: <!-- -->[ <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$match</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$expr</span>: <!-- -->{ <span class="lg-highlight-attr">$in</span>: <!-- -->[ <span class="lg-highlight-string">"$$orders_drink"</span>, <span class="lg-highlight-string">"$beverages"</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">as</span>: <span class="lg-highlight-string">"matches"</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">
There is a match for the
<code class="leafygreen-ui-1l06pbn">
soda
</code>
value in the
<code class="leafygreen-ui-1l06pbn">
orders.drink
</code>
and
<code class="leafygreen-ui-1l06pbn">
restaurants.beverages
</code>
fields. This output shows the
<code class="leafygreen-ui-1l06pbn">
matches
</code>
array and contains all joined fields from the
<code class="leafygreen-ui-1l06pbn">
restaurants
</code>
collection
for the match:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : 1, \"item\" : \"filet\",\n \"restaurant_name\" : \"American Steak House\",\n \"matches\" : [ ]\n}\n{\n \"_id\" : 2, \"item\" : \"cheese pizza\",\n \"restaurant_name\" : \"Honest John Pizza\",\n \"drink\" : \"lemonade\",\n \"matches\" : [ ]\n}\n{\n \"_id\" : 3, \"item\" : \"cheese pizza\",\n \"restaurant_name\" : \"Honest John Pizza\",\n \"drink\" : \"soda\",\n \"matches\" : [ {\n \"_id\" : 2, \"name\" : \"Honest John Pizza\",\n \"food\" : [ \"cheese pizza\", \"pepperoni pizza\" ],\n \"beverages\" : [ \"soda\" ]\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">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"filet"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"restaurant_name"</span> <!-- -->: <span class="lg-highlight-string">"American Steak House"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"matches"</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>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"cheese pizza"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"restaurant_name"</span> <!-- -->: <span class="lg-highlight-string">"Honest John Pizza"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"drink"</span> <!-- -->: <span class="lg-highlight-string">"lemonade"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"matches"</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>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"cheese pizza"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"restaurant_name"</span> <!-- -->: <span class="lg-highlight-string">"Honest John Pizza"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"drink"</span> <!-- -->: <span class="lg-highlight-string">"soda"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"matches"</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">"Honest John Pizza"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"food"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"cheese pizza"</span>, <span class="lg-highlight-string">"pepperoni pizza"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"beverages"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"soda"</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">
Before the introduction of concise correlated subqueries, you had to use
an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/eq/#mongodb-expression-exp.-eq">
<code class="leafygreen-ui-1l06pbn">
$eq
</code>
</a>
equality match between the local field and the
joined field in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator in the
<code class="leafygreen-ui-1l06pbn">
pipeline
</code>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-lookup" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</span>
</a>
stage as shown in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-multiple-joins" target="_self">
<span>
Use Multiple Join Conditions and a Correlated Subquery
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
This example uses the older verbose syntax from MongoDB versions before
5.0 and returns the same results as the previous concise example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.aggregate( [\n {\n $lookup: {\n from: \"restaurants\",\n let: { orders_restaurant_name: \"$restaurant_name\",\n orders_drink: \"$drink\" },\n pipeline: [ {\n $match: {\n $expr: {\n $and: [\n { $eq: [ \"$$orders_restaurant_name\", \"$name\" ] },\n { $in: [ \"$$orders_drink\", \"$beverages\" ] }\n ]\n }\n }\n } ],\n as: \"matches\"\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">orders</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">$lookup</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <span class="lg-highlight-string">"restaurants"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">let</span>: <!-- -->{ <span class="lg-highlight-attr">orders_restaurant_name</span>: <span class="lg-highlight-string">"$restaurant_name"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">orders_drink</span>: <span class="lg-highlight-string">"$drink"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pipeline</span>: <!-- -->[ <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$match</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$expr</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">$eq</span>: <!-- -->[ <span class="lg-highlight-string">"$$orders_restaurant_name"</span>, <span class="lg-highlight-string">"$name"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$in</span>: <!-- -->[ <span class="lg-highlight-string">"$$orders_drink"</span>, <span class="lg-highlight-string">"$beverages"</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"> <span class="lg-highlight-attr">as</span>: <span class="lg-highlight-string">"matches"</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 previous examples correspond to this pseudo-SQL statement:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"SELECT *, matches\nFROM orders\nWHERE matches IN (\n SELECT *\n FROM restaurants\n WHERE restaurants.name = orders.restaurant_name\n AND restaurants.beverages = orders.drink\n);","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> <span class="lg-highlight-operator">*</span>, <span class="lg-highlight-keyword">matches</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> orders</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> <span class="lg-highlight-keyword">matches</span> <span class="lg-highlight-keyword">IN</span> (</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">SELECT</span> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">FROM</span> restaurants</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">WHERE</span> restaurants.name <span class="lg-highlight-operator">=</span> orders.restaurant_name</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">AND</span> restaurants.beverages <span class="lg-highlight-operator">=</span> orders.drink</td></tr><tr class=""><td class="leafygreen-ui-7razhx">);</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-correlated-subqueries-performance" target="_self">
<span>
Correlated Subquery Performance Considerations
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-lookup-example-namespaces-in-subpipelines">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Namespaces in Subpipelines
<a class="headerlink leafygreen-ui-1kepx47" href="#namespaces-in-subpipelines" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="namespaces-in-subpipelines">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, namespaces in subpipelines within
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
and
<code class="leafygreen-ui-1l06pbn">
$unionWith
</code>
are validated to ensure the correct use of
<code class="leafygreen-ui-1l06pbn">
from
</code>
and
<code class="leafygreen-ui-1l06pbn">
coll
</code>
fields:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
, omit the
<code class="leafygreen-ui-1l06pbn">
from
</code>
field if you use a subpipeline with
a stage which doesn't require a specified collection. For example, a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/documents/#mongodb-pipeline-pipe.-documents">
<code class="leafygreen-ui-1l06pbn">
$documents
</code>
</a>
stage.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Similarly, for
<code class="leafygreen-ui-1l06pbn">
$unionWith
</code>
, omit the
<code class="leafygreen-ui-1l06pbn">
coll
</code>
field.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Unchanged behavior:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For a
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
that starts with a stage for a collection, for
example a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/collStats/#mongodb-pipeline-pipe.-collStats">
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
</a>
subpipeline,
you must include the
<code class="leafygreen-ui-1l06pbn">
from
</code>
field and specify the collection.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Similarly, for
<code class="leafygreen-ui-1l06pbn">
$unionWith
</code>
, include the
<code class="leafygreen-ui-1l06pbn">
coll
</code>
field and specify
the collection.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The following scenario shows an example.
</p>
<p class="leafygreen-ui-1kp3ins">
Create a collection
<code class="leafygreen-ui-1l06pbn">
cakeFlavors
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeFlavors.insertMany( [\n { _id: 1, flavor: \"chocolate\" },\n { _id: 2, flavor: \"strawberry\" },\n { _id: 3, flavor: \"cherry\" }\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">cakeFlavors</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">flavor</span>: <span class="lg-highlight-string">"chocolate"</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">flavor</span>: <span class="lg-highlight-string">"strawberry"</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">flavor</span>: <span class="lg-highlight-string">"cherry"</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">
Starting in MongoDB 8.0, the following example returns an error because
it contains an invalid
<code class="leafygreen-ui-1l06pbn">
from
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeFlavors.aggregate( [ {\n $lookup: {\n from: \"cakeFlavors\",\n pipeline: [ { $documents: [ {} ] } ],\n as: \"test\"\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">cakeFlavors</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">$lookup</span>: <!-- -->{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <span class="lg-highlight-string">"cakeFlavors"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pipeline</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$documents</span>: <!-- -->[ <!-- -->{<!-- -->} ] } ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">as</span>: <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">} ] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 MongoDB versions before 8.0, the previous example runs.
</p>
<p class="leafygreen-ui-1kp3ins">
For an example with a valid
<code class="leafygreen-ui-1l06pbn">
from
</code>
field, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-lookup-single-equality-example" target="_self">
<span>
Perform a Single Equality Join with
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
.
</span>
</a>
</p>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/support/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Technical Support
<a class="headerlink leafygreen-ui-1kepx47" href="#technical-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="technical-support">
</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="#mongodb-community" target="_self">
<span>
MongoDB Community
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#mongodb-atlas-or-cloud-manager" target="_self">
<span>
MongoDB Atlas or Cloud Manager
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#mongodb-enterprise-or-ops-manager" target="_self">
<span>
MongoDB Enterprise or Ops Manager
</span>
</a>
</li>
</ul>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
MongoDB Community
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-community" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-community">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
For questions, discussions, or general technical support, visit the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://community.mongodb.com/" target="_self">
<span>
MongoDB Community Forums
</span>
</a>
.
The MongoDB Community Forums are a centralized place to connect with
other MongoDB users, ask questions, and get answers.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
MongoDB Atlas or Cloud Manager
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-atlas-or-cloud-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="mongodb-atlas-or-cloud-manager">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
For technical support questions, please log into your
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://cloud.mongodb.com/user" target="_self">
<span>
MongoDB Cloud
account
</span>
</a>
and open a
support ticket.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
MongoDB Enterprise or Ops Manager
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-enterprise-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="mongodb-enterprise-or-ops-manager">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
For technical support questions, please file a ticket through the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://support.mongodb.com" target="_self">
<span>
MongoDB Support Portal
</span>
</a>
.
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/bitNot/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$bitNot (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-bitnot--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="-bitnot--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>
<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>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.3
</em>
.
</p>
</div>
<dl class="expression">
<dt>
<code class="leafygreen-ui-1l06pbn">
$bitNot
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-bitNot" 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.-bitNot">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns the result of a bitwise
<code class="leafygreen-ui-1l06pbn">
not
</code>
operation on a single
<code class="leafygreen-ui-1l06pbn">
int
</code>
or
<code class="leafygreen-ui-1l06pbn">
long
</code>
value.
</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
<code class="leafygreen-ui-1l06pbn">
$bitNot
</code>
operator has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $bitNot: <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">$bitNot</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 expression can be a single argument or an array with one
<code class="leafygreen-ui-1l06pbn">
int
</code>
or
<code class="leafygreen-ui-1l06pbn">
long
</code>
element.
</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>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
All numbers 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>
are doubles, not integers. To
specify integers 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>
, use the
<code class="leafygreen-ui-1l06pbn">
NumberInt()
</code>
or the
<code class="leafygreen-ui-1l06pbn">
NumberLong()
</code>
constructor. To learn more, see
<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
</span>
</a>
or
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/data-types/#std-label-shell-type-long" target="_self">
<span>
Long
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To learn how your MongoDB driver handles numeric values, refer to your
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
driver's documentation
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If any arguments in the array are of a different data type such as a string,
double, or decimal, MongoDB returns an error.
</p>
<p class="leafygreen-ui-1kp3ins">
If the expression evalutates to
<code class="leafygreen-ui-1l06pbn">
null
</code>
, the operation returns
<code class="leafygreen-ui-1l06pbn">
null
</code>
.
</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 example on this page uses the
<code class="leafygreen-ui-1l06pbn">
switches
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.switches.insertMany( [\n { _id: 0, a: NumberInt(0), b: NumberInt(127) },\n { _id: 1, a: NumberInt(2), b: NumberInt(3) },\n { _id: 2, a: NumberInt(3), b: NumberInt(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">switches</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">a</span>: <span class="lg-highlight-title lg-highlight-class">NumberInt</span>(<span class="lg-highlight-number">0</span>)<!-- -->, <span class="lg-highlight-attr">b</span>: <span class="lg-highlight-title lg-highlight-class">NumberInt</span>(<span class="lg-highlight-number">127</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">a</span>: <span class="lg-highlight-title lg-highlight-class">NumberInt</span>(<span class="lg-highlight-number">2</span>)<!-- -->, <span class="lg-highlight-attr">b</span>: <span class="lg-highlight-title lg-highlight-class">NumberInt</span>(<span class="lg-highlight-number">3</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">a</span>: <span class="lg-highlight-title lg-highlight-class">NumberInt</span>(<span class="lg-highlight-number">3</span>)<!-- -->, <span class="lg-highlight-attr">b</span>: <span class="lg-highlight-title lg-highlight-class">NumberInt</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 following aggregation uses the
<code class="leafygreen-ui-1l06pbn">
$bitNot
</code>
operator in 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.switches.aggregate( [\n {\n $project: {\n result: {\n $bitNot: \"$a\"\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">switches</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"> <span class="lg-highlight-attr">result</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$bitNot</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><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":" [\n { _id: 0, result: -1 },\n { _id: 1, result: -3 },\n { _id: 2, result: -4 }\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>, <span class="lg-highlight-attr">result</span>: <!-- -->-<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">1</span>, <span class="lg-highlight-attr">result</span>: <!-- -->-<span class="lg-highlight-number">3</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">result</span>: <!-- -->-<span class="lg-highlight-number">4</span> }</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/aggregation/#std-label-aggregation-pipeline-operators">
Aggregation Operators
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/bit/#std-label-update-bit">
$bit
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/db.runCommand/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.runCommand()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.runcommand--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.runcommand--">
</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>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#response" target="_self">
<span>
Response
</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.runCommand(command, [options])
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.runCommand" 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.runCommand">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Provides a helper to run specified
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/#std-label-database-commands">
database commands
</a>
. This is the preferred method to issue
database commands, as it provides a consistent interface between
the shell and drivers.
</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">
command
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document or string
</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-document">
Document
</a>
or string that specifies the
command. If specified as a string,
<code class="leafygreen-ui-1l06pbn">
db.runCommand()
</code>
transforms the string into a document.
</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>
Starting in mongosh 2.0, options that specify how
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
runs the command.
</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">
options.readPreference
</code>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-runCommand-options-readPref-arg">
</span>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#std-label-read-preference">
read preference
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Read preference to run the command with. If not specified,
defaults to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#mongodb-readmode-primary">
<code class="leafygreen-ui-1l06pbn">
primary
</code>
</a>
.
<code class="leafygreen-ui-1l06pbn">
db.runCommand()
</code>
ignores
any other read preference global configuration set, including
using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Mongo.setReadPref/#mongodb-method-Mongo.setReadPref">
<code class="leafygreen-ui-1l06pbn">
setReadPref()
</code>
</a>
or by specifying
<code class="leafygreen-ui-1l06pbn">
readPreference
</code>
in a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/connection-string/#std-label-mongodb-uri">
connection string
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
In mongosh 1.x,
<code class="leafygreen-ui-1l06pbn">
db.runCommand()
</code>
does not take an
<code class="leafygreen-ui-1l06pbn">
options
</code>
argument. To set the read preference, use
<code class="leafygreen-ui-1l06pbn">
Mongo.setReadPref()
</code>
or specify
<code class="leafygreen-ui-1l06pbn">
readPreference
</code>
in the
connection string.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
To specify a time limit in milliseconds, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/terminate-running-operations/#std-label-terminate-running-ops">
Terminate Running Operations
<!-- -->
.
</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">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.runCommand" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.runCommand()
</code>
</span>
</a>
runs the command in the context of the
current database. Some commands are only applicable in the context of
the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database, and you must change your
<code class="leafygreen-ui-1l06pbn">
db
</code>
object
before running these commands or 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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-runCommand-read-pref-behavior">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
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>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in mongosh 2.0, you can specify the read preference for a
command with the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-runCommand-options-readPref-arg" target="_self">
<span>
options
</span>
</a>
argument. If you do not specify a read preference,
<code class="leafygreen-ui-1l06pbn">
db.runCommand()
</code>
defaults to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#mongodb-readmode-primary">
<code class="leafygreen-ui-1l06pbn">
primary
</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">
In mongosh 2.0,
<code class="leafygreen-ui-1l06pbn">
db.runCommand()
</code>
ignores any other read preference
global configuration set, including using
<code class="leafygreen-ui-1l06pbn">
Mongo.setReadPref()
</code>
or
by specifying
<code class="leafygreen-ui-1l06pbn">
readPreference
</code>
in a connection string.
</p>
<p class="leafygreen-ui-1kp3ins">
In mongosh 1.x,
<code class="leafygreen-ui-1l06pbn">
db.runCommand()
</code>
does not take an
<code class="leafygreen-ui-1l06pbn">
options
</code>
argument.
<code class="leafygreen-ui-1l06pbn">
db.runCommand()
</code>
uses the read preference specified either
by using
<code class="leafygreen-ui-1l06pbn">
Mongo.setReadPref()
</code>
or by specifying
<code class="leafygreen-ui-1l06pbn">
readPreference
</code>
in
the connection string.
</p>
</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>
<p class="leafygreen-ui-1kp3ins">
The following examples show how to use
<code class="leafygreen-ui-1l06pbn">
db.runCommand()
</code>
to run
database commands.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
hello Command Without Specified Read Preference
<a class="headerlink leafygreen-ui-1kepx47" href="#hello-command-without-specified-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="hello-command-without-specified-read-preference">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
This example shows how to use
<code class="leafygreen-ui-1l06pbn">
db.runCommand()
</code>
to run 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.
</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>
<p class="leafygreen-ui-1kp3ins">
For details on output, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/hello/#std-label-hello-cmd-output">
hello Output
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
For details on read preference behavior, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-runCommand-read-pref-behavior" target="_self">
<span>
Read Preference
<!-- -->
.
</span>
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
hello Command with readPreference: secondaryPreferred
<a class="headerlink leafygreen-ui-1kepx47" href="#hello-command-with-readpreference--secondarypreferred" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="hello-command-with-readpreference--secondarypreferred">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
This example shows how to use
<code class="leafygreen-ui-1l06pbn">
db.runCommand()
</code>
to run 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 with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#mongodb-readmode-secondaryPreferred">
<code class="leafygreen-ui-1l06pbn">
secondaryPreferred
</code>
</a>
read preference.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand( { hello: 1 }, { readPreference: \"secondaryPreferred\" } )","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">hello</span>: <span class="lg-highlight-number">1</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">readPreference</span>: <span class="lg-highlight-string">"secondaryPreferred"</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 details on output, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/hello/#std-label-hello-cmd-output">
hello Output
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
For details on read preference behavior, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-runCommand-read-pref-behavior" target="_self">
<span>
Read Preference
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-command-response">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Response
<a class="headerlink leafygreen-ui-1kepx47" href="#response" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="response">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The method returns a response document that contains the following
fields:
</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>
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>
<em>
<command result>
</em>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Result fields specific to the
<code class="leafygreen-ui-1l06pbn">
command
</code>
that ran.
</div>
</div>
</td>
</tr>
<tr class="">
<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>
A number that indicates if the command succeeded (
<code class="leafygreen-ui-1l06pbn">
1
</code>
)
or failed (
<code class="leafygreen-ui-1l06pbn">
0
</code>
).
</div>
</div>
</td>
</tr>
<tr class="">
<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>
<p class="leafygreen-ui-1kp3ins">
The logical time of the operation. MongoDB uses the logical time
to order operations.
<em>
Only for replica sets and sharded
clusters.
</em>
</p>
<p class="leafygreen-ui-1kp3ins">
If the command does not generate an oplog entry, for example, a
read operation, then the operation does not advance the logical
clock. In this case,
<code class="leafygreen-ui-1l06pbn">
operationTime
</code>
returns:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For 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>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-snapshot/#mongodb-readconcern-readconcern.-snapshot-">
<code class="leafygreen-ui-1l06pbn">
"snapshot"
</code>
</a>
, and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-linearizable/#mongodb-readconcern-readconcern.-linearizable-">
<code class="leafygreen-ui-1l06pbn">
"linearizable"
</code>
</a>
,
the timestamp of the most recent entry in the oplog.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For read concern
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-majority/#mongodb-readconcern-readconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
, the timestamp of
the most recent
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
majority-acknowledged
</code>
</a>
entry in the oplog.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For operations associated with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-isolation-consistency-recency/#std-label-causal-consistency">
causally consistent sessions
</a>
, the MongoDB drivers use the logical time
to automatically set the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern/#std-label-afterClusterTime">
Read Operations and
<code class="leafygreen-ui-1l06pbn">
afterClusterTime
</code>
</a>
period.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<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>
<p class="leafygreen-ui-1kp3ins">
A document that returns the signed cluster time. Cluster time is a
logical time used for ordering of operations.
<em>
Only for replica
sets and sharded clusters. For internal use only.
</em>
</p>
<p class="leafygreen-ui-1kp3ins">
The document contains the following fields:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
clusterTime
</code>
: timestamp of the highest known cluster time for the member.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
signature
</code>
: a document that contains the hash of the cluster time and the id
of the key used to sign the cluster time.
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/accumulator/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$accumulator (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-accumulator--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="-accumulator--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">
$accumulator
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-group-grp.-accumulator" 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.-accumulator">
</div>
</a>
</dt>
<dd>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Server-side JavaScript Deprecated
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, server-side JavaScript functions
(
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/function/#mongodb-expression-exp.-function">
<code class="leafygreen-ui-1l06pbn">
$function
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/where/#mongodb-query-op.-where">
<code class="leafygreen-ui-1l06pbn">
$where
</code>
</a>
) are
deprecated. MongoDB logs a warning when you run these functions.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Defines a custom
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-agg-operators-group-accumulators">
accumulator operator
</a>
. Accumulators are operators that
maintain their state (e.g. totals, maximums, minimums, and related
data) as documents progress through the pipeline. Use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
operator to execute your own JavaScript
functions to implement behavior not supported by the MongoDB Query
Language. See also
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/function/#mongodb-expression-exp.-function">
<code class="leafygreen-ui-1l06pbn">
$function
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
is available in these 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/bucket/#mongodb-pipeline-pipe.-bucket">
<code class="leafygreen-ui-1l06pbn">
$bucket
</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/bucketAuto/#mongodb-pipeline-pipe.-bucketAuto">
<code class="leafygreen-ui-1l06pbn">
$bucketAuto
</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/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
</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">
Executing JavaScript inside of an aggregation operator may
decrease performance. Only use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
operator
if the provided
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation-pipeline/#std-label-aggregation-pipeline-operator-reference">
pipeline operators
</a>
cannot fulfill your
application's needs.
</p>
</div>
</div>
</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.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
operator has this syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $accumulator: {\n init: <code>,\n initArgs: <array>, // Optional\n accumulate: <code>,\n accumulateArgs: <array>,\n merge: <code>,\n finalize: <code>, // Optional\n lang: <string>\n }\n}</code></code></code></code>","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">$accumulator</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">init</span>: <<!-- -->code<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">initArgs</span>: <!-- --><array expression>, // Optional</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> accumulate: <code>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> accumulateArgs: <array expression>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> merge: <code>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> finalize: <code>, // Optional</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> lang: <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>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:15%"/>
<col style="width:40%"/>
</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>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-init" target="_self">
<span>
init
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
String or Code
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-accumulator-init">
</span>
<p class="leafygreen-ui-1kp3ins">
Function used to initialize the state. The
<code class="leafygreen-ui-1l06pbn">
init
</code>
function
receives its arguments from the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-initArgs" target="_self">
<span>
initArgs
</span>
</a>
array expression. You can specify the
function definition as either BSON type Code or String.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
init
</code>
function has the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"function (<initarg1>, <initarg2>, ...) {\n ...\n return <initialstate>\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"><span class="lg-highlight-keyword lg-highlight-function">function</span> <!-- -->(<span class="lg-highlight-params"><initArg1>, <initArg2>, ...</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">return</span> <<!-- -->initialState<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Spilling to disk or running a query on a sharded cluster can cause the
accumulator to be computed as a merge of multiple sub-accumulations, each
of which begins by calling
<code class="leafygreen-ui-1l06pbn">
init()
</code>
. Ensure that your
<code class="leafygreen-ui-1l06pbn">
init()
</code>
,
<code class="leafygreen-ui-1l06pbn">
accumulate()
</code>
, and
<code class="leafygreen-ui-1l06pbn">
merge()
</code>
functions are compatible with this
execution model.
</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="#std-label-accumulator-initArgs" target="_self">
<span>
initArgs
</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-accumulator-initArgs">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. Arguments passed to the
<code class="leafygreen-ui-1l06pbn">
init
</code>
function.
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
initArgs
</code>
has the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[ <initarg1>, <initarg2>, ... ]","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[ <<!-- -->initArg1<!-- -->><!-- -->,<!-- --> <!-- --><<!-- -->initArg2<!-- -->><!-- -->,<!-- --> <!-- -->...<!-- --> ]</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<strong class="css-rws738">
IMPORTANT:
</strong>
When used in a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/bucketAuto/#mongodb-pipeline-pipe.-bucketAuto">
<code class="leafygreen-ui-1l06pbn">
$bucketAuto
</code>
</a>
stage,
<code class="leafygreen-ui-1l06pbn">
initArgs
</code>
cannot refer to the group key (i.e., you cannot use the
<code class="leafygreen-ui-1l06pbn">
$<fieldName>
</code>
syntax). Instead, in a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/bucketAuto/#mongodb-pipeline-pipe.-bucketAuto">
<code class="leafygreen-ui-1l06pbn">
$bucketAuto
</code>
</a>
stage,
you can only specify constant values in
<code class="leafygreen-ui-1l06pbn">
initArgs
</code>
.
</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="#std-label-accumulator-accumulate" target="_self">
<span>
accumulate
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
String or Code
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-accumulator-accumulate">
</span>
<p class="leafygreen-ui-1kp3ins">
Function used to accumulate documents. The
<code class="leafygreen-ui-1l06pbn">
accumulate
</code>
function receives its arguments from the current state and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-accumulateArgs" target="_self">
<span>
accumulateArgs
</span>
</a>
array
expression. The result of the
<code class="leafygreen-ui-1l06pbn">
accumulate
</code>
function becomes
the new state. You can specify the function definition as
either BSON type Code or String.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
accumulate
</code>
function has the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"function(state, <accumarg1>, <accumarg2>, ...) {\n ...\n return <newstate>\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"><span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">state, <accumArg1>, <accumArg2>, ...</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">return</span> <<!-- -->newState<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</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="#std-label-accumulator-accumulateArgs" target="_self">
<span>
accumulateArgs
</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-accumulator-accumulateArgs">
</span>
<p class="leafygreen-ui-1kp3ins">
Arguments passed to the
<code class="leafygreen-ui-1l06pbn">
accumulate
</code>
function. You can use
<code class="leafygreen-ui-1l06pbn">
accumulateArgs
</code>
to specify what field value(s) to pass to
the
<code class="leafygreen-ui-1l06pbn">
accumulate
</code>
function.
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
accumulateArgs
</code>
has the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[ <accumarg1>, <accumarg2>, ... ]","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[ <<!-- -->accumArg1<!-- -->><!-- -->,<!-- --> <!-- --><<!-- -->accumArg2<!-- -->><!-- -->,<!-- --> <!-- -->...<!-- --> ]</td></tr></tbody></table></code></pre>
</div>
</div>
</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="#std-label-accumulator-merge" target="_self">
<span>
merge
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
String or Code
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-accumulator-merge">
</span>
<p class="leafygreen-ui-1kp3ins">
Function used to merge two internal states.
<code class="leafygreen-ui-1l06pbn">
merge
</code>
must be
either a String or Code BSON type.
<code class="leafygreen-ui-1l06pbn">
merge
</code>
returns the
combined result of the two merged states. For information on
when the merge function is called, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-multiple-states" target="_self">
<span>
Merge Two States with
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
merge
</code>
function has the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"function (<state1>, <state2>) {\n <logic>\n return <newstate>\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"><span class="lg-highlight-keyword lg-highlight-function">function</span> <!-- -->(<span class="lg-highlight-params"><state1>, <state2></span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- --><logic to merge state1 and state2></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> return <newState></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</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="#std-label-accumulator-finalize" target="_self">
<span>
finalize
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
String or Code
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-accumulator-finalize">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. Function used to update the result of the accumulation.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
finalize
</code>
function has the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"function (state) {\n ...\n return <finalstate>\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"><span class="lg-highlight-keyword lg-highlight-function">function</span> <!-- -->(<span class="lg-highlight-params">state</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">return</span> <<!-- -->finalState<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</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="#std-label-accumulator-lang" target="_self">
<span>
lang
</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-accumulator-lang">
</span>
<p class="leafygreen-ui-1kp3ins">
The language used in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
code.
</p>
<p class="leafygreen-ui-1kp3ins">
<strong class="css-rws738">
IMPORTANT:
</strong>
Currently, the only supported value for
<code class="leafygreen-ui-1l06pbn">
lang
</code>
is
<code class="leafygreen-ui-1l06pbn">
js
</code>
.
</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">
The following steps outline how the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
operator
processes documents:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The operator begins at an initial state, defined by the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-init" target="_self">
<span>
init
</span>
</a>
function.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For each document, the operator updates
the state based on the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-accumulate" target="_self">
<span>
accumulate
</span>
</a>
function. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-accumulate" target="_self">
<span>
accumulate
</span>
</a>
function's
first argument is the current state, and additional arguments are be
specified in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-accumulateArgs" target="_self">
<span>
accumulateArgs
</span>
</a>
array.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
When the operator needs to merge multiple intermediate states, it
executes the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-merge" target="_self">
<span>
merge
</span>
</a>
function. For more
information on when the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-merge" target="_self">
<span>
merge
</span>
</a>
function is
called, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-merge-multiple-states" target="_self">
<span>
Merge Two States with
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
.
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-finalize" target="_self">
<span>
finalize
</span>
</a>
function has been
defined, once all documents have been processed and the state has
been updated accordingly,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-finalize" target="_self">
<span>
finalize
</span>
</a>
converts the state to a final output.
</p>
</li>
</ol>
<span class="leafygreen-ui-1qnjd6d" id="std-label-merge-multiple-states">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Merge Two States with
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#merge-two-states-with--merge" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="merge-two-states-with--merge">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
As part of its internal operations, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
operator
may need to merge two separate, intermediate states. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-merge" target="_self">
<span>
merge
</span>
</a>
function specifies how the operator should merge
two states.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-merge" target="_self">
<span>
merge
</span>
</a>
function always merges two
states at a time. In the event that more than two states must be merged,
the resulting merge of two states is merged with a single state. This
process repeats until all states are merged.
</p>
<p class="leafygreen-ui-1kp3ins">
For example,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
may need to combine two states in the
following scenarios:
</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-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
is run on a sharded cluster. The operator
needs to merge the results from each shard to obtain the final
result.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A single
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
operation exceeds its specified
memory limit. If you specify the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.aggregate/#mongodb-method-db.collection.aggregate">
<code class="leafygreen-ui-1l06pbn">
allowDiskUse
</code>
</a>
option, the operator stores the
in-progress operation on disk and finishes the operation in memory.
Once the operation finishes, the results from disk and memory are
merged together using the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-merge" target="_self">
<span>
merge
</span>
</a>
function.
</p>
</li>
</ul>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Document Processing Order
<a class="headerlink leafygreen-ui-1kepx47" href="#document-processing-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="document-processing-order">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The order that MongoDB processes documents for the
<code class="leafygreen-ui-1l06pbn">
init()
</code>
,
<code class="leafygreen-ui-1l06pbn">
accumulate()
</code>
, and
<code class="leafygreen-ui-1l06pbn">
merge()
</code>
functions can vary, and might differ
from the order that those documents are specified to the
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
function.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, consider a series of documents where the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
fields are
the letters of the alphabet:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ _id: 'a' },\n{ _id: 'b' },\n{ _id: 'c' }\n...\n{ _id: 'z' }"}
</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">{ _id: 'a' },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ _id: 'b' },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ _id: 'c' }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">...</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ _id: 'z' }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Next, consider an aggregation pipeline that sorts the documents by the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field and then uses an
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
function to concatenate
the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field values:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n $sort: { _id: 1 }\n },\n {\n $group: {\n _id: null,\n alphabet: {\n $accumulator: {\n init: function() {\n return \"\"\n },\n accumulate: function(state, letter) {\n return(state + letter)\n },\n accumulateArgs: [ \"$_id\" ],\n merge: function(state1, state2) {\n return(state1 + state2)\n },\n lang: \"js\"\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">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$sort</span>: <!-- -->{ <span class="lg-highlight-attr">_id</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">$group</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-literal">null</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">alphabet</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$accumulator</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">init</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<!-- -->)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">return</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-attr">accumulate</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">state, letter</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">return</span>(<!-- -->state <!-- -->+ <!-- -->letter)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">accumulateArgs</span>: <!-- -->[ <span class="lg-highlight-string">"$_id"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">merge</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">state1, state2</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">return</span>(<!-- -->state1 <!-- -->+ <!-- -->state2)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">lang</span>: <span class="lg-highlight-string">"js"</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">
MongoDB does not guarantee that the documents are processed in the
sorted order, meaning the
<code class="leafygreen-ui-1l06pbn">
alphabet
</code>
field does not necessarily get
set to
<code class="leafygreen-ui-1l06pbn">
abc...z
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
Due to this behavior, ensure that your
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
function does
not need to process and return documents in a specific order.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Javascript Enabled
<a class="headerlink leafygreen-ui-1kepx47" href="#javascript-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="javascript-enabled">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
, you must have server-side scripting
enabled.
</p>
<p class="leafygreen-ui-1kp3ins">
If you do not use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
(or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/function/#mongodb-expression-exp.-function">
<code class="leafygreen-ui-1l06pbn">
$function
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/where/#mongodb-query-op.-where">
<code class="leafygreen-ui-1l06pbn">
$where
</code>
</a>
, or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mapReduce/#mongodb-dbcommand-dbcmd.mapReduce">
<code class="leafygreen-ui-1l06pbn">
mapReduce
</code>
</a>
), disable server-side
scripting:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For 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, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.javascriptEnabled">
<code class="leafygreen-ui-1l06pbn">
security.javascriptEnabled
</code>
</a>
configuration option or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--noscripting">
<code class="leafygreen-ui-1l06pbn">
--noscripting
</code>
</a>
command-line option.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
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>
instance, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.javascriptEnabled">
<code class="leafygreen-ui-1l06pbn">
security.javascriptEnabled
</code>
</a>
configuration option or the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#std-option-mongos.--noscripting">
<code class="leafygreen-ui-1l06pbn">
--noscripting
</code>
</a>
command-line option.
</p>
<div class="line-block">
<div class="line">
In earlier versions, MongoDB does not allow JavaScript execution on
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances.
</div>
</div>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
See also
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/security-checklist/#std-label-security-checklist-javascript">
➤
<!-- -->
Run MongoDB with Secure Configuration Options
<!-- -->
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Unsupported Array and String Functions
<a class="headerlink leafygreen-ui-1kepx47" href="#unsupported-array-and-string-functions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="unsupported-array-and-string-functions">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB 6.0 upgrades the internal JavaScript engine used for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/server-side-javascript/#std-label-server-side-javascript">
server-side JavaScript
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/function/#mongodb-expression-exp.-function">
<code class="leafygreen-ui-1l06pbn">
$function
</code>
</a>
, and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/where/#mongodb-query-op.-where">
<code class="leafygreen-ui-1l06pbn">
$where
</code>
</a>
expressions and from MozJS-60 to MozJS-91. Several deprecated,
non-standard array and string functions that existed in MozJS-60 are
removed in MozJS-91.
</p>
<p class="leafygreen-ui-1kp3ins">
For the complete list of removed array and string functions, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/release-notes/6.0-compatibility/#std-label-6.0-js-engine-change">
6.0 compatibility notes
<!-- -->
.
</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">
Use
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
to Implement the
<code class="leafygreen-ui-1l06pbn">
$avg
</code>
Operator
<a class="headerlink leafygreen-ui-1kepx47" href="#use--accumulator-to-implement-the--avg-operator" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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--accumulator-to-implement-the--avg-operator">
</div>
</a>
</h3>
<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 example walks through using the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
operator
to implement the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/avg/#mongodb-group-grp.-avg">
<code class="leafygreen-ui-1l06pbn">
$avg
</code>
</a>
operator, which is already supported
by MongoDB. The goal of this example is not to implement new
functionality, but to illustrate the behavior and syntax of the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
operator with familiar logic.
</p>
</div>
</div>
<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>
, create a sample collection named
<code class="leafygreen-ui-1l06pbn">
books
</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.books.insertMany([\n { \"_id\" : 8751, \"title\" : \"The Banquet\", \"author\" : \"Dante\", \"copies\" : 2 },\n { \"_id\" : 8752, \"title\" : \"Divine Comedy\", \"author\" : \"Dante\", \"copies\" : 1 },\n { \"_id\" : 8645, \"title\" : \"Eclogues\", \"author\" : \"Dante\", \"copies\" : 2 },\n { \"_id\" : 7000, \"title\" : \"The Odyssey\", \"author\" : \"Homer\", \"copies\" : 10 },\n { \"_id\" : 7020, \"title\" : \"Iliad\", \"author\" : \"Homer\", \"copies\" : 10 }\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">insertMany</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">8751</span>, <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"The Banquet"</span>, <span class="lg-highlight-string">"author"</span> <!-- -->: <span class="lg-highlight-string">"Dante"</span>, <span class="lg-highlight-string">"copies"</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">8752</span>, <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"Divine Comedy"</span>, <span class="lg-highlight-string">"author"</span> <!-- -->: <span class="lg-highlight-string">"Dante"</span>, <span class="lg-highlight-string">"copies"</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">8645</span>, <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"Eclogues"</span>, <span class="lg-highlight-string">"author"</span> <!-- -->: <span class="lg-highlight-string">"Dante"</span>, <span class="lg-highlight-string">"copies"</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">7000</span>, <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"The Odyssey"</span>, <span class="lg-highlight-string">"author"</span> <!-- -->: <span class="lg-highlight-string">"Homer"</span>, <span class="lg-highlight-string">"copies"</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">7020</span>, <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"Iliad"</span>, <span class="lg-highlight-string">"author"</span> <!-- -->: <span class="lg-highlight-string">"Homer"</span>, <span class="lg-highlight-string">"copies"</span> <!-- -->: <span class="lg-highlight-number">10</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
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
groups
</code>
</a>
the documents by
<code class="leafygreen-ui-1l06pbn">
author
</code>
, and uses
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
to compute the average
number of copies across books for each author:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.books.aggregate([\n{\n $group :\n {\n _id : \"$author\",\n avgCopies:\n {\n $accumulator:\n {\n init: function() { // Set the initial state\n return { count: 0, sum: 0 }\n },\n\n accumulate: function(state, numCopies) { // Define how to update the state\n return {\n count: state.count + 1,\n sum: state.sum + numCopies\n }\n },\n\n accumulateArgs: [\"$copies\"], // Argument required by the accumulate function\n\n merge: function(state1, state2) { // When the operator performs a merge,\n return { // add the fields from the two states\n count: state1.count + state2.count,\n sum: state1.sum + state2.sum\n }\n },\n\n finalize: function(state) { // After collecting the results from all documents,\n return (state.sum / state.count) // calculate the average\n },\n lang: \"js\"\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">books</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"> <!-- -->$group<!-- --> <!-- -->:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->_id <!-- -->: <span class="lg-highlight-string">"$author"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">avgCopies</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$accumulator</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">init</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<!-- -->)<!-- --> <!-- -->{ <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Set the initial </span><span class="lg-highlight-comment">state</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">return</span> <!-- -->{ <span class="lg-highlight-attr">count</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">sum</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"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">accumulate</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">state, numCopies</span>)<!-- --> <!-- -->{ <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Define how to </span><span class="lg-highlight-comment">update the state</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">return</span> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">count</span>: <!-- -->state.<span class="lg-highlight-property">count</span> <!-- -->+ <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sum</span>: <!-- -->state.<span class="lg-highlight-property">sum</span> <!-- -->+ <!-- -->numCopies</td></tr><tr 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-attr">accumulateArgs</span>: <!-- -->[<span class="lg-highlight-string">"$copies"</span>]<!-- -->, <span class="lg-highlight-comment">// Argument required by the accumulate 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-attr">merge</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">state1, state2</span>)<!-- --> <!-- -->{ <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> When the operator </span><span class="lg-highlight-comment">performs a merge,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">return</span> <!-- -->{ <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> add the fields </span><span class="lg-highlight-comment">from the two states</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">count</span>: <!-- -->state1.<span class="lg-highlight-property">count</span> <!-- -->+ <!-- -->state2.<span class="lg-highlight-property">count</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sum</span>: <!-- -->state1.<span class="lg-highlight-property">sum</span> <!-- -->+ <!-- -->state2.<span class="lg-highlight-property">sum</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-attr">finalize</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">state</span>)<!-- --> <!-- -->{ <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> After collecting the </span><span class="lg-highlight-comment">results from all documents,</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">return</span> <!-- -->(<!-- -->state.<span class="lg-highlight-property">sum</span> <!-- -->/ <!-- -->state.<span class="lg-highlight-property">count</span>) <span class="lg-highlight-comment">// calculate the average</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">lang</span>: <span class="lg-highlight-string">"js"</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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Result
<a class="headerlink leafygreen-ui-1kepx47" href="#result" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="result">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
This operation returns the following result:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : \"Dante\", \"avgCopies\" : 1.6666666666666667 }\n{ \"_id\" : \"Homer\", \"avgCopies\" : 10 }","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">"Dante"</span>, <span class="lg-highlight-string">"avgCopies"</span> <!-- -->: <span class="lg-highlight-number">1.6666666666666667</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Homer"</span>, <span class="lg-highlight-string">"avgCopies"</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>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior-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="behavior-1">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
defines an initial state where
<code class="leafygreen-ui-1l06pbn">
count
</code>
and
<code class="leafygreen-ui-1l06pbn">
sum
</code>
are both set to
<code class="leafygreen-ui-1l06pbn">
0
</code>
. For each document that the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
processes, it updates the state by:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Incrementing the
<code class="leafygreen-ui-1l06pbn">
count
</code>
by 1 and
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Adding the values of the document's
<code class="leafygreen-ui-1l06pbn">
copies
</code>
field to the
<code class="leafygreen-ui-1l06pbn">
sum
</code>
.
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-accumulate" target="_self">
<span>
accumulate
</span>
</a>
function can access the
<code class="leafygreen-ui-1l06pbn">
copies
</code>
field because it is passed in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-accumulateArgs" target="_self">
<span>
accumulateArgs
</span>
</a>
field.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
With each document that is processed, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-accumulate" target="_self">
<span>
accumulate
</span>
</a>
function returns the updated
state.
</p>
<p class="leafygreen-ui-1kp3ins">
Once all documents have been processed, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-finalize" target="_self">
<span>
finalize
</span>
</a>
function divides the
<code class="leafygreen-ui-1l06pbn">
sum
</code>
of
the copies by the
<code class="leafygreen-ui-1l06pbn">
count
</code>
of documents to obtain the average. This
removes the need to keep a running computed average, since the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-finalize" target="_self">
<span>
finalize
</span>
</a>
function receives the cumulative
<code class="leafygreen-ui-1l06pbn">
sum
</code>
and
<code class="leafygreen-ui-1l06pbn">
count
</code>
of all documents.
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Comparison with
<code class="leafygreen-ui-1l06pbn">
$avg
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#comparison-with--avg" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-with--avg">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
This operation is equivalent to the following pipeline, which uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/avg/#mongodb-group-grp.-avg">
<code class="leafygreen-ui-1l06pbn">
$avg
</code>
</a>
operator:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.books.aggregate([\n{\n $group : {\n _id : \"$author\",\n avgCopies: { $avg: \"$copies\" }\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">books</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"> <!-- -->$group<!-- --> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->_id <!-- -->: <span class="lg-highlight-string">"$author"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">avgCopies</span>: <!-- -->{ <span class="lg-highlight-attr">$avg</span>: <span class="lg-highlight-string">"$copies"</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>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Use
<code class="leafygreen-ui-1l06pbn">
initArgs
</code>
to Vary the Initial State by Group
<a class="headerlink leafygreen-ui-1kepx47" href="#use-initargs-to-vary-the-initial-state-by-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="use-initargs-to-vary-the-initial-state-by-group">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-initArgs" target="_self">
<span>
initArgs
</span>
</a>
option in
to vary the initial state of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
. This can be
useful if you want to, for example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use the value of a field which is not in your state to affect your
state, or
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Set the initial state to a different value based on the group being
processed.
</p>
</li>
</ul>
<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>
, create a sample collection named
<code class="leafygreen-ui-1l06pbn">
restaurants
</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.restaurants.insertMany([\n { \"_id\" : 1, \"name\" : \"Food Fury\", \"city\" : \"Bettles\", \"cuisine\" : \"American\" },\n { \"_id\" : 2, \"name\" : \"Meal Macro\", \"city\" : \"Bettles\", \"cuisine\" : \"Chinese\" },\n { \"_id\" : 3, \"name\" : \"Big Crisp\", \"city\" : \"Bettles\", \"cuisine\" : \"Latin\" },\n { \"_id\" : 4, \"name\" : \"The Wrap\", \"city\" : \"Onida\", \"cuisine\" : \"American\" },\n { \"_id\" : 5, \"name\" : \"Spice Attack\", \"city\" : \"Onida\", \"cuisine\" : \"Latin\" },\n { \"_id\" : 6, \"name\" : \"Soup City\", \"city\" : \"Onida\", \"cuisine\" : \"Chinese\" },\n { \"_id\" : 7, \"name\" : \"Crave\", \"city\" : \"Pyote\", \"cuisine\" : \"American\" },\n { \"_id\" : 8, \"name\" : \"The Gala\", \"city\" : \"Pyote\", \"cuisine\" : \"Chinese\" }\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">restaurants</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">"Food Fury"</span>, <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Bettles"</span>, <span class="lg-highlight-string">"cuisine"</span> <!-- -->: <span class="lg-highlight-string">"American"</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">"Meal Macro"</span>, <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Bettles"</span>, <span class="lg-highlight-string">"cuisine"</span> <!-- -->: <span class="lg-highlight-string">"Chinese"</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">"Big Crisp"</span>, <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Bettles"</span>, <span class="lg-highlight-string">"cuisine"</span> <!-- -->: <span class="lg-highlight-string">"Latin"</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">"The Wrap"</span>, <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Onida"</span>, <span class="lg-highlight-string">"cuisine"</span> <!-- -->: <span class="lg-highlight-string">"American"</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">"Spice Attack"</span>, <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Onida"</span>, <span class="lg-highlight-string">"cuisine"</span> <!-- -->: <span class="lg-highlight-string">"Latin"</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">"Soup City"</span>, <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Onida"</span>, <span class="lg-highlight-string">"cuisine"</span> <!-- -->: <span class="lg-highlight-string">"Chinese"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">7</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Crave"</span>, <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Pyote"</span>, <span class="lg-highlight-string">"cuisine"</span> <!-- -->: <span class="lg-highlight-string">"American"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">8</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"The Gala"</span>, <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Pyote"</span>, <span class="lg-highlight-string">"cuisine"</span> <!-- -->: <span class="lg-highlight-string">"Chinese"</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">
Suppose an application allows users to query this data to find
restaurants. It may be useful to show more results for
the city where the user lives. For this example, we assume that the
user's city is called in a variable called
<code class="leafygreen-ui-1l06pbn">
userProfileCity
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
The following aggregation pipeline
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
groups
</code>
</a>
the
documents by
<code class="leafygreen-ui-1l06pbn">
city
</code>
. The operation uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
to display a different number of results from each city depending on
whether the restaurant's city matches the city in the user's profile:
</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 execute this example 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>
, replace
<code class="leafygreen-ui-1l06pbn">
<userProfileCity>
</code>
in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-initArgs" target="_self">
<span>
initArgs
</span>
</a>
with a string containing an actual city value, such as
<code class="leafygreen-ui-1l06pbn">
Bettles
</code>
.
</p>
</div>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.restaurants.aggregate([\n{\n $group :\n {\n _id : { city: \"$city\" },\n restaurants:\n {\n $accumulator:\n {\n init: function(city, userProfileCity) { // Set the initial state\n return {\n max: city === userProfileCity ? 3 : 1, // If the group matches the user's city, return 3 restaurants\n restaurants: [] // else, return 1 restaurant\n }\n },\n\n initArgs: [\"$city\", <userprofilecity>], // Argument to pass to the init function\n\n accumulate: function(state, restaurantName) { // Define how to update the state\n if (state.restaurants.length < state.max) {\n state.restaurants.push(restaurantName);\n }\n return state;\n },\n\n accumulateArgs: [\"$name\"], // Argument required by the accumulate function\n\n merge: function(state1, state2) {\n return {\n max: state1.max,\n restaurants: state1.restaurants.concat(state2.restaurants).slice(0, state1.max)\n }\n },\n\n finalize: function(state) { // Adjust the state to only return field we need\n return state.restaurants\n }\n\n lang: \"js\"\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-ukaj9n">1</td><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">restaurants</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>(<!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">2</td><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">3</td><td class="leafygreen-ui-7razhx"> <!-- -->$group<!-- --> <!-- -->:</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">4</td><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">5</td><td class="leafygreen-ui-7razhx"> <!-- -->_id <!-- -->: <!-- -->{ <span class="lg-highlight-attr">city</span>: <span class="lg-highlight-string">"$city"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">6</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">restaurants</span>:</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">7</td><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">8</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$accumulator</span>:</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">9</td><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">10</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">init</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">city, userProfileCity</span>)<!-- --> <!-- -->{ <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Set the initial </span><span class="lg-highlight-comment">state</span></td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">11</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">return</span> <!-- -->{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-z611p6">12</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">max</span>: <!-- -->city <!-- -->=== <!-- -->userProfileCity <!-- -->? <span class="lg-highlight-number">3</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-comment">// If</span><span class="lg-highlight-comment"> the group matches </span><span class="lg-highlight-comment">the user's city, return 3 restaurants</span></td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">13</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">restaurants</span>: <!-- -->[<!-- -->] <span class="lg-highlight-comment">// else, return 1 restaurant</span></td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">14</td><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">15</td><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">16</td><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">17</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">initArgs</span>: <!-- -->[<span class="lg-highlight-string">"$city"</span>, <<!-- -->userProfileCity<!-- -->><!-- -->]<!-- -->, <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Argument to pass </span><span class="lg-highlight-comment">to the init function</span></td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">18</td><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">19</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">accumulate</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">state, restaurantName</span>)<!-- --> <!-- -->{ <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Define how to </span><span class="lg-highlight-comment">update the state</span></td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">20</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">if</span> <!-- -->(<!-- -->state.<span class="lg-highlight-property">restaurants</span>.<span class="lg-highlight-property">length</span> <!-- -->< <!-- -->state.<span class="lg-highlight-property">max</span>) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">21</td><td class="leafygreen-ui-7razhx"> <!-- -->state.<span class="lg-highlight-property">restaurants</span>.<span class="lg-highlight-title lg-highlight-function">push</span>(<!-- -->restaurantName)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">22</td><td class="leafygreen-ui-7razhx"> <!-- -->}</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">23</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">return</span> <!-- -->state<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">24</td><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">25</td><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">26</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">accumulateArgs</span>: <!-- -->[<span class="lg-highlight-string">"$name"</span>]<!-- -->, <span class="lg-highlight-comment">// Argument required by the accumulate function</span></td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">27</td><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">28</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">merge</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">state1, state2</span>)<!-- --> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">29</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">return</span> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">30</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">max</span>: <!-- -->state1.<span class="lg-highlight-property">max</span>,</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">31</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">restaurants</span>: <!-- -->state1.<span class="lg-highlight-property">restaurants</span>.<span class="lg-highlight-title lg-highlight-function">concat</span>(<!-- -->state2.<span class="lg-highlight-property">restaurants</span>).<span class="lg-highlight-title lg-highlight-function">slice</span>(<span class="lg-highlight-number">0</span>, <!-- -->state1.<span class="lg-highlight-property">max</span>)</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">32</td><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">33</td><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">34</td><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">35</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">finalize</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">state</span>)<!-- --> <!-- -->{ <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Adjust the state </span><span class="lg-highlight-comment">to</span><span class="lg-highlight-comment"> only return field </span><span class="lg-highlight-comment">we need</span></td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">36</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">return</span> <!-- -->state.<span class="lg-highlight-property">restaurants</span></td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">37</td><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">38</td><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">39</td><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">lang</span>: <span class="lg-highlight-string">"js"</span></td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">40</td><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">41</td><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">42</td><td class="leafygreen-ui-7razhx"> }</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">43</td><td class="leafygreen-ui-7razhx">}</td></tr><tr class=""><td class="leafygreen-ui-ukaj9n">44</td><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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Results
<a class="headerlink leafygreen-ui-1kepx47" href="#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="results">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
If the value of
<code class="leafygreen-ui-1l06pbn">
userProfileCity
</code>
is
<code class="leafygreen-ui-1l06pbn">
Bettles
</code>
, this operation
returns the following result:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : { \"city\" : \"Bettles\" }, \"restaurants\" : { \"restaurants\" : [ \"Food Fury\", \"Meal Macro\", \"Big Crisp\" ] } }\n{ \"_id\" : { \"city\" : \"Onida\" }, \"restaurants\" : { \"restaurants\" : [ \"The Wrap\" ] } }\n{ \"_id\" : { \"city\" : \"Pyote\" }, \"restaurants\" : { \"restaurants\" : [ \"Crave\" ] } }","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-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Bettles"</span> }<!-- -->, <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Food Fury"</span>, <span class="lg-highlight-string">"Meal Macro"</span>, <span class="lg-highlight-string">"Big Crisp"</span> ] } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Onida"</span> }<!-- -->, <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"The Wrap"</span> ] } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Pyote"</span> }<!-- -->, <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Crave"</span> ] } }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If the value of
<code class="leafygreen-ui-1l06pbn">
userProfileCity
</code>
is
<code class="leafygreen-ui-1l06pbn">
Onida
</code>
, this operation
returns the following result:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : { \"city\" : \"Bettles\" }, \"restaurants\" : { \"restaurants\" : [ \"Food Fury\" ] } }\n{ \"_id\" : { \"city\" : \"Onida\" }, \"restaurants\" : { \"restaurants\" : [ \"The Wrap\", \"Spice Attack\", \"Soup City\" ] } }\n{ \"_id\" : { \"city\" : \"Pyote\" }, \"restaurants\" : { \"restaurants\" : [ \"Crave\" ] } }","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-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Bettles"</span> }<!-- -->, <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Food Fury"</span> ] } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Onida"</span> }<!-- -->, <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"The Wrap"</span>, <span class="lg-highlight-string">"Spice Attack"</span>, <span class="lg-highlight-string">"Soup City"</span> ] } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Pyote"</span> }<!-- -->, <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Crave"</span> ] } }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If the value of
<code class="leafygreen-ui-1l06pbn">
userProfileCity
</code>
is
<code class="leafygreen-ui-1l06pbn">
Pyote
</code>
, this operation
returns the following result:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : { \"city\" : \"Bettles\" }, \"restaurants\" : { \"restaurants\" : [ \"Food Fury\" ] } }\n{ \"_id\" : { \"city\" : \"Onida\" }, \"restaurants\" : { \"restaurants\" : [ \"The Wrap\" ] } }\n{ \"_id\" : { \"city\" : \"Pyote\" }, \"restaurants\" : { \"restaurants\" : [ \"Crave\", \"The Gala\" ] } }","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-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Bettles"</span> }<!-- -->, <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Food Fury"</span> ] } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Onida"</span> }<!-- -->, <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"The Wrap"</span> ] } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Pyote"</span> }<!-- -->, <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Crave"</span>, <span class="lg-highlight-string">"The Gala"</span> ] } }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If the value of
<code class="leafygreen-ui-1l06pbn">
userProfileCity
</code>
is any other value, this operation
returns the following result:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : { \"city\" : \"Bettles\" }, \"restaurants\" : { \"restaurants\" : [ \"Food Fury\" ] } }\n{ \"_id\" : { \"city\" : \"Onida\" }, \"restaurants\" : { \"restaurants\" : [ \"The Wrap\" ] } }\n{ \"_id\" : { \"city\" : \"Pyote\" }, \"restaurants\" : { \"restaurants\" : [ \"Crave\" ] } }","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-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Bettles"</span> }<!-- -->, <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Food Fury"</span> ] } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Onida"</span> }<!-- -->, <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"The Wrap"</span> ] } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"city"</span> <!-- -->: <span class="lg-highlight-string">"Pyote"</span> }<!-- -->, <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"restaurants"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Crave"</span> ] } }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#behavior-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="behavior-2">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-init" target="_self">
<span>
init
</span>
</a>
function defines an initial state
containing
<code class="leafygreen-ui-1l06pbn">
max
</code>
and
<code class="leafygreen-ui-1l06pbn">
restaurants
</code>
fields. The
<code class="leafygreen-ui-1l06pbn">
max
</code>
field sets
the maximum number of restaurants for that particular group. If the
document's
<code class="leafygreen-ui-1l06pbn">
city
</code>
field matches
<code class="leafygreen-ui-1l06pbn">
userProfileCity
</code>
, that group
contains a maximum of 3 restaurants. Otherwise, if the document
<code class="leafygreen-ui-1l06pbn">
_id
</code>
does not match
<code class="leafygreen-ui-1l06pbn">
userProfileCity
</code>
, the group contains at most a single
restaurant. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-init" target="_self">
<span>
init
</span>
</a>
function receives both
the
<code class="leafygreen-ui-1l06pbn">
city
</code>
<code class="leafygreen-ui-1l06pbn">
userProfileCity
</code>
arguments from the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-initArgs" target="_self">
<span>
initArgs
</span>
</a>
array.
</p>
<p class="leafygreen-ui-1kp3ins">
For each document that the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-accumulator" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</span>
</a>
processes, it pushes
the
<code class="leafygreen-ui-1l06pbn">
name
</code>
of the restaurant to the
<code class="leafygreen-ui-1l06pbn">
restaurants
</code>
array, provided
that name would not put the length of
<code class="leafygreen-ui-1l06pbn">
restaurants
</code>
over the
<code class="leafygreen-ui-1l06pbn">
max
</code>
value. With each document that is processed, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-accumulate" target="_self">
<span>
accumulate
</span>
</a>
function returns the updated state.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-merge" target="_self">
<span>
merge
</span>
</a>
function defines how to merge two
states. The function concatenates the
<code class="leafygreen-ui-1l06pbn">
restaurant
</code>
arrays from each
state together, and the length of the resulting array is limited using
the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice" rel="noopener noreferrer">
<span>
slice()
</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 ensure that it does not exceed the
<code class="leafygreen-ui-1l06pbn">
max
</code>
value.
</p>
<p class="leafygreen-ui-1kp3ins">
Once all documents have been processed, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-accumulator-finalize" target="_self">
<span>
finalize
</span>
</a>
function modifies the resulting state to only
return the names of the restaurants. Without this function, the
<code class="leafygreen-ui-1l06pbn">
max
</code>
field would also be included in the output, which does not fulfill any
needs for the application.
</p>
</section>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/sh.enableSharding/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
sh.enableSharding()
<a class="headerlink leafygreen-ui-1kepx47" href="#sh.enablesharding--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.enablesharding--">
</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>
</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.enableSharding(database, primaryShard)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-sh.enableSharding" 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.enableSharding">
</div>
</a>
</dt>
<dd>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0, this method is
<strong>
not
</strong>
required to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/sharding/#std-label-sharding-background">
shard
</a>
a collection.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Explicitly creates a database. 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>
method
<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>
to shard collections on the database.
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="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.enableSharding" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.enableSharding()
</code>
</span>
</a>
wraps the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/enableSharding/#mongodb-dbcommand-dbcmd.enableSharding">
<code class="leafygreen-ui-1l06pbn">
enableSharding
</code>
</a>
command.
</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/enableSharding/#mongodb-dbcommand-dbcmd.enableSharding">
<code class="leafygreen-ui-1l06pbn">
enableSharding
</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-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">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.enableSharding" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.enableSharding()
</code>
</span>
</a>
has the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.enableSharding(\n <database>,\n <primary> // Optional.\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">sh.<span class="lg-highlight-title lg-highlight-function">enableSharding</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <<!-- -->database<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-tag lg-highlight-xml"><</span><span class="lg-highlight-name lg-highlight-tag lg-highlight-xml">primary</span><span class="lg-highlight-tag lg-highlight-xml"> </span><span class="lg-highlight-attr lg-highlight-tag lg-highlight-xml">shard</span><span class="lg-highlight-tag lg-highlight-xml">></span><span class="lg-highlight-xml"> // Optional.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-xml">)</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>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Parameter
<a class="headerlink leafygreen-ui-1kepx47" href="#parameter" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="parameter">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.enableSharding" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.enableSharding()
</code>
</span>
</a>
method takes 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: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>
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-method-enableSharding-db" target="_self">
<span>
database
</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-method-enableSharding-db">
</span>
<p class="leafygreen-ui-1kp3ins">
The name of the database you want to create.
</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-method-enableSharding-primaryShard" target="_self">
<span>
primaryShard
</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-method-enableSharding-primaryShard">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-shards/#std-label-primary-shard">
primary shard
</a>
for the
database. It's the default shard for all unsharded collections
in the database.
</p>
<p class="leafygreen-ui-1kp3ins">
<strong class="css-4dei7l">
WARNING:
</strong>
In general, you should
<strong>
not
</strong>
specify the
primary shard. Allow the cluster to select the primary shard
instead.
</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>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Write Concern
<a class="headerlink leafygreen-ui-1kepx47" href="#write-concern" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="write-concern">
</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>
uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
for the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/enableSharding/#mongodb-dbcommand-dbcmd.enableSharding">
<code class="leafygreen-ui-1l06pbn">
enableSharding
</code>
</a>
command and its helper
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.enableSharding" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.enableSharding()
</code>
.
</span>
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Primary Shard
<a class="headerlink leafygreen-ui-1kepx47" href="#primary-shard" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="primary-shard">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
In general, you should not need to specify the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-method-enableSharding-primaryShard" target="_self">
<span>
primaryShard
</span>
</a>
in the command. Instead, allow the
balancer to select the primary shard.
</p>
<p class="leafygreen-ui-1kp3ins">
However, if you do specify the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-method-enableSharding-primaryShard" target="_self">
<span>
primary shard
</span>
</a>
for a database and the database
is already enabled for sharding with a different primary shard, the
operation returns an error and the primary shard for the database
remains as before. To change the primary shard for a database, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/movePrimary/#mongodb-dbcommand-dbcmd.movePrimary">
<code class="leafygreen-ui-1l06pbn">
movePrimary
</code>
</a>
command instead.
</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 example, run from a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
, explicitly
creates the
<code class="leafygreen-ui-1l06pbn">
shardTest
</code>
database:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.enableSharding(\"shardTest\")","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">enableSharding</span>(<span class="lg-highlight-string">"shardTest"</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 verify, 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>
</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/sh.shardCollection/#mongodb-method-sh.shardCollection">
<code class="leafygreen-ui-1l06pbn">
sh.shardCollection()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/deploy-shard-cluster/">
Deploy a Self-Managed Sharded Cluster
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/read-preference-mechanics/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Server Selection Algorithm
<a class="headerlink leafygreen-ui-1kepx47" href="#server-selection-algorithm" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="server-selection-algorithm">
</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="#read-preference-for-replica-sets" target="_self">
<span>
Read Preference for Replica Sets
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#read-preference-for-sharded-clusters" target="_self">
<span>
Read Preference for Sharded Clusters
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
MongoDB drivers use a Server Selection algorithm to choose which
replica set member to use or, when connected to multiple
<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
<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 use.
</p>
<p class="leafygreen-ui-1kp3ins">
Server selection occurs once per operation.
</p>
<p class="leafygreen-ui-1kp3ins">
The information on this page applies to 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>
<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/transactions/#std-label-transactions">
Distributed transactions
</a>
that contain
read operations must use read preference
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#mongodb-readmode-primary">
<code class="leafygreen-ui-1l06pbn">
primary
</code>
</a>
. All
operations in a given transaction must route to the same member.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-replica-set-read-preference-behavior-ping-time">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-replica-set-read-preference-behavior-nearest">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-replica-set-read-preference-behavior-member-selection">
</span>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Read Preference for Replica Sets
<a class="headerlink leafygreen-ui-1kepx47" href="#read-preference-for-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="read-preference-for-replica-sets">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Server selection occurs once per operation and is governed by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#std-label-read-preference">
read preference
</a>
and
<code class="leafygreen-ui-1l06pbn">
localThresholdMS
</code>
settings to determine member eligibility for reads. The read preference
is re-evaluated for each operation.
</p>
<div class="header-buffer" id="ref-default-threshold-id1">
</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>
Read Preference Mode
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Selection Process
</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/core/read-preference/#mongodb-readmode-primary">
<code class="leafygreen-ui-1l06pbn">
primary
</code>
</a>
(Default)
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The driver selects the primary.
</p>
</li>
</ol>
</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/core/read-preference/#mongodb-readmode-secondary">
<code class="leafygreen-ui-1l06pbn">
secondary
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The driver assembles a list of eligible secondary members.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference-staleness/#std-label-replica-set-read-preference-max-staleness">
maxStalenessSeconds
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/configure-replica-set-tag-sets/">
tag
sets
</a>
specified
in the read preference can further restrict the
eligibility of the members.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the list of eligible members is not empty, the driver
determines which eligible member is the "closest" (i.e. the
member with the lowest average network round-trip-time) and
calculates a latency window by adding the average
round-trip-time of this "closest" server and the
<code class="leafygreen-ui-1l06pbn">
localThresholdMS
</code>
. The driver uses this latency window to
pare down the list of eligible members to those members that
fall within this window.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
From this list of eligible members that fall within the
latency window, the driver randomly chooses an eligible member.
</p>
</li>
</ol>
</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/core/read-preference/#mongodb-readmode-nearest">
<code class="leafygreen-ui-1l06pbn">
nearest
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The driver assembles a list of eligible members (primary and
secondaries).
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference-staleness/#std-label-replica-set-read-preference-max-staleness">
maxStalenessSeconds
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/configure-replica-set-tag-sets/">
tag sets
</a>
specified
in the read preference can further limit the eligibility
of the members.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the list of eligible members is not empty, the driver
determines which eligible member is the "closest" (i.e. the
member with the lowest average network round-trip-time) and
calculates a latency window by adding the average
round-trip-time of this "closest" server and the
<code class="leafygreen-ui-1l06pbn">
localThresholdMS
</code>
<a class="footnote-reference header-buffer" href="#footnote-default-threshold" id="ref-default-threshold-id1">
[
<!-- -->
1
<!-- -->
]
</a>
. The driver uses
this latency window to pare down the list of eligible members
to those members that fall within this window.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
From this list of eligible members that fall within the
latency window, the driver randomly chooses an eligible member.
</p>
</li>
</ol>
</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/core/read-preference/#mongodb-readmode-primaryPreferred">
<code class="leafygreen-ui-1l06pbn">
primaryPreferred
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the primary is available, driver selects the primary.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the primary is unavailable, server selection follows
the process for the read preference
<code class="leafygreen-ui-1l06pbn">
secondary
</code>
to select
an eligible secondary member.
</p>
</li>
</ol>
</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/core/read-preference/#mongodb-readmode-secondaryPreferred">
<code class="leafygreen-ui-1l06pbn">
secondaryPreferred
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Following the server selection process for the read
preference
<code class="leafygreen-ui-1l06pbn">
secondary
</code>
, if a list of eligible secondary
members is non-empty, driver chooses an eligible secondary member.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Otherwise, if the list is empty, driver selects the primary.
</p>
</li>
</ol>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-replica-set-read-preference-behavior-sharding">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-replica-set-read-preference-behavior-mongos">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Read Preference for Sharded Clusters
<a class="headerlink leafygreen-ui-1kepx47" href="#read-preference-for-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="read-preference-for-sharded-clusters">
</div>
</a>
</h2>
<span class="leafygreen-ui-1qnjd6d" id="std-label-read-preference-mechanics-multiple-mongos">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Load Balancing
<a class="headerlink leafygreen-ui-1kepx47" href="#load-balancing" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="load-balancing">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If there is more than one
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instance in the connection
seed list, the driver determines which
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
is the
"closest" (i.e. the member with the lowest average network
round-trip-time) and calculates the latency window by adding the
average round-trip-time of this "closest"
<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 the
<code class="leafygreen-ui-1l06pbn">
localThresholdMS
</code>
. The driver will load balance randomly
across 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 that fall within the latency
window.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-read-preference-mechanics-sharded-cluster">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Read Preference and Shards
<a class="headerlink leafygreen-ui-1kepx47" href="#read-preference-and-shards" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-and-shards">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
For sharded clusters that have replica set shards,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
applies the read preference when reading from the shards. Server
selection is governed by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/">
read preference
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-replication.localPingThresholdMs">
<code class="leafygreen-ui-1l06pbn">
replication.localPingThresholdMs
</code>
</a>
settings. The read preference is re-evaluated for each operation.
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Hedged Reads
<a class="headerlink leafygreen-ui-1kepx47" href="#hedged-reads" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="hedged-reads">
</div>
</a>
</h4>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, hedged reads are deprecated. Queries that
specify the read preference
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#mongodb-readmode-nearest">
<code class="leafygreen-ui-1l06pbn">
nearest
</code>
</a>
no longer use hedged
reads by default. If you explicitly specify a hedged read, MongoDB
performs a hedged read and logs a warning.
</p>
</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>
supports
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-query-router/#std-label-mongos-hedged-reads">
hedged reads
</a>
for
non-
<code class="leafygreen-ui-1l06pbn">
primary
</code>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#std-label-read-preference">
read preference
</a>
modes.
That is,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
can send an additional read to another member,
if available, to hedge the read operation if using non-
<code class="leafygreen-ui-1l06pbn">
primary
</code>
read preferences. The additional read sent to hedge the read operation
uses the
<code class="leafygreen-ui-1l06pbn">
maxTimeMS
</code>
value of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.maxTimeMSForHedgedReads">
<code class="leafygreen-ui-1l06pbn">
maxTimeMSForHedgedReads
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Hedged reads are supported for the following operations:
</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/command/collStats/#mongodb-dbcommand-dbcmd.collStats">
<code class="leafygreen-ui-1l06pbn">
collStats
</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/dataSize/#mongodb-dbcommand-dbcmd.dataSize">
<code class="leafygreen-ui-1l06pbn">
dataSize
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dbStats/#mongodb-dbcommand-dbcmd.dbStats">
<code class="leafygreen-ui-1l06pbn">
dbStats
</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>
</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/command/filemd5/#mongodb-dbcommand-dbcmd.filemd5">
<code class="leafygreen-ui-1l06pbn">
filemd5
</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>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/listCollections/#mongodb-dbcommand-dbcmd.listCollections">
<code class="leafygreen-ui-1l06pbn">
listCollections
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/listIndexes/#mongodb-dbcommand-dbcmd.listIndexes">
<code class="leafygreen-ui-1l06pbn">
listIndexes
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/planCacheListFilters/#mongodb-dbcommand-dbcmd.planCacheListFilters">
<code class="leafygreen-ui-1l06pbn">
planCacheListFilters
</code>
</a>
</p>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p class="leafygreen-ui-1kp3ins">
To use hedged reads:
</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>
must have enabled support for hedged reads
(Default). See the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.readHedgingMode">
<code class="leafygreen-ui-1l06pbn">
readHedgingMode
</code>
</a>
parameter.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Non-
<code class="leafygreen-ui-1l06pbn">
primary
</code>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#std-label-read-preference">
read preferences
</a>
must
enable use of hedged reads.
</p>
</li>
</ul>
<div class="header-buffer" id="ref-default-threshold-id2">
</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>
Read Preference Mode
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Selection Process
</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/core/read-preference/#mongodb-readmode-primary">
<code class="leafygreen-ui-1l06pbn">
primary
</code>
</a>
(Default)
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ol class="leafygreen-ui-rioki0" type="1">
<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>
selects the primary.
</p>
</li>
</ol>
</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/core/read-preference/#mongodb-readmode-secondary">
<code class="leafygreen-ui-1l06pbn">
secondary
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ol class="leafygreen-ui-rioki0" type="1">
<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>
assembles a list of eligible secondary members.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference-staleness/#std-label-replica-set-read-preference-max-staleness">
maxStalenessSeconds
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/configure-replica-set-tag-sets/">
tag
sets
</a>
specified
in the read preference can further restrict the
eligibility of the members.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the list of eligible members is not empty, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
determines which eligible member is
the "closest" (i.e. the member with the lowest average
network round-trip-time) and calculates a latency window
by adding the average round-trip-time of this "closest"
server and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-replication.localPingThresholdMs">
<code class="leafygreen-ui-1l06pbn">
replication.localPingThresholdMs
</code>
</a>
(or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#std-option-mongos.--localThreshold">
<code class="leafygreen-ui-1l06pbn">
--localThreshold
</code>
</a>
command line option). The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
uses this
latency window to pare down the list of eligible members
to those members that fall within this window.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
From this list of eligible members that fall within the
latency window, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
randomly chooses
an eligible member.
</p>
</li>
</ol>
</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/core/read-preference/#mongodb-readmode-nearest">
<code class="leafygreen-ui-1l06pbn">
nearest
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ol class="leafygreen-ui-rioki0" type="1">
<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>
assembles a list of eligible members (primary and
secondaries).
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference-staleness/#std-label-replica-set-read-preference-max-staleness">
maxStalenessSeconds
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/configure-replica-set-tag-sets/">
tag sets
</a>
specified
in the read preference can further limit the eligibility
of the members.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the list of eligible members is not empty, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
determines which eligible member is
the "closest" (i.e. the member with the lowest average
network round-trip-time) and calculates a latency window
by adding the average round-trip-time of this "closest"
server and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-replication.localPingThresholdMs">
<code class="leafygreen-ui-1l06pbn">
replication.localPingThresholdMs
</code>
</a>
(or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#std-option-mongos.--localThreshold">
<code class="leafygreen-ui-1l06pbn">
--localThreshold
</code>
</a>
command line option)
<a class="footnote-reference header-buffer" href="#footnote-default-threshold" id="ref-default-threshold-id2">
[
<!-- -->
1
<!-- -->
]
</a>
. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
uses this latency window to pare
down the list of eligible members to those members that
fall within this window.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
From this list of eligible members that fall within the
latency window, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
randomly chooses
an eligible member.
</p>
</li>
</ol>
</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/core/read-preference/#mongodb-readmode-primaryPreferred">
<code class="leafygreen-ui-1l06pbn">
primaryPreferred
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the primary is available,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
selects the primary.
</p>
</li>
</ol>
<dl>
<dt>
# . If the primary is unavailable, server
</dt>
<dd class="css-1ufxuu7">
selection follows the process for the read preference
<code class="leafygreen-ui-1l06pbn">
secondary
</code>
.
</dd>
</dl>
</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/core/read-preference/#mongodb-readmode-secondaryPreferred">
<code class="leafygreen-ui-1l06pbn">
secondaryPreferred
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Following the server selection process for the read
preference
<code class="leafygreen-ui-1l06pbn">
secondary
</code>
, if a list of eligible secondary
members is non-empty,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
chooses an
eligible secondary.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the list of eligible secondary
members is empty or if
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
is using hedged
reads and only one eligible
secondary is available,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
selects the
primary.
</p>
</li>
</ol>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-default-threshold" 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-default-threshold-id1">
1
</a>
,
<a href="#ref-default-threshold-id2">
2
</a>
)
</em>
<!-- -->
The default threshold value is 15 milliseconds.
</td>
</tr>
</tbody>
</table>
</section>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/capped-collections/change-max-docs-capped-collection/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Change Maximum Documents in a Capped Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#change-maximum-documents-in-a-capped-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="change-maximum-documents-in-a-capped-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="#steps" target="_self">
<span>
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>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
To change the maximum number of documents in a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/capped-collections/#std-label-manual-capped-collection">
capped collection
</a>
, use 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's
<code class="leafygreen-ui-1l06pbn">
cappedMax
</code>
option.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
cappedMax
</code>
is less than or equal to
<code class="leafygreen-ui-1l06pbn">
0
</code>
, there is no maximum
document limit.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
cappedMax
</code>
is less than the current number of documents in the
collection, MongoDB removes the excess documents on the next insert
operation.
</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">
Generally,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-ttl/#std-label-index-feature-ttl">
TTL (Time To Live) indexes
</a>
offer
better performance and more flexibility than capped collections. TTL
indexes expire and remove data from normal collections based on the
value of a date-typed field and a TTL value for the index.
</p>
<p class="leafygreen-ui-1kp3ins">
Capped collections serialize write operations and therefore have worse
concurrent insert, update, and delete performance than non-capped
collections. Before you create a capped collection, consider if you
can use a TTL index instead.
</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">
Create a capped collection called
<code class="leafygreen-ui-1l06pbn">
log
</code>
that can store a maximum of
20,000 documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.createCollection( \"log\", { capped: true, size: 5242880, max: 20000 } )","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">"log"</span>, <!-- -->{ <span class="lg-highlight-attr">capped</span>: <span class="lg-highlight-literal">true</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-number">5242880</span>, <span class="lg-highlight-attr">max</span>: <span class="lg-highlight-number">20000</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">
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>
<p class="leafygreen-ui-1kp3ins">
Run the following command to set the maximum number of documents in the
<code class="leafygreen-ui-1l06pbn">
log
</code>
collection to 5,000:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand( { collMod: \"log\", cappedMax: 5000 } )","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">collMod</span>: <span class="lg-highlight-string">"log"</span>, <span class="lg-highlight-attr">cappedMax</span>: <span class="lg-highlight-number">5000</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/core/capped-collections/change-size-capped-collection/#std-label-capped-collections-change-size">
Change the Size of a Capped Collection
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/capped-collections/check-if-collection-is-capped/#std-label-capped-collections-check">
Check if a Collection is Capped
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/capped-collections/query-capped-collection/#std-label-capped-collections-query">
Query a Capped Collection
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/capped-collections/query-capped-collection/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Query a Capped Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#query-a-capped-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="query-a-capped-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-x9koo9" href="#multiple-concurrent-writes" target="_self">
<span>
Multiple Concurrent Writes
</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-x9koo9" href="#create-a-capped-collection" target="_self">
<span>
Create a capped collection
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#insert-sample-data" target="_self">
<span>
Insert sample data
</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="#return-documents-in-insertion-order" target="_self">
<span>
Return Documents in Insertion Order
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#return-most-recent-documents" target="_self">
<span>
Return Most Recent Documents
</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">
When you query a capped collection without specifying a sort order,
MongoDB returns results in the same order that they were inserted,
meaning the oldest documents are returned first.
</p>
<p class="leafygreen-ui-1kp3ins">
Use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-natural-order">
natural ordering
</a>
to retrieve the most
recently inserted elements from the collection efficiently. This is
similar to using the
<code class="leafygreen-ui-1l06pbn">
tail
</code>
command on a log file.
</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">
Generally,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-ttl/#std-label-index-feature-ttl">
TTL (Time To Live) indexes
</a>
offer
better performance and more flexibility than capped collections. TTL
indexes expire and remove data from normal collections based on the
value of a date-typed field and a TTL value for the index.
</p>
<p class="leafygreen-ui-1kp3ins">
Capped collections serialize write operations and therefore have worse
concurrent insert, update, and delete performance than non-capped
collections. Before you create a capped collection, consider if you
can use a TTL index instead.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Multiple Concurrent Writes
<a class="headerlink leafygreen-ui-1kepx47" href="#multiple-concurrent-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-concurrent-writes">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If there are concurrent writers to a capped collection, MongoDB does not
guarantee that documents are returned in insertion order.
</p>
</section>
</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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Create a capped collection"},{"@type":"HowToStep","text":"Insert sample data"}],"name":"Before you Begin","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 capped collection
<a class="headerlink leafygreen-ui-1kepx47" href="#create-a-capped-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-capped-collection">
</div>
</a>
</h3>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.createCollection(\"log\", { capped: true, size: 100000 } )","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">"log"</span>, <!-- -->{ <span class="lg-highlight-attr">capped</span>: <span class="lg-highlight-literal">true</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-number">100000</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">
Insert sample data
<a class="headerlink leafygreen-ui-1kepx47" href="#insert-sample-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="insert-sample-data">
</div>
</a>
</h3>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.log.insertMany( [\n {\n message: \"system start\",\n type: \"startup\",\n time: 1711403508\n },\n {\n message: \"user login attempt\",\n type: \"info\",\n time: 1711403907\n },\n {\n message: \"user login fail\",\n type: \"warning\",\n time: 1711404209\n },\n {\n message: \"user login success\",\n type: \"info\",\n time: 1711404367\n },\n {\n message: \"user logout\",\n type: \"info\",\n time: 1711404555\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">log</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">message</span>: <span class="lg-highlight-string">"system start"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"startup"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-number">1711403508</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">message</span>: <span class="lg-highlight-string">"user login attempt"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"info"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-number">1711403907</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">message</span>: <span class="lg-highlight-string">"user login fail"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"warning"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-number">1711404209</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">message</span>: <span class="lg-highlight-string">"user login success"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"info"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-number">1711404367</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">message</span>: <span class="lg-highlight-string">"user logout"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"info"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-number">1711404555</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>
</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>
<p class="leafygreen-ui-1kp3ins">
The following examples show you how to:
</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="#std-label-query-capped-collection-insertion-order" target="_self">
<span>
Return Documents in Insertion Order
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-query-capped-collection-recent" target="_self">
<span>
Return Most Recent Documents
</span>
</a>
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-query-capped-collection-insertion-order">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Return Documents in Insertion Order
<a class="headerlink leafygreen-ui-1kepx47" href="#return-documents-in-insertion-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="return-documents-in-insertion-order">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Query the
<code class="leafygreen-ui-1l06pbn">
log
</code>
collection for documents where
<code class="leafygreen-ui-1l06pbn">
type
</code>
is
<code class="leafygreen-ui-1l06pbn">
info
</code>
,
and use the default sort order:
</p>
<div class="css-1f5771r">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.log.find( { type: \"info\" } )","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">log</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"info"</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="css-9st569 efa38qy0" style="--border-color:#E8EDEB">
<button aria-disabled="false" class="lg-ui-button-0000 leafygreen-ui-hix6y6" data-lgid="lg-button" role="button" type="button">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg alt="" aria-hidden="true" class="leafygreen-ui-hjnzs9" fill="none" height="16" role="presentation" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg">
<path clip-rule="evenodd" d="M14.364 10.778a1 1 0 0 0 0-1.414l-4.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 7.243l4.243 4.242a1 1 0 0 0 1.414 0l.707-.707Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
HIDE OUTPUT
</div>
</button>
</div>
<div class="leafygreen-ui-1oteowz">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: ObjectId(\"660204b74cabd75abebadbc2\"),\n message: 'user login attempt',\n type: 'info',\n time: 1711403907\n },\n {\n _id: ObjectId(\"660204b74cabd75abebadbc4\"),\n message: 'user login success',\n type: 'info',\n time: 1711404367\n },\n {\n _id: ObjectId(\"660204b74cabd75abebadbc5\"),\n message: 'user logout',\n type: 'info',\n time: 1711404555\n }\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1sax7hu e11q6jfk0" style="--code-container-border:initial;--code-pre-border:none">
<div class="leafygreen-ui-1fyvhqe">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-12cejcz" tabindex="-1"><code class="lg-highlight-hljs-dark 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">"660204b74cabd75abebadbc2"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">message</span>: <span class="lg-highlight-string">'user login attempt'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">'info'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-number">1711403907</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">"660204b74cabd75abebadbc4"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">message</span>: <span class="lg-highlight-string">'user login success'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">'info'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-number">1711404367</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">"660204b74cabd75abebadbc5"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">message</span>: <span class="lg-highlight-string">'user logout'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">'info'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-number">1711404555</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>
<p class="leafygreen-ui-1kp3ins">
Documents are returned in the order that they were inserted.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-query-capped-collection-recent">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Return Most Recent Documents
<a class="headerlink leafygreen-ui-1kepx47" href="#return-most-recent-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="return-most-recent-documents">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To return documents in reverse insertion order (meaning the most recent
documents are first), specify the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.sort/#mongodb-method-cursor.sort">
<code class="leafygreen-ui-1l06pbn">
sort()
</code>
</a>
method with
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/meta/natural/#mongodb-operator-metaOp.-natural">
<code class="leafygreen-ui-1l06pbn">
$natural
</code>
</a>
parameter set to
<code class="leafygreen-ui-1l06pbn">
-1
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
The following query returns the three most recent documents from the
<code class="leafygreen-ui-1l06pbn">
log
</code>
collection, starting with the most recent document:
</p>
<div class="css-1f5771r">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.log.find().sort( { $natural: -1 } ).limit(3)","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">log</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">$natural</span>: <!-- -->-<span class="lg-highlight-number">1</span> } ).<span class="lg-highlight-title lg-highlight-function">limit</span>(<span class="lg-highlight-number">3</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="css-9st569 efa38qy0" style="--border-color:#E8EDEB">
<button aria-disabled="false" class="lg-ui-button-0000 leafygreen-ui-hix6y6" data-lgid="lg-button" role="button" type="button">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg alt="" aria-hidden="true" class="leafygreen-ui-hjnzs9" fill="none" height="16" role="presentation" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg">
<path clip-rule="evenodd" d="M14.364 10.778a1 1 0 0 0 0-1.414l-4.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 7.243l4.243 4.242a1 1 0 0 0 1.414 0l.707-.707Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
HIDE OUTPUT
</div>
</button>
</div>
<div class="leafygreen-ui-1oteowz">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":" [\n {\n _id: ObjectId(\"6601f2484cabd75abebadbbb\"),\n message: 'user logout',\n type: 'info',\n time: 1711404555\n },\n {\n _id: ObjectId(\"6601f2484cabd75abebadbba\"),\n message: 'user login success',\n type: 'info',\n time: 1711404367\n },\n {\n _id: ObjectId(\"6601f2484cabd75abebadbb9\"),\n message: 'user login fail',\n type: 'warning',\n time: 1711404209\n }\n ]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1sax7hu e11q6jfk0" style="--code-container-border:initial;--code-pre-border:none">
<div class="leafygreen-ui-1fyvhqe">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-12cejcz" tabindex="-1"><code class="lg-highlight-hljs-dark 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">"6601f2484cabd75abebadbbb"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">message</span>: <span class="lg-highlight-string">'user logout'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">'info'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-number">1711404555</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">"6601f2484cabd75abebadbba"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">message</span>: <span class="lg-highlight-string">'user login success'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">'info'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-number">1711404367</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">"6601f2484cabd75abebadbb9"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">message</span>: <span class="lg-highlight-string">'user login fail'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">'warning'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-number">1711404209</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>
</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/core/index-ttl/#std-label-index-feature-ttl">
TTL Indexes
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-optimization/#std-label-read-operations-indexing">
Query Optimization
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/data-modeling/schema-design-process/create-indexes/#std-label-create-indexes-to-support-queries">
Create Indexes to Support Your Queries
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/capped-collections/check-if-collection-is-capped/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Check if a Collection is Capped
<a class="headerlink leafygreen-ui-1kepx47" href="#check-if-a-collection-is-capped" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-if-a-collection-is-capped">
</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-cpmeq5" href="#learn-more" target="_self">
<span>
Learn More
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
To check if a collection is capped, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.isCapped/#mongodb-method-db.collection.isCapped">
<code class="leafygreen-ui-1l06pbn">
isCapped()
</code>
</a>
method.
</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">
Generally,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-ttl/#std-label-index-feature-ttl">
TTL (Time To Live) indexes
</a>
offer
better performance and more flexibility than capped collections. TTL
indexes expire and remove data from normal collections based on the
value of a date-typed field and a TTL value for the index.
</p>
<p class="leafygreen-ui-1kp3ins">
Capped collections serialize write operations and therefore have worse
concurrent insert, update, and delete performance than non-capped
collections. Before you create a capped collection, consider if you
can use a TTL index instead.
</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">
Create a non-capped collection and a capped collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.createCollection(\"nonCappedCollection1\")\n\ndb.createCollection(\"cappedCollection1\", { capped: true, size: 100000 } )","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">createCollection</span>(<span class="lg-highlight-string">"nonCappedCollection1"</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">createCollection</span>(<span class="lg-highlight-string">"cappedCollection1"</span>, <!-- -->{ <span class="lg-highlight-attr">capped</span>: <span class="lg-highlight-literal">true</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-number">100000</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">
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>
<p class="leafygreen-ui-1kp3ins">
To check if the collections are capped, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.isCapped/#mongodb-method-db.collection.isCapped">
<code class="leafygreen-ui-1l06pbn">
isCapped()
</code>
</a>
method:
</p>
<div class="css-1f5771r">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.nonCappedCollection1.isCapped()\n\ndb.cappedCollection1.isCapped()","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">nonCappedCollection1</span>.<span class="lg-highlight-title lg-highlight-function">isCapped</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-property">cappedCollection1</span>.<span class="lg-highlight-title lg-highlight-function">isCapped</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="css-9st569 efa38qy0" style="--border-color:#E8EDEB">
<button aria-disabled="false" class="lg-ui-button-0000 leafygreen-ui-hix6y6" data-lgid="lg-button" role="button" type="button">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg alt="" aria-hidden="true" class="leafygreen-ui-hjnzs9" fill="none" height="16" role="presentation" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg">
<path clip-rule="evenodd" d="M14.364 10.778a1 1 0 0 0 0-1.414l-4.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 7.243l4.243 4.242a1 1 0 0 0 1.414 0l.707-.707Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
HIDE OUTPUT
</div>
</button>
</div>
<div class="leafygreen-ui-1oteowz">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"false\ntrue","programmingLanguage":"JavaScript"}
</script>
<div class="css-1sax7hu e11q6jfk0" style="--code-container-border:initial;--code-pre-border:none">
<div class="leafygreen-ui-1fyvhqe">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-12cejcz" tabindex="-1"><code class="lg-highlight-hljs-dark javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-literal">false</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-literal">true</span></td></tr></tbody></table></code></pre>
</div>
</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/core/capped-collections/create-capped-collection/#std-label-capped-collections-create">
Create a Capped Collection
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/capped-collections/convert-collection-to-capped/#std-label-capped-collections-convert">
Convert a Collection to Capped
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/collStats/#mongodb-pipeline-pipe.-collStats">
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/capped-collections/change-size-capped-collection/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Change the Size of a Capped Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#change-the-size-of-a-capped-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="change-the-size-of-a-capped-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="#steps" target="_self">
<span>
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>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
To change the size of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/capped-collections/#std-label-manual-capped-collection">
capped collection
</a>
, use 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's
<code class="leafygreen-ui-1l06pbn">
cappedSize
</code>
option.
<code class="leafygreen-ui-1l06pbn">
cappedSize
</code>
is specified in bytes, and must be
greater than
<code class="leafygreen-ui-1l06pbn">
0
</code>
and less than or equal to
<code class="leafygreen-ui-1l06pbn">
1024^5
</code>
(1
<span class="leafygreen-ui-vm4wms" tabindex="0">
PB
</span>
).
</p>
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
cappedSize
</code>
is less than the current size of the collection,
MongoDB removes the excess documents on the next insert operation.
</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">
Generally,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-ttl/#std-label-index-feature-ttl">
TTL (Time To Live) indexes
</a>
offer
better performance and more flexibility than capped collections. TTL
indexes expire and remove data from normal collections based on the
value of a date-typed field and a TTL value for the index.
</p>
<p class="leafygreen-ui-1kp3ins">
Capped collections serialize write operations and therefore have worse
concurrent insert, update, and delete performance than non-capped
collections. Before you create a capped collection, consider if you
can use a TTL index instead.
</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">
Create a capped collection called
<code class="leafygreen-ui-1l06pbn">
log
</code>
that has a maximum size of
2,621,440 bytes:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.createCollection( \"log\", { capped: true, size: 2621440 } )","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">"log"</span>, <!-- -->{ <span class="lg-highlight-attr">capped</span>: <span class="lg-highlight-literal">true</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-number">2621440</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">
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>
<p class="leafygreen-ui-1kp3ins">
Run the following command to set the maximum size of the
<code class="leafygreen-ui-1l06pbn">
log
</code>
collection to 5,242,880 bytes:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand( { collMod: \"log\", cappedSize: 5242880 } )","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">collMod</span>: <span class="lg-highlight-string">"log"</span>, <span class="lg-highlight-attr">cappedSize</span>: <span class="lg-highlight-number">5242880</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/core/capped-collections/change-max-docs-capped-collection/#std-label-capped-collections-change-max-docs">
Change Maximum Documents in a Capped Collection
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/capped-collections/check-if-collection-is-capped/#std-label-capped-collections-check">
Check if a Collection is Capped
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/capped-collections/query-capped-collection/#std-label-capped-collections-query">
Query a Capped Collection
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/capped-collections/convert-collection-to-capped/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Convert a Collection to Capped
<a class="headerlink leafygreen-ui-1kepx47" href="#convert-a-collection-to-capped" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-a-collection-to-capped">
</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="#convert-the-collection-to-a-capped-collection" target="_self">
<span>
Convert the collection to a capped collection
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#confirm-that-the-collection-is-capped" target="_self">
<span>
Confirm that the collection is capped
</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 convert a non-capped collection to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/capped-collections/#std-label-manual-capped-collection">
capped collection
</a>
, 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>
database command.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
convertToCapped
</code>
command holds a database-exclusive lock for the
duration of the operation. Other operations that lock the same database
are blocked until the
<code class="leafygreen-ui-1l06pbn">
convertToCapped
</code>
operation completes.
</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">
Generally,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-ttl/#std-label-index-feature-ttl">
TTL (Time To Live) indexes
</a>
offer
better performance and more flexibility than capped collections. TTL
indexes expire and remove data from normal collections based on the
value of a date-typed field and a TTL value for the index.
</p>
<p class="leafygreen-ui-1kp3ins">
Capped collections serialize write operations and therefore have worse
concurrent insert, update, and delete performance than non-capped
collections. Before you create a capped collection, consider if you
can use a TTL index instead.
</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">
Create a non-capped collection called
<code class="leafygreen-ui-1l06pbn">
log2
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.createCollection(\"log2\")","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">createCollection</span>(<span class="lg-highlight-string">"log2"</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">
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":"To convert the log2 collection to a capped collection, run the\nconvertToCapped command:The log2 collection has a maximum size of 100,000 bytes.","name":"Convert the collection to a capped collection"},{"@type":"HowToStep","text":"To confirm that the log2 collection is now capped, use the\nisCapped() method:","name":"Confirm that the collection is capped"}],"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">
Convert the collection to a capped collection
<a class="headerlink leafygreen-ui-1kepx47" href="#convert-the-collection-to-a-capped-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="convert-the-collection-to-a-capped-collection">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To convert the
<code class="leafygreen-ui-1l06pbn">
log2
</code>
collection to a capped collection, run 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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand( {\n convertToCapped: \"log2\",\n size: 100000\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"> <span class="lg-highlight-attr">convertToCapped</span>: <span class="lg-highlight-string">"log2"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-number">100000</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">
log2
</code>
collection has a maximum size of 100,000 bytes.
</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">
Confirm that the collection is capped
<a class="headerlink leafygreen-ui-1kepx47" href="#confirm-that-the-collection-is-capped" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="confirm-that-the-collection-is-capped">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To confirm that the
<code class="leafygreen-ui-1l06pbn">
log2
</code>
collection is now capped, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.isCapped/#mongodb-method-db.collection.isCapped">
<code class="leafygreen-ui-1l06pbn">
isCapped()
</code>
</a>
method:
</p>
<div class="css-1f5771r">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.log2.isCapped()","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">log2</span>.<span class="lg-highlight-title lg-highlight-function">isCapped</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="css-9st569 efa38qy0" style="--border-color:#E8EDEB">
<button aria-disabled="false" class="lg-ui-button-0000 leafygreen-ui-hix6y6" data-lgid="lg-button" role="button" type="button">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg alt="" aria-hidden="true" class="leafygreen-ui-hjnzs9" fill="none" height="16" role="presentation" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg">
<path clip-rule="evenodd" d="M14.364 10.778a1 1 0 0 0 0-1.414l-4.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 7.243l4.243 4.242a1 1 0 0 0 1.414 0l.707-.707Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
HIDE OUTPUT
</div>
</button>
</div>
<div class="leafygreen-ui-1oteowz">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":" true","programmingLanguage":"JavaScript"}
</script>
<div class="css-1sax7hu e11q6jfk0" style="--code-container-border:initial;--code-pre-border:none">
<div class="leafygreen-ui-1fyvhqe">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-1qhgg7e" tabindex="-1"><code class="lg-highlight-hljs-dark javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-literal">true</span></td></tr></tbody></table></code></pre>
</div>
</div>
</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">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/faq/concurrency/#std-label-faq-concurrency-database-lock">
Which administrative commands lock a database?
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/capped-collections/change-size-capped-collection/#std-label-capped-collections-change-size">
Change the Size of a Capped Collection
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/capped-collections/query-capped-collection/#std-label-capped-collections-query">
Query a Capped Collection
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/automerger-concept/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
The AutoMerger
<a class="headerlink leafygreen-ui-1kepx47" href="#the-automerger" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-automerger">
</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="#details" target="_self">
<span>
Details
</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>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 7.0, the balancer can automatically merge chunks
that meet the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-mergeability-concept" target="_self">
<span>
mergeability requirements
<!-- -->
.
</span>
</a>
</p>
<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 AutoMerger runs in the background as part of balancing
operations. For most use cases, the default settings perform well. For
details on which settings to customize for your deployment, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-automerge-policy-settings" target="_self">
<span>
AutoMerger Policy
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
When the AutoMerger runs, it squashes together all sequences
of mergeable chunks for each shard of each collection.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
AutoMerger Policy
<a class="headerlink leafygreen-ui-1kepx47" href="#automerger-policy" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="automerger-policy">
</div>
</a>
</h3>
<span class="leafygreen-ui-1qnjd6d" id="std-label-automerge-policy-settings">
</span>
<p class="leafygreen-ui-1kp3ins">
Unless explicitly disabled, the AutoMerger starts the first
time the balancer is enabled and pauses for the next
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.autoMergerIntervalSecs">
<code class="leafygreen-ui-1l06pbn">
autoMergerIntervalSecs
</code>
</a>
after the routine drains.
</p>
<p class="leafygreen-ui-1kp3ins">
When AutoMerger is enabled, automerging happens
every
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.autoMergerIntervalSecs">
<code class="leafygreen-ui-1l06pbn">
autoMergerIntervalSecs
</code>
</a>
seconds.
</p>
<p class="leafygreen-ui-1kp3ins">
For a given collection, AutoMerger guarantees that subsequent
merges are delayed at least the amount specified by
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.autoMergerThrottlingMS">
<code class="leafygreen-ui-1l06pbn">
autoMergerThrottlingMS
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
If a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-sharded-cluster-balancer/#std-label-sharding-schedule-balancing-window">
balancing window
</a>
is
set, AutoMerger only runs during the window.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Balancing Settings Precedence
<a class="headerlink leafygreen-ui-1kepx47" href="#balancing-settings-precedence" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="balancing-settings-precedence">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Automerging happens as part of balancing operations.
In order to decide if and when to execute automerger,
the settings are taken into account in this order:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Global
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#std-label-balancer-sharding-params">
balancing settings
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Per-collection balancing settings (configured by
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/configureCollectionBalancing/#mongodb-dbcommand-dbcmd.configureCollectionBalancing">
<code class="leafygreen-ui-1l06pbn">
configureCollectionBalancing
</code>
)
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Global
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#std-label-automerger-params">
AutoMerger settings
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Per-collection AutoMerger settings (configured by
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/configureCollectionBalancing/#mongodb-dbcommand-dbcmd.configureCollectionBalancing">
<code class="leafygreen-ui-1l06pbn">
configureCollectionBalancing
</code>
)
</a>
</p>
</li>
</ol>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Details
<a class="headerlink leafygreen-ui-1kepx47" href="#details" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="details">
</div>
</a>
</h2>
<span class="leafygreen-ui-1qnjd6d" id="std-label-mergeability-concept">
</span>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
mergeAllChunksOnShard
</code>
finds and merges all mergeable chunks for a
collection on the same shard. Two or more contiguous chunks in the same
collection are
<strong>
mergeable
</strong>
when they meet all of these conditions:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
They are owned by the same shard.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
They are not
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-data-partitioning/#std-label-jumbo-chunk">
jumbo
</a>
chunks.
<code class="leafygreen-ui-1l06pbn">
jumbo
</code>
chunks are
not mergeable because they cannot participate in migrations.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Their history can be purged safely, without breaking transactions and
snapshot reads:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The last migration involving the chunk happened at least as many
seconds ago as the value of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.minSnapshotHistoryWindowInSeconds">
<code class="leafygreen-ui-1l06pbn">
minSnapshotHistoryWindowInSeconds
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The last migration involving the chunk happened at least as many
seconds ago as the value of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.transactionLifetimeLimitSeconds">
<code class="leafygreen-ui-1l06pbn">
transactionLifetimeLimitSeconds
</code>
.
</a>
</p>
</li>
</ul>
</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">
This example assumes that history is empty for all chunks and all chunks
are non-jumbo. Since both conditions are true, all contiguous intervals
on the same shard are
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mergeAllChunksOnShard/#std-label-mergeability">
mergeable
<!-- -->
.
</a>
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Setup
<a class="headerlink leafygreen-ui-1kepx47" href="#setup" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="setup">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
These chunks belong to a collection named
<code class="leafygreen-ui-1l06pbn">
coll
</code>
with shard key
<code class="leafygreen-ui-1l06pbn">
x
</code>
.
There are nine chunks in total.
</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:25%"/>
<col style="width:25%"/>
</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>
Chunk ID
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Min
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Max
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Shard
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 0
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 10
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
B
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 10
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 20
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
C
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 20
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 30
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
D
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 30
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 40
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
E
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 40
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 50
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard1
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
F
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 50
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 60
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard1
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
G
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 60
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 70
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
H
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 70
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 80
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
I
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 80
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 90
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard1
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" 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>
</h3>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"This command merges the contiguous sequences of chunks:A-B-C-DG-H","name":"Merge All Mergeable Chunks on Shard0"},{"@type":"HowToStep","text":"This command merges the contiguous sequences of chunks E-F.","name":"Merge All Mergeable Chunks on Shard1"}],"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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Merge All Mergeable Chunks on Shard0
<a class="headerlink leafygreen-ui-1kepx47" href="#merge-all-mergeable-chunks-on-shard0" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="merge-all-mergeable-chunks-on-shard0">
</div>
</a>
</h4>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { mergeAllChunksOnShard: \"db.coll\", shard: \"Shard0\" } )","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">mergeAllChunksOnShard</span>: <span class="lg-highlight-string">"db.coll"</span>, <span class="lg-highlight-attr">shard</span>: <span class="lg-highlight-string">"Shard0"</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 command merges the contiguous sequences of chunks:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A-B-C-D
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
G-H
</p>
</li>
</ul>
</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">
Merge All Mergeable Chunks on Shard1
<a class="headerlink leafygreen-ui-1kepx47" href="#merge-all-mergeable-chunks-on-shard1" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="merge-all-mergeable-chunks-on-shard1">
</div>
</a>
</h4>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { mergeAllChunksOnShard: \"db.coll\", shard: \"Shard1\" } )","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">mergeAllChunksOnShard</span>: <span class="lg-highlight-string">"db.coll"</span>, <span class="lg-highlight-attr">shard</span>: <span class="lg-highlight-string">"Shard1"</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 command merges the contiguous sequences of chunks E-F.
</p>
</section>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Result
<a class="headerlink leafygreen-ui-1kepx47" href="#result" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="result">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
After these commands have completed, the contiguous chunks have been
merged. There are four total chunks instead of the original nine.
</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:25%"/>
<col style="width:25%"/>
</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>
Chunk ID
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Min
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Max
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Shard
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A-B-C-D
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 0
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 40
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
E-F
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 40
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 60
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard1
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
G-H
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 60
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 80
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
I
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 80
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 90
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard1
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/create-users/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Create a User on Self-Managed Deployments
<a class="headerlink leafygreen-ui-1kepx47" href="#create-a-user-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="create-a-user-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="#mongodb-atlas-compatibility" target="_self">
<span>
MongoDB Atlas Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#prerequisites" target="_self">
<span>
Prerequisites
</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="#connect-and-authenticate" target="_self">
<span>
Connect and authenticate
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#create-additional-users-for-your-deployment" target="_self">
<span>
Create additional users for your deployment
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#connect-to-the-instance-and-authenticate-as-mytester" target="_self">
<span>
Connect to the instance and authenticate as
<code class="css-1kekw28 e1wawog0">
myTester
</code>
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#insert-a-document-as-mytester" target="_self">
<span>
Insert a document as
<code class="css-1kekw28 e1wawog0">
myTester
</code>
</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>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#username-password-authentication" target="_self">
<span>
Username/Password Authentication
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#kerberos-authentication" target="_self">
<span>
Kerberos Authentication
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#ldap-authentication" target="_self">
<span>
LDAP Authentication
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#x.509-client-certificate-authentication" target="_self">
<span>
x.509 Client Certificate Authentication
</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">
With access control enabled, users are required to identify themselves.
You have to grant a user one or more
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-roles">
roles
</a>
. A role grants
a user
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-privileges">
privileges
</a>
to perform certain
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#std-label-security-user-actions">
actions
</a>
on MongoDB
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/resource-document/#std-label-resource-document">
resources
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Each application and user of a MongoDB system should map to a distinct
user. This principle of
<em>
access isolation
</em>
facilitates access revocation
and ongoing user maintenance. To ensure a system of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-least-privilege">
least privilege
</a>
, only grant the minimal set of privileges required to a user.
</p>
<p class="leafygreen-ui-1kp3ins">
The user information on this page applies to self-managed deployments hosted in
all of the following environments unless specified otherwise:
</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>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
MongoDB Atlas Compatibility
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-atlas-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="mongodb-atlas-compatibility">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following limitations apply only to deployments hosted in
MongoDB Atlas. If any of these limits present a problem for your organization,
contact
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/support/" target="_self">
<span>
Atlas support
<!-- -->
.
</span>
</a>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
You must use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/cli/stable/command/atlas-dbusers-create/" target="_self">
<span>
Atlas CLI
</span>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Database-Users" target="_self">
<span>
Atlas Administration API
</span>
</a>
,
Atlas UI, or a supported
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/partner-integrations/#std-label-partner-integrations" target="_self">
<span>
integration
</span>
</a>
to add, modify, or delete database users on MongoDB Atlas clusters.
Otherwise, MongoDB Atlas rolls back any user modifications. To add users
in MongoDB Atlas, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/security-add-mongodb-users/#std-label-add-mongodb-users" target="_self">
<span>
Add Database Users
</span>
</a>
in the MongoDB Atlas documentation.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The available MongoDB Atlas
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/security-add-mongodb-users/#std-label-atlas-user-privileges" target="_self">
<span>
built-in roles
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/security-add-mongodb-users/#std-label-atlas-specific-privileges" target="_self">
<span>
specific privileges
</span>
</a>
support a subset of MongoDB commands.
See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/unsupported-commands/#std-label-paid-tier-command-limitations" target="_self">
<span>
Unsupported Commands in M10+ Clusters
</span>
</a>
for more information.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
MongoDB Atlas supports a maximum of 100 database users per MongoDB Atlas
project. If you require more than 100 database users on a project,
contact
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/support/" target="_self">
<span>
Atlas support
<!-- -->
.
</span>
</a>
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-add-user-prereq">
</span>
</section>
<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">
To be able to create users, you need to:
</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/enable-authentication/#std-label-enable-access-control">
enable access control
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/configure-scram-client-authentication/#std-label-create-user-admin">
create a user administrator
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For routine user creation, you must possess the following permissions:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To create a new user in a database, you must have the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-createUser">
<code class="leafygreen-ui-1l06pbn">
createUser
</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">
To grant roles to a user, you must have 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 the role's database.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The
<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>
built-in roles provide
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-createUser">
<code class="leafygreen-ui-1l06pbn">
createUser
</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/#std-label-resource-document">
resources
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-create-user-procedure">
</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>
<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 following procedures use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-scram/#std-label-authentication-scram">
SCRAM
</a>
authentication. For additional information on other authentication
mechanisms, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-create-users-examples" target="_self">
<span>
Additional Examples
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To configure database users for your self-managed MongoDB Enterprise
or MongoDB Community deployment, follow these steps:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Connect and authenticateUsing mongosh, connect to your primary\nmongod or, in a sharded cluster, connect to your\nmongos and authenticate as a user administrator or a\nuser with the required privileges:Start mongosh with the -u\n<username>, -p, and the\n--authenticationDatabase <database> command line options:Enter your password when prompted.Using mongosh, connect to your database\ndeployment:In mongosh, switch to the\nauthentication database (in this case, admin), and\nuse the db.auth(<username>, <pwd>) method to authenticate:The passwordPrompt() method prompts you to enter the\npassword. You can also specify your password directly as a string. We\nrecommend to use the passwordPrompt() method to avoid the\npassword being visible on your screen and potentially leaking the\npassword to your shell history.Enter the password when prompted."},{"@type":"HowToStep","text":"Create additional users for your deploymentThe following step uses SCRAM authentication.\nFor additional information on other authentication mechanisms, see\nAdditional Examples.After authenticating as the user administrator, use the\ndb.createUser() method to create additional users. You can assign\nany built-in roles or\nuser-defined roles to the\nusers.The following operation adds a user myTester to the test\ndatabase who has the readWrite role in the test\ndatabase as well as the read role in the reporting\ndatabase.The passwordPrompt() method prompts you to enter the\npassword. You can also specify your password directly as a string. We\nrecommend to use the passwordPrompt() method to avoid the\npassword being visible on your screen and potentially leaking the\npassword to your shell history.The database where you create the user (in this example, test) is\nthat user's authentication database. Although the user authenticates to\nthis database, the user can have roles in other databases. The\nuser's authentication database does not limit the user's privileges.After creating the additional users, exit mongosh."},{"@type":"HowToStep","text":"Connect to the instance and authenticate as myTesterIt is not possible to switch between users in the same\nmongosh session. Authenticating as a different user\nmeans the session has the privileges of both authenticated\nusers. To switch between users exit and relaunch\nmongosh.After exiting mongosh as myUserAdmin, reconnect as\nmyTester:Start mongosh with the -u\n<username>, -p, and the\n--authenticationDatabase <database> command line options:Enter the password for the user when prompted.Using mongosh, connect to your database\ndeployment:In mongosh, switch to the\nauthentication database (in this case, admin), and\nuse the db.auth(<username>, <pwd>) method to authenticate:The passwordPrompt() method prompts you to enter the\npassword. You can also specify your password directly as a string. We\nrecommend to use the passwordPrompt() method to avoid the\npassword being visible on your screen and potentially leaking the\npassword to your shell history.Enter the password for the user when prompted."},{"@type":"HowToStep","text":"Insert a document as myTesterAs the user myTester, you have privileges to perform read and\nwrite operations in the test database (as well as perform read\noperations in the reporting database). Once authenticated as\nmyTester, insert a document into a collection in the test\ndatabase. For example, you can perform the following insert\noperation in the test database:"}],"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">
Connect and authenticate
<a class="headerlink leafygreen-ui-1kepx47" href="#connect-and-authenticate" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-and-authenticate">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
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>
, connect to your primary
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
or, in a sharded cluster, connect to your
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
and authenticate as a user administrator or a
user with the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-add-user-prereq" target="_self">
<span>
required privileges
<!-- -->
:
</span>
</a>
</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 authafter/cmdline" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_2x794p42a" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_4u5upkm83" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Authenticate during Connection
</div>
</button>
<button aria-controls="_7y8gds964" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_7ceja597j" 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="_29ksjmu5g" class="" id="_cura344aq" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
Start
<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>
with the
<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">
-u
<username>
</code>
</span>
</a>
,
<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">
-p
</code>
</span>
</a>
, and the
<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 <database>
</code>
</span>
</a>
command line options:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongosh --port 27017 --authenticationDatabase \\\n \"admin\" -u \"myUserAdmin\" -p","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 --port 27017 --authenticationDatabase \</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"admin"</span> -u <span class="lg-highlight-string">"myUserAdmin"</span> -p</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Enter your password when prompted.
</p>
</div>
</div>
</div>
<div>
<div aria-labelledby="_r745g09xn" class="leafygreen-ui-38lglc" id="_2xas0kc3s" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
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>
, connect to your database
deployment:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongosh --port 27017","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 --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">
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>
, switch to the
authentication database (in this case,
<code class="leafygreen-ui-1l06pbn">
admin
</code>
), 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(<username>, <pwd>)
</code>
</a>
method to authenticate:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use admin\ndb.auth(\"myUserAdmin\", passwordPrompt()) // or cleartext password","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">auth</span>(<span class="lg-highlight-string">"myUserAdmin"</span>, <span class="lg-highlight-title lg-highlight-function">passwordPrompt</span>(<!-- -->)) <span class="lg-highlight-comment">// or cleartext password</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>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/passwordPrompt/#mongodb-method-passwordPrompt">
<code class="leafygreen-ui-1l06pbn">
passwordPrompt()
</code>
</a>
method prompts you to enter the
password. You can also specify your password directly as a string. We
recommend to use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/passwordPrompt/#mongodb-method-passwordPrompt">
<code class="leafygreen-ui-1l06pbn">
passwordPrompt()
</code>
</a>
method to avoid the
password being visible on your screen and potentially leaking the
password to your shell history.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Enter the password when prompted.
</p>
</div>
</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">
Create additional users for your deployment
<a class="headerlink leafygreen-ui-1kepx47" href="#create-additional-users-for-your-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="create-additional-users-for-your-deployment">
</div>
</a>
</h3>
<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 following step uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-scram/#std-label-authentication-scram">
SCRAM
</a>
authentication.
For additional information on other authentication mechanisms, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-create-users-examples" target="_self">
<span>
Additional Examples
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
After authenticating as the user administrator, use 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 to create additional users. You can assign
any
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#std-label-built-in-roles">
built-in roles
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-user-defined-roles/#std-label-user-defined-roles">
user-defined roles
</a>
to the
users.
</p>
<p class="leafygreen-ui-1kp3ins">
The following operation adds a user
<code class="leafygreen-ui-1l06pbn">
myTester
</code>
to the
<code class="leafygreen-ui-1l06pbn">
test
</code>
database who has the
<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 as well as the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-read">
<code class="leafygreen-ui-1l06pbn">
read
</code>
</a>
role in the
<code class="leafygreen-ui-1l06pbn">
reporting
</code>
database.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use test\ndb.createUser(\n {\n user: \"myTester\",\n pwd: passwordPrompt(), // or cleartext password\n roles: [ { role: \"readWrite\", db: \"test\" },\n { role: \"read\", db: \"reporting\" } ]\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">use test</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<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-attr">user</span>: <span class="lg-highlight-string">"myTester"</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>(<!-- -->)<!-- -->, <span class="lg-highlight-comment">// or cleartext password</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"> <!-- -->{ <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">"reporting"</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>
<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/method/passwordPrompt/#mongodb-method-passwordPrompt">
<code class="leafygreen-ui-1l06pbn">
passwordPrompt()
</code>
</a>
method prompts you to enter the
password. You can also specify your password directly as a string. We
recommend to use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/passwordPrompt/#mongodb-method-passwordPrompt">
<code class="leafygreen-ui-1l06pbn">
passwordPrompt()
</code>
</a>
method to avoid the
password being visible on your screen and potentially leaking the
password to your shell history.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The database where you create the user (in this example,
<code class="leafygreen-ui-1l06pbn">
test
</code>
) is
that user's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-users/#std-label-user-authentication-database">
authentication database
</a>
. Although the user authenticates to
this database, the user can have roles in other databases. The
user's authentication database does not limit the user's privileges.
</p>
<p class="leafygreen-ui-1kp3ins">
After creating the additional users, exit
<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>
</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 instance and authenticate as
<code class="leafygreen-ui-1l06pbn">
myTester
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#connect-to-the-instance-and-authenticate-as-mytester" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-instance-and-authenticate-as-mytester">
</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">
It is not possible to switch between users in the same
<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. Authenticating as a different user
means the session has the privileges of
<strong>
both
</strong>
authenticated
users. To switch between users exit and relaunch
<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>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
After exiting
<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>
as
<code class="leafygreen-ui-1l06pbn">
myUserAdmin
</code>
, reconnect as
<code class="leafygreen-ui-1l06pbn">
myTester
</code>
:
</p>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-11">
<div aria-label="Tabs to describe usage of authafter2/cmdline2" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_0atzs0dx7" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_1a8ouiawt" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Authenticate during Connection
</div>
</button>
<button aria-controls="_mftp0o1ew" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_hibamg84j" 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="_ttmzrfu3f" class="" id="_wg32xsp2m" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
Start
<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>
with the
<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">
-u
<username>
</code>
</span>
</a>
,
<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">
-p
</code>
</span>
</a>
, and the
<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 <database>
</code>
</span>
</a>
command line options:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongosh --port 27017 -u \"myTester\" \\\n --authenticationDatabase \"test\" -p","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 --port 27017 -u <span class="lg-highlight-string">"myTester"</span> \</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> --authenticationDatabase <span class="lg-highlight-string">"test"</span> -p</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Enter the password for the user when prompted.
</p>
</div>
</div>
</div>
<div>
<div aria-labelledby="_dlwdlxx5u" class="leafygreen-ui-38lglc" id="_6zen80djr" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
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>
, connect to your database
deployment:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongosh --port 27017","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 --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">
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>
, switch to the
authentication database (in this case,
<code class="leafygreen-ui-1l06pbn">
admin
</code>
), 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(<username>, <pwd>)
</code>
</a>
method to authenticate:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use test\ndb.auth(\"myTester\", passwordPrompt()) // or cleartext password","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">db.<span class="lg-highlight-title lg-highlight-function">auth</span>(<span class="lg-highlight-string">"myTester"</span>, <span class="lg-highlight-title lg-highlight-function">passwordPrompt</span>(<!-- -->)) <span class="lg-highlight-comment">// or cleartext password</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>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/passwordPrompt/#mongodb-method-passwordPrompt">
<code class="leafygreen-ui-1l06pbn">
passwordPrompt()
</code>
</a>
method prompts you to enter the
password. You can also specify your password directly as a string. We
recommend to use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/passwordPrompt/#mongodb-method-passwordPrompt">
<code class="leafygreen-ui-1l06pbn">
passwordPrompt()
</code>
</a>
method to avoid the
password being visible on your screen and potentially leaking the
password to your shell history.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Enter the password for the user when prompted.
</p>
</div>
</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">
Insert a document as
<code class="leafygreen-ui-1l06pbn">
myTester
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#insert-a-document-as-mytester" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-a-document-as-mytester">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
As the user
<code class="leafygreen-ui-1l06pbn">
myTester
</code>
, you have privileges to perform read and
write operations in the
<code class="leafygreen-ui-1l06pbn">
test
</code>
database (as well as perform read
operations in the
<code class="leafygreen-ui-1l06pbn">
reporting
</code>
database). Once authenticated as
<code class="leafygreen-ui-1l06pbn">
myTester
</code>
, insert a document into a collection in the
<code class="leafygreen-ui-1l06pbn">
test
</code>
database. For example, you can perform the following insert
operation in the
<code class="leafygreen-ui-1l06pbn">
test
</code>
database:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.foo.insertOne( { x: 1, y: 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">foo</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>( <!-- -->{ <span class="lg-highlight-attr">x</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">y</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>
</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/manage-users-and-roles/">
Manage Users and Roles on Self-Managed Deployments
</a>
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-create-users-examples">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-add-new-user">
</span>
</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>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Username/Password Authentication
<a class="headerlink leafygreen-ui-1kepx47" href="#username-password-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="username-password-authentication">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following operation creates a user in the
<code class="leafygreen-ui-1l06pbn">
reporting
</code>
database with the specified name, password, and roles.
</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">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/passwordPrompt/#mongodb-method-passwordPrompt">
<code class="leafygreen-ui-1l06pbn">
passwordPrompt()
</code>
</a>
method prompts you to enter the
password. You can also specify your password directly as a string. We
recommend to use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/passwordPrompt/#mongodb-method-passwordPrompt">
<code class="leafygreen-ui-1l06pbn">
passwordPrompt()
</code>
</a>
method to avoid the
password being visible on your screen and potentially leaking the
password to your shell history.
</p>
</div>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use reporting\ndb.createUser(\n {\n user: \"reportsUser\",\n pwd: passwordPrompt(), // or cleartext password\n roles: [\n { role: \"read\", db: \"reporting\" },\n { role: \"read\", db: \"products\" },\n { role: \"read\", db: \"sales\" },\n { role: \"readWrite\", db: \"accounts\" }\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 reporting</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<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-attr">user</span>: <span class="lg-highlight-string">"reportsUser"</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>(<!-- -->)<!-- -->, <span class="lg-highlight-comment">// or cleartext password</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">"read"</span>, <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"reporting"</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">"products"</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">"sales"</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">"accounts"</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>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Kerberos Authentication
<a class="headerlink leafygreen-ui-1kepx47" href="#kerberos-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="kerberos-authentication">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Users that authenticate to MongoDB using an external authentication
mechanism, such as Kerberos, must be created in the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database, which allows
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</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>
to consult an external source for authentication.
</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>
<p class="leafygreen-ui-1kp3ins">
For Kerberos authentication, you must add the Kerberos principal as the
username. You do not need to specify a password.
</p>
<p class="leafygreen-ui-1kp3ins">
The following operation adds the Kerberos principal
<code class="leafygreen-ui-1l06pbn">
[email protected]
</code>
with read-only access to the
<code class="leafygreen-ui-1l06pbn">
records
</code>
database:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use $external\ndb.createUser(\n {\n user: \"[email protected]\",\n roles: [\n { role: \"read\", db: \"records\" }\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 <!-- -->$external</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<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-attr">user</span>: <span class="lg-highlight-string">"[email protected]"</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">"read"</span>, <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"records"</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-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 more information about setting up Kerberos authentication for
your MongoDB deployment, see the following tutorials:
</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/control-access-to-mongodb-with-kerberos-authentication/">
Configure Self-Managed MongoDB with Kerberos Authentication on Linux
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/control-access-to-mongodb-windows-with-kerberos-authentication/">
Configure Self-Managed MongoDB with Kerberos Authentication on Windows
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
LDAP Authentication
<a class="headerlink leafygreen-ui-1kepx47" href="#ldap-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="ldap-authentication">
</div>
</a>
</h3>
<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">
Users that authenticate to MongoDB using an external authentication
mechanism, such as LDAP, must be created in the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database, which allows
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</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>
to consult an external source for authentication.
</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>
<p class="leafygreen-ui-1kp3ins">
For LDAP authentication, you must specify a username. You do not need
to specify the password, as that is handled by the LDAP service.
</p>
<p class="leafygreen-ui-1kp3ins">
The following operation adds the
<code class="leafygreen-ui-1l06pbn">
reporting
</code>
user with read-only
access to the
<code class="leafygreen-ui-1l06pbn">
records
</code>
database:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use $external\ndb.createUser(\n {\n user: \"reporting\",\n roles: [\n { role: \"read\", db: \"records\" }\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 <!-- -->$external</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<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-attr">user</span>: <span class="lg-highlight-string">"reporting"</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">"read"</span>, <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"records"</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-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 more information about setting up LDAP authentication for
your MongoDB deployment, see the following tutorials:
</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/configure-ldap-sasl-activedirectory/">
Authenticate Using Self-Managed SASL and LDAP with ActiveDirectory
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/configure-ldap-sasl-openldap/">
Authenticate Using Self-Managed SASL and LDAP with OpenLDAP
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
x.509 Client Certificate Authentication
<a class="headerlink leafygreen-ui-1kepx47" href="#x.509-client-certificate-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="x.509-client-certificate-authentication">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Users that authenticate to MongoDB using an external authentication
mechanism, such as x.509 Client Certificate Authentication, must be created in the
<code class="leafygreen-ui-1l06pbn">
$external
</code>
database, which allows
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</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>
to consult an external source for authentication.
</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>
<p class="leafygreen-ui-1kp3ins">
For x.509 Client Certificate authentication, you must add the value of
the
<code class="leafygreen-ui-1l06pbn">
subject
</code>
from the client certificate as a MongoDB user. Each
unique x.509 client certificate corresponds to a single MongoDB user.
You do not need to specify a password.
</p>
<p class="leafygreen-ui-1kp3ins">
The following operation adds the client certificate subject
<code class="leafygreen-ui-1l06pbn">
CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry
</code>
user with read-only access to the
<code class="leafygreen-ui-1l06pbn">
records
</code>
database.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use $external\ndb.createUser(\n {\n user: \"CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry\",\n roles: [\n { role: \"read\", db: \"records\" }\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">use <!-- -->$external</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<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-attr">user</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">"read"</span>, <span class="lg-highlight-attr">db</span>: <span class="lg-highlight-string">"records"</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-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 more information about setting up x.509 Client Certificate
authentication for your MongoDB deployment, see the following
tutorials:
</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/configure-x509-client-authentication/">
Use x.509 Certificates to Authenticate Clients on Self-Managed Deployments
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</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 manage users, assign roles, and create custom roles for your
self-managed MongoDB Enterprise or MongoDB Community deployment,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-users-and-roles/">
Manage Users and Roles on Self-Managed Deployments
<!-- -->
.
</a>
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/top/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
top
<a class="headerlink leafygreen-ui-1kepx47" href="#top" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="top">
</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="#event-fields" target="_self">
<span>
Event 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>
<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>
<dl class="dbcommand">
<dt>
<code class="leafygreen-ui-1l06pbn">
top
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.top" 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.top">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.top" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
top
</code>
</span>
</a>
is an administrative command that returns usage statistics
for each collection. You can use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.top" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
top
</code>
</span>
</a>
metrics to compare the
relative performance of your collections against each other.
</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
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.top" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
top
</code>
</span>
</a>
command must be run against a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-label-mongod">
mongod
</a>
instance. Running
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.top" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
top
</code>
</span>
</a>
against a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#std-label-mongos">
mongos
</a>
instance returns an error.
</p>
</div>
</div>
</dd>
</dl>
<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">
For every collection,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.top" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
top
</code>
</span>
</a>
returns the amount of
<code class="leafygreen-ui-1l06pbn">
time
</code>
, in
microseconds, that each
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-event-types" target="_self">
<span>
event
</span>
</a>
takes to execute and a
<code class="leafygreen-ui-1l06pbn">
count
</code>
of how many times each event has executed. The
<code class="leafygreen-ui-1l06pbn">
time
</code>
and
<code class="leafygreen-ui-1l06pbn">
count
</code>
metrics reset only after you restart 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.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Redaction
<a class="headerlink leafygreen-ui-1kepx47" href="#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="redaction">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/#std-label-qe-manual-feature-qe">
Queryable Encryption
</a>
, the
<code class="leafygreen-ui-1l06pbn">
top
</code>
command only returns the collection name.
</p>
</section>
</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">
Issue the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.top" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
top
</code>
</span>
</a>
command against the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-admin-database">
admin database
<!-- -->
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n {\n top: 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">top</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-event-types">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Event Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#event-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="event-fields">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.top" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
top
</code>
</span>
</a>
command returns usage statistics for the following event
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>
<code class="leafygreen-ui-1l06pbn">
total
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The combination of all
<code class="leafygreen-ui-1l06pbn">
readLock
</code>
and
<code class="leafygreen-ui-1l06pbn">
writeLock
</code>
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">
readLock
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Usage statistics for operations that use read locks. These operations
include but are not limited to queries and aggregations.
</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">
writeLock
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Usage statistics for operations that use write locks. These operations
include but are not limited to inserting, updating, and removing
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">
queries
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Usage statistics for query operations such as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/find/#mongodb-dbcommand-dbcmd.find">
<code class="leafygreen-ui-1l06pbn">
find
</code>
</a>
. The
<code class="leafygreen-ui-1l06pbn">
queries.time
</code>
and
<code class="leafygreen-ui-1l06pbn">
queries.count
</code>
fields also update
<code class="leafygreen-ui-1l06pbn">
readLock.time
</code>
and increment
<code class="leafygreen-ui-1l06pbn">
readLock.count
</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">
getmore
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Usage statistics for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
operations. The
<code class="leafygreen-ui-1l06pbn">
getmore.time
</code>
and
<code class="leafygreen-ui-1l06pbn">
getmore.count
</code>
fields also update
<code class="leafygreen-ui-1l06pbn">
readLock.time
</code>
and increment
<code class="leafygreen-ui-1l06pbn">
readLock.count
</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">
insert
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Usage statistics for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/insert/#mongodb-dbcommand-dbcmd.insert">
<code class="leafygreen-ui-1l06pbn">
insert
</code>
</a>
operations. The
<code class="leafygreen-ui-1l06pbn">
insert.time
</code>
and
<code class="leafygreen-ui-1l06pbn">
insert.count
</code>
fields also update
<code class="leafygreen-ui-1l06pbn">
readLock.time
</code>
and increment
<code class="leafygreen-ui-1l06pbn">
readLock.count
</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">
update
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Usage statistics for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/update/#mongodb-dbcommand-dbcmd.update">
<code class="leafygreen-ui-1l06pbn">
update
</code>
</a>
operations. The
<code class="leafygreen-ui-1l06pbn">
update.time
</code>
and
<code class="leafygreen-ui-1l06pbn">
update.count
</code>
fields also update
<code class="leafygreen-ui-1l06pbn">
readLock.time
</code>
and increment
<code class="leafygreen-ui-1l06pbn">
readLock.count
</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">
remove
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Usage statistics for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/delete/#mongodb-dbcommand-dbcmd.delete">
<code class="leafygreen-ui-1l06pbn">
delete
</code>
</a>
operations. The
<code class="leafygreen-ui-1l06pbn">
remove.time
</code>
and
<code class="leafygreen-ui-1l06pbn">
remove.count
</code>
fields also update
<code class="leafygreen-ui-1l06pbn">
readLock.time
</code>
and increment
<code class="leafygreen-ui-1l06pbn">
readLock.count
</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">
commands
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Usage statistics for operations such as aggregations, index creation,
and index removal. Depending on the type of command, the
<code class="leafygreen-ui-1l06pbn">
commands.time
</code>
and
<code class="leafygreen-ui-1l06pbn">
commands.count
</code>
fields update the
<code class="leafygreen-ui-1l06pbn">
writeLock
</code>
fields or the
<code class="leafygreen-ui-1l06pbn">
readLock
</code>
fields.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, aggregation operations increment
<code class="leafygreen-ui-1l06pbn">
readLock.time
</code>
and
<code class="leafygreen-ui-1l06pbn">
readLock.count
</code>
. Index creation increments
<code class="leafygreen-ui-1l06pbn">
writeLock.time
</code>
and
<code class="leafygreen-ui-1l06pbn">
writeLock.count
</code>
.
</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">
The output of the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.top" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
top
</code>
</span>
</a>
command resembles the following output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"totals\" : {\n note: \"all times in microseconds\",\n \"records.users\" : {\n \"total\" : {\n \"time\" : 305277,\n \"count\" : 2825\n },\n \"readLock\" : {\n \"time\" : 305264,\n \"count\" : 2824\n },\n \"writeLock\" : {\n \"time\" : 13,\n \"count\" : 1\n },\n \"queries\" : {\n \"time\" : 305264,\n \"count\" : 2824\n },\n \"getmore\" : {\n \"time\" : 0,\n \"count\" : 0\n },\n \"insert\" : {\n \"time\" : 0,\n \"count\" : 0\n },\n \"update\" : {\n \"time\" : 0,\n \"count\" : 0\n },\n \"remove\" : {\n \"time\" : 0,\n \"count\" : 0\n },\n \"commands\" : {\n \"time\" : 0,\n \"count\" : 0\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">"totals"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">note</span>: <span class="lg-highlight-string">"all times in microseconds"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"records.users"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"total"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"time"</span> <!-- -->: <span class="lg-highlight-number">305277</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2825</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"readLock"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"time"</span> <!-- -->: <span class="lg-highlight-number">305264</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2824</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"writeLock"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"time"</span> <!-- -->: <span class="lg-highlight-number">13</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</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">"queries"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"time"</span> <!-- -->: <span class="lg-highlight-number">305264</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2824</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"getmore"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"time"</span> <!-- -->: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</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-string">"insert"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"time"</span> <!-- -->: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</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-string">"update"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"time"</span> <!-- -->: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</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-string">"remove"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"time"</span> <!-- -->: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</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-string">"commands"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"time"</span> <!-- -->: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</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></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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/faq/concurrency/#std-label-faq-concurrency-locking">
What type of locking does MongoDB use?
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/reduce/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$reduce (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-reduce--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="-reduce--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="#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">
$reduce
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-reduce" 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.-reduce">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Applies an expression to each element in an array and combines them
into a single value.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-reduce" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$reduce
</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":"{\n $reduce: {\n input: <array>,\n initialValue: <expression>,\n in: <expression>\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">$reduce</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">input</span>: <<!-- -->array<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">initialValue</span>: <<!-- -->expression<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">in</span>: <<!-- -->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 class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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>
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">
input
</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">
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. 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>
<p class="leafygreen-ui-1kp3ins">
If the argument resolves to a value of
<code class="leafygreen-ui-1l06pbn">
null
</code>
or refers to a
missing field,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-reduce" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$reduce
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
null
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If the argument does not resolve to an array or
<code class="leafygreen-ui-1l06pbn">
null
</code>
nor refers
to a missing field,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-reduce" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$reduce
</code>
</span>
</a>
returns an error.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
initialValue
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
expression
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The initial cumulative
<code class="leafygreen-ui-1l06pbn">
value
</code>
set before
<code class="leafygreen-ui-1l06pbn">
in
</code>
is applied to the first element
of the
<code class="leafygreen-ui-1l06pbn">
input
</code>
array.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
in
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
expression
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
A valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that
<code class="leafygreen-ui-1l06pbn">
$reduce
</code>
applies
to each element in the
<code class="leafygreen-ui-1l06pbn">
input
</code>
array in left-to-right order. Wrap the
<code class="leafygreen-ui-1l06pbn">
input
</code>
value with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/reverseArray/#mongodb-expression-exp.-reverseArray">
<code class="leafygreen-ui-1l06pbn">
$reverseArray
</code>
</a>
to yield the equivalent of applying
the combining expression from right-to-left.
</p>
<p class="leafygreen-ui-1kp3ins">
During evaluation of the
<code class="leafygreen-ui-1l06pbn">
in
</code>
expression, two variables will be available:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
value
</code>
is the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/aggregation-variables/#std-label-aggregation-variables">
variable
</a>
that represents the cumulative value of the expression.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
this
</code>
is the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/aggregation-variables/#std-label-aggregation-variables">
variable
</a>
that refers to the element being processed.
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
input
</code>
resolves to an empty array,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-reduce" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$reduce
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
initialValue
</code>
.
</p>
</dd>
</dl>
<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 $reduce: {\n input: [\"a\", \"b\", \"c\"],\n initialValue: \"\",\n in: { $concat : [\"$$value\", \"$$this\"] }\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">$reduce</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">input</span>: <!-- -->[<span class="lg-highlight-string">"a"</span>, <span class="lg-highlight-string">"b"</span>, <span class="lg-highlight-string">"c"</span>]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">initialValue</span>: <span class="lg-highlight-string">""</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">in</span>: <!-- -->{ <!-- -->$concat<!-- --> <!-- -->: <!-- -->[<span class="lg-highlight-string">"$$value"</span>, <span class="lg-highlight-string">"$$this"</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">
"abc"
</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 $reduce: {\n input: [ 1, 2, 3, 4 ],\n initialValue: { sum: 5, product: 2 },\n in: {\n sum: { $add : [\"$$value.sum\", \"$$this\"] },\n product: { $multiply: [ \"$$value.product\", \"$$this\" ] }\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">$reduce</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">input</span>: <!-- -->[ <span class="lg-highlight-number">1</span>, <span class="lg-highlight-number">2</span>, <span class="lg-highlight-number">3</span>, <span class="lg-highlight-number">4</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">initialValue</span>: <!-- -->{ <span class="lg-highlight-attr">sum</span>: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-attr">product</span>: <span class="lg-highlight-number">2</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">in</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sum</span>: <!-- -->{ <!-- -->$add<!-- --> <!-- -->: <!-- -->[<span class="lg-highlight-string">"$$value.sum"</span>, <span class="lg-highlight-string">"$$this"</span>] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">product</span>: <!-- -->{ <span class="lg-highlight-attr">$multiply</span>: <!-- -->[ <span class="lg-highlight-string">"$$value.product"</span>, <span class="lg-highlight-string">"$$this"</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">
{ "sum" : 15, "product" : 48 }
</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 $reduce: {\n input: [ [ 3, 4 ], [ 5, 6 ] ],\n initialValue: [ 1, 2 ],\n in: { $concatArrays : [\"$$value\", \"$$this\"] }\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">$reduce</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">input</span>: <!-- -->[ <!-- -->[ <span class="lg-highlight-number">3</span>, <span class="lg-highlight-number">4</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-number">5</span>, <span class="lg-highlight-number">6</span> ] ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">initialValue</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">in</span>: <!-- -->{ <!-- -->$concatArrays<!-- --> <!-- -->: <!-- -->[<span class="lg-highlight-string">"$$value"</span>, <span class="lg-highlight-string">"$$this"</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">
[ 1, 2, 3, 4, 5, 6 ]
</code>
</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">
Multiplication
<a class="headerlink leafygreen-ui-1kepx47" href="#multiplication" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="multiplication">
</div>
</a>
</h3>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Probability
<a class="headerlink leafygreen-ui-1kepx47" href="#probability" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="probability">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
A collection named
<code class="leafygreen-ui-1l06pbn">
events
</code>
contains the events of a probability
experiment. Each experiment can have multiple
<code class="leafygreen-ui-1l06pbn">
events
</code>
, such as
rolling a die several times or drawing several cards
<strong>
(without replacement)
</strong>
in succession to achieve a desired result. In order to obtain the
overall probability of the experiment, we will need to multiply the
probability of each event in the experiment.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.events.insertMany( [\n { _id : 1, type : \"die\", experimentId :\"r5\", description : \"Roll a 5\", eventNum : 1, probability : 0.16666666666667 },\n { _id : 2, type : \"card\", experimentId :\"d3rc\", description : \"Draw 3 red cards\", eventNum : 1, probability : 0.5 },\n { _id : 3, type : \"card\", experimentId :\"d3rc\", description : \"Draw 3 red cards\", eventNum : 2, probability : 0.49019607843137 },\n { _id : 4, type : \"card\", experimentId :\"d3rc\", description : \"Draw 3 red cards\", eventNum : 3, probability : 0.48 },\n { _id : 5, type : \"die\", experimentId :\"r16\", description : \"Roll a 1 then a 6\", eventNum : 1, probability : 0.16666666666667 },\n { _id : 6, type : \"die\", experimentId :\"r16\", description : \"Roll a 1 then a 6\", eventNum : 2, probability : 0.16666666666667 },\n { _id : 7, type : \"card\", experimentId :\"dak\", description : \"Draw an ace, then a king\", eventNum : 1, probability : 0.07692307692308 },\n { _id : 8, type : \"card\", experimentId :\"dak\", description : \"Draw an ace, then a king\", eventNum : 2, probability : 0.07843137254902 }\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">events</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>, <!-- -->type <!-- -->: <span class="lg-highlight-string">"die"</span>, <!-- -->experimentId <!-- -->:<span class="lg-highlight-string">"r5"</span>, <!-- -->description <!-- -->: <span class="lg-highlight-string">"Roll a 5"</span>, <!-- -->eventNum <!-- -->: <span class="lg-highlight-number">1</span>, <!-- -->probability <!-- -->: <span class="lg-highlight-number">0.16666666666667</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">2</span>, <!-- -->type <!-- -->: <span class="lg-highlight-string">"card"</span>, <!-- -->experimentId <!-- -->:<span class="lg-highlight-string">"d3rc"</span>, <!-- -->description <!-- -->: <span class="lg-highlight-string">"Draw 3 red cards"</span>, <!-- -->eventNum <!-- -->: <span class="lg-highlight-number">1</span>, <!-- -->probability <!-- -->: <span class="lg-highlight-number">0.5</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">3</span>, <!-- -->type <!-- -->: <span class="lg-highlight-string">"card"</span>, <!-- -->experimentId <!-- -->:<span class="lg-highlight-string">"d3rc"</span>, <!-- -->description <!-- -->: <span class="lg-highlight-string">"Draw 3 red cards"</span>, <!-- -->eventNum <!-- -->: <span class="lg-highlight-number">2</span>, <!-- -->probability <!-- -->: <span class="lg-highlight-number">0.49019607843137</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">4</span>, <!-- -->type <!-- -->: <span class="lg-highlight-string">"card"</span>, <!-- -->experimentId <!-- -->:<span class="lg-highlight-string">"d3rc"</span>, <!-- -->description <!-- -->: <span class="lg-highlight-string">"Draw 3 red cards"</span>, <!-- -->eventNum <!-- -->: <span class="lg-highlight-number">3</span>, <!-- -->probability <!-- -->: <span class="lg-highlight-number">0.48</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">5</span>, <!-- -->type <!-- -->: <span class="lg-highlight-string">"die"</span>, <!-- -->experimentId <!-- -->:<span class="lg-highlight-string">"r16"</span>, <!-- -->description <!-- -->: <span class="lg-highlight-string">"Roll a 1 then a 6"</span>, <!-- -->eventNum <!-- -->: <span class="lg-highlight-number">1</span>, <!-- -->probability <!-- -->: <span class="lg-highlight-number">0.16666666666667</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">6</span>, <!-- -->type <!-- -->: <span class="lg-highlight-string">"die"</span>, <!-- -->experimentId <!-- -->:<span class="lg-highlight-string">"r16"</span>, <!-- -->description <!-- -->: <span class="lg-highlight-string">"Roll a 1 then a 6"</span>, <!-- -->eventNum <!-- -->: <span class="lg-highlight-number">2</span>, <!-- -->probability <!-- -->: <span class="lg-highlight-number">0.16666666666667</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">7</span>, <!-- -->type <!-- -->: <span class="lg-highlight-string">"card"</span>, <!-- -->experimentId <!-- -->:<span class="lg-highlight-string">"dak"</span>, <!-- -->description <!-- -->: <span class="lg-highlight-string">"Draw an ace, then a king"</span>, <!-- -->eventNum <!-- -->: <span class="lg-highlight-number">1</span>, <!-- -->probability <!-- -->: <span class="lg-highlight-number">0.07692307692308</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">8</span>, <!-- -->type <!-- -->: <span class="lg-highlight-string">"card"</span>, <!-- -->experimentId <!-- -->:<span class="lg-highlight-string">"dak"</span>, <!-- -->description <!-- -->: <span class="lg-highlight-string">"Draw an ace, then a king"</span>, <!-- -->eventNum <!-- -->: <span class="lg-highlight-number">2</span>, <!-- -->probability <!-- -->: <span class="lg-highlight-number">0.07843137254902</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">
<strong>
Steps
</strong>
:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use
<code class="leafygreen-ui-1l06pbn">
$group
</code>
to group by the
<code class="leafygreen-ui-1l06pbn">
experimentId
</code>
and use
<code class="leafygreen-ui-1l06pbn">
$push
</code>
to
create an array with the probability of each event.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use
<code class="leafygreen-ui-1l06pbn">
$reduce
</code>
with
<code class="leafygreen-ui-1l06pbn">
$multiply
</code>
to multiply and combine the
elements of
<code class="leafygreen-ui-1l06pbn">
probabilityArr
</code>
into a single value and project it.
</p>
</li>
</ol>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.probability.aggregate(\n [\n {\n $group: {\n _id: \"$experimentId\",\n probabilityArr: { $push: \"$probability\" }\n }\n },\n {\n $project: {\n description: 1,\n results: {\n $reduce: {\n input: \"$probabilityArr\",\n initialValue: 1,\n in: { $multiply: [ \"$$value\", \"$$this\" ] }\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">probability</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">$group</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$experimentId"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">probabilityArr</span>: <!-- -->{ <span class="lg-highlight-attr">$push</span>: <span class="lg-highlight-string">"$probability"</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">$project</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">results</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$reduce</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"$probabilityArr"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">initialValue</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">in</span>: <!-- -->{ <span class="lg-highlight-attr">$multiply</span>: <!-- -->[ <span class="lg-highlight-string">"$$value"</span>, <span class="lg-highlight-string">"$$this"</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">
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 : \"dak\", results : 0.00603318250377101 }\n{ _id : \"r5\", results : 0.16666666666667 }\n{ _id : \"r16\", results : 0.027777777777778886 }\n{ _id : \"d3rc\", results : 0.11764705882352879 }","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">{ <!-- -->_id <!-- -->: <span class="lg-highlight-string">"dak"</span>, <!-- -->results <!-- -->: <span class="lg-highlight-number">0.00603318250377101</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-string">"r5"</span>, <!-- -->results <!-- -->: <span class="lg-highlight-number">0.16666666666667</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-string">"r16"</span>, <!-- -->results <!-- -->: <span class="lg-highlight-number">0.027777777777778886</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-string">"d3rc"</span>, <!-- -->results <!-- -->: <span class="lg-highlight-number">0.11764705882352879</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Discounted Merchandise
<a class="headerlink leafygreen-ui-1kepx47" href="#discounted-merchandise" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="discounted-merchandise">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
A collection named
<code class="leafygreen-ui-1l06pbn">
clothes
</code>
contains the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.clothes.insertMany( [\n { _id : 1, productId : \"ts1\", description : \"T-Shirt\", color : \"black\", size : \"M\", price : 20, discounts : [ 0.5, 0.1 ] },\n { _id : 2, productId : \"j1\", description : \"Jeans\", color : \"blue\", size : \"36\", price : 40, discounts : [ 0.25, 0.15, 0.05 ] },\n { _id : 3, productId : \"s1\", description : \"Shorts\", color : \"beige\", size : \"32\", price : 30, discounts : [ 0.15, 0.05 ] },\n { _id : 4, productId : \"ts2\", description : \"Cool T-Shirt\", color : \"White\", size : \"L\", price : 25, discounts : [ 0.3 ] },\n { _id : 5, productId : \"j2\", description : \"Designer Jeans\", color : \"blue\", size : \"30\", price : 80, discounts : [ 0.1, 0.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">clothes</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>, <!-- -->productId <!-- -->: <span class="lg-highlight-string">"ts1"</span>, <!-- -->description <!-- -->: <span class="lg-highlight-string">"T-Shirt"</span>, <!-- -->color <!-- -->: <span class="lg-highlight-string">"black"</span>, <!-- -->size <!-- -->: <span class="lg-highlight-string">"M"</span>, <!-- -->price <!-- -->: <span class="lg-highlight-number">20</span>, <!-- -->discounts <!-- -->: <!-- -->[ <span class="lg-highlight-number">0.5</span>, <span class="lg-highlight-number">0.1</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">2</span>, <!-- -->productId <!-- -->: <span class="lg-highlight-string">"j1"</span>, <!-- -->description <!-- -->: <span class="lg-highlight-string">"Jeans"</span>, <!-- -->color <!-- -->: <span class="lg-highlight-string">"blue"</span>, <!-- -->size <!-- -->: <span class="lg-highlight-string">"36"</span>, <!-- -->price <!-- -->: <span class="lg-highlight-number">40</span>, <!-- -->discounts <!-- -->: <!-- -->[ <span class="lg-highlight-number">0.25</span>, <span class="lg-highlight-number">0.15</span>, <span class="lg-highlight-number">0.05</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">3</span>, <!-- -->productId <!-- -->: <span class="lg-highlight-string">"s1"</span>, <!-- -->description <!-- -->: <span class="lg-highlight-string">"Shorts"</span>, <!-- -->color <!-- -->: <span class="lg-highlight-string">"beige"</span>, <!-- -->size <!-- -->: <span class="lg-highlight-string">"32"</span>, <!-- -->price <!-- -->: <span class="lg-highlight-number">30</span>, <!-- -->discounts <!-- -->: <!-- -->[ <span class="lg-highlight-number">0.15</span>, <span class="lg-highlight-number">0.05</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">4</span>, <!-- -->productId <!-- -->: <span class="lg-highlight-string">"ts2"</span>, <!-- -->description <!-- -->: <span class="lg-highlight-string">"Cool T-Shirt"</span>, <!-- -->color <!-- -->: <span class="lg-highlight-string">"White"</span>, <!-- -->size <!-- -->: <span class="lg-highlight-string">"L"</span>, <!-- -->price <!-- -->: <span class="lg-highlight-number">25</span>, <!-- -->discounts <!-- -->: <!-- -->[ <span class="lg-highlight-number">0.3</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">5</span>, <!-- -->productId <!-- -->: <span class="lg-highlight-string">"j2"</span>, <!-- -->description <!-- -->: <span class="lg-highlight-string">"Designer Jeans"</span>, <!-- -->color <!-- -->: <span class="lg-highlight-string">"blue"</span>, <!-- -->size <!-- -->: <span class="lg-highlight-string">"30"</span>, <!-- -->price <!-- -->: <span class="lg-highlight-number">80</span>, <!-- -->discounts <!-- -->: <!-- -->[ <span class="lg-highlight-number">0.1</span>, <span class="lg-highlight-number">0.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">
Each document contains a
<code class="leafygreen-ui-1l06pbn">
discounts
</code>
array containing the currently
available percent-off coupons for each item. If each discount can be
applied to the product once, we can calculate the lowest price by using
<code class="leafygreen-ui-1l06pbn">
$reduce
</code>
to apply the following formula for each element in the
<code class="leafygreen-ui-1l06pbn">
discounts
</code>
array: (1 - discount) * price.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.clothes.aggregate(\n [\n {\n $project: {\n discountedPrice: {\n $reduce: {\n input: \"$discounts\",\n initialValue: \"$price\",\n in: { $multiply: [ \"$$value\", { $subtract: [ 1, \"$$this\" ] } ] }\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">clothes</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">discountedPrice</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$reduce</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"$discounts"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">initialValue</span>: <span class="lg-highlight-string">"$price"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">in</span>: <!-- -->{ <span class="lg-highlight-attr">$multiply</span>: <!-- -->[ <span class="lg-highlight-string">"$$value"</span>, <!-- -->{ <span class="lg-highlight-attr">$subtract</span>: <!-- -->[ <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"$$this"</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">
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 : ObjectId(\"57c893067054e6e47674ce01\"), discountedPrice : 9 }\n{ _id : ObjectId(\"57c9932b7054e6e47674ce12\"), discountedPrice : 24.224999999999998 }\n{ _id : ObjectId(\"57c993457054e6e47674ce13\"), discountedPrice : 24.224999999999998 }\n{ _id : ObjectId(\"57c993687054e6e47674ce14\"), discountedPrice : 17.5 }\n{ _id : ObjectId(\"57c993837054e6e47674ce15\"), discountedPrice : 54 }","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">{ <!-- -->_id <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"57c893067054e6e47674ce01"</span>)<!-- -->, <!-- -->discountedPrice <!-- -->: <span class="lg-highlight-number">9</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"57c9932b7054e6e47674ce12"</span>)<!-- -->, <!-- -->discountedPrice <!-- -->: <span class="lg-highlight-number">24.224999999999998</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"57c993457054e6e47674ce13"</span>)<!-- -->, <!-- -->discountedPrice <!-- -->: <span class="lg-highlight-number">24.224999999999998</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"57c993687054e6e47674ce14"</span>)<!-- -->, <!-- -->discountedPrice <!-- -->: <span class="lg-highlight-number">17.5</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"57c993837054e6e47674ce15"</span>)<!-- -->, <!-- -->discountedPrice <!-- -->: <span class="lg-highlight-number">54</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
String Concatenation
<a class="headerlink leafygreen-ui-1kepx47" href="#string-concatenation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="string-concatenation">
</div>
</a>
</h3>
<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":"db.people.insertMany( [\n { _id : 1, name : \"Melissa\", hobbies : [ \"softball\", \"drawing\", \"reading\" ] },\n { _id : 2, name : \"Brad\", hobbies : [ \"gaming\", \"skateboarding\" ] },\n { _id : 3, name : \"Scott\", hobbies : [ \"basketball\", \"music\", \"fishing\" ] },\n { _id : 4, name : \"Tracey\", hobbies : [ \"acting\", \"yoga\" ] },\n { _id : 5, name : \"Josh\", hobbies : [ \"programming\" ] },\n { _id : 6, name : \"Claire\" }\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">people</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>, <!-- -->name <!-- -->: <span class="lg-highlight-string">"Melissa"</span>, <!-- -->hobbies <!-- -->: <!-- -->[ <span class="lg-highlight-string">"softball"</span>, <span class="lg-highlight-string">"drawing"</span>, <span class="lg-highlight-string">"reading"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">2</span>, <!-- -->name <!-- -->: <span class="lg-highlight-string">"Brad"</span>, <!-- -->hobbies <!-- -->: <!-- -->[ <span class="lg-highlight-string">"gaming"</span>, <span class="lg-highlight-string">"skateboarding"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">3</span>, <!-- -->name <!-- -->: <span class="lg-highlight-string">"Scott"</span>, <!-- -->hobbies <!-- -->: <!-- -->[ <span class="lg-highlight-string">"basketball"</span>, <span class="lg-highlight-string">"music"</span>, <span class="lg-highlight-string">"fishing"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">4</span>, <!-- -->name <!-- -->: <span class="lg-highlight-string">"Tracey"</span>, <!-- -->hobbies <!-- -->: <!-- -->[ <span class="lg-highlight-string">"acting"</span>, <span class="lg-highlight-string">"yoga"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">5</span>, <!-- -->name <!-- -->: <span class="lg-highlight-string">"Josh"</span>, <!-- -->hobbies <!-- -->: <!-- -->[ <span class="lg-highlight-string">"programming"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">6</span>, <!-- -->name <!-- -->: <span class="lg-highlight-string">"Claire"</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 reduces the
<code class="leafygreen-ui-1l06pbn">
hobbies
</code>
array of strings into a single string
<code class="leafygreen-ui-1l06pbn">
bio
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.people.aggregate(\n [\n // Filter to return only non-empty arrays\n { $match: { \"hobbies\": { $gt: [ ] } } },\n {\n $project: {\n name: 1,\n bio: {\n $reduce: {\n input: \"$hobbies\",\n initialValue: \"My hobbies include:\",\n in: {\n $concat: [\n \"$$value\",\n {\n $cond: {\n if: { $eq: [ \"$$value\", \"My hobbies include:\" ] },\n then: \" \",\n else: \", \"\n }\n },\n \"$$this\"\n ]\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-property">people</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-comment">//</span><span class="lg-highlight-comment"> Filter to return </span><span class="lg-highlight-comment">only non-empty arrays</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$match</span>: <!-- -->{ <span class="lg-highlight-string">"hobbies"</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</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"> <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">bio</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$reduce</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"$hobbies"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">initialValue</span>: <span class="lg-highlight-string">"My hobbies include:"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">in</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$concat</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$$value"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$cond</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">if</span>: <!-- -->{ <span class="lg-highlight-attr">$eq</span>: <!-- -->[ <span class="lg-highlight-string">"$$value"</span>, <span class="lg-highlight-string">"My hobbies include:"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">" "</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">else</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"> <span class="lg-highlight-string">"$$this"</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><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 : \"Melissa\", bio : \"My hobbies include: softball, drawing, reading\" }\n{ _id : 2, name : \"Brad\", bio : \"My hobbies include: gaming, skateboarding\" }\n{ _id : 3, name : \"Scott\", bio : \"My hobbies include: basketball, music, fishing\" }\n{ _id : 4, name : \"Tracey\", bio : \"My hobbies include: acting, yoga\" }\n{ _id : 5, name : \"Josh\", bio : \"My hobbies include: programming\" }","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">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">1</span>, <!-- -->name <!-- -->: <span class="lg-highlight-string">"Melissa"</span>, <!-- -->bio <!-- -->: <span class="lg-highlight-string">"My hobbies include: softball, drawing, reading"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">2</span>, <!-- -->name <!-- -->: <span class="lg-highlight-string">"Brad"</span>, <!-- -->bio <!-- -->: <span class="lg-highlight-string">"My hobbies include: gaming, skateboarding"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">3</span>, <!-- -->name <!-- -->: <span class="lg-highlight-string">"Scott"</span>, <!-- -->bio <!-- -->: <span class="lg-highlight-string">"My hobbies include: basketball, music, fishing"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">4</span>, <!-- -->name <!-- -->: <span class="lg-highlight-string">"Tracey"</span>, <!-- -->bio <!-- -->: <span class="lg-highlight-string">"My hobbies include: acting, yoga"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">5</span>, <!-- -->name <!-- -->: <span class="lg-highlight-string">"Josh"</span>, <!-- -->bio <!-- -->: <span class="lg-highlight-string">"My hobbies include: programming"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Array Concatenation
<a class="headerlink leafygreen-ui-1kepx47" href="#array-concatenation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="array-concatenation">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
A collection named
<code class="leafygreen-ui-1l06pbn">
matrices
</code>
contains the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.matrices.insertMany( [\n { _id : 1, arr : [ [ 24, 55, 79 ], [ 14, 78, 35 ], [ 84, 90, 3 ], [ 50, 89, 70 ] ] },\n { _id : 2, arr : [ [ 39, 32, 43, 7 ], [ 62, 17, 80, 64 ], [ 17, 88, 11, 73 ] ] },\n { _id : 3, arr : [ [ 42 ], [ 26, 59 ], [ 17 ], [ 72, 19, 35 ] ] },\n { _id : 4 }\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">matrices</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>, <!-- -->arr <!-- -->: <!-- -->[ <!-- -->[ <span class="lg-highlight-number">24</span>, <span class="lg-highlight-number">55</span>, <span class="lg-highlight-number">79</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-number">14</span>, <span class="lg-highlight-number">78</span>, <span class="lg-highlight-number">35</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-number">84</span>, <span class="lg-highlight-number">90</span>, <span class="lg-highlight-number">3</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-number">50</span>, <span class="lg-highlight-number">89</span>, <span class="lg-highlight-number">70</span> ] ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">2</span>, <!-- -->arr <!-- -->: <!-- -->[ <!-- -->[ <span class="lg-highlight-number">39</span>, <span class="lg-highlight-number">32</span>, <span class="lg-highlight-number">43</span>, <span class="lg-highlight-number">7</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-number">62</span>, <span class="lg-highlight-number">17</span>, <span class="lg-highlight-number">80</span>, <span class="lg-highlight-number">64</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-number">17</span>, <span class="lg-highlight-number">88</span>, <span class="lg-highlight-number">11</span>, <span class="lg-highlight-number">73</span> ] ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">3</span>, <!-- -->arr <!-- -->: <!-- -->[ <!-- -->[ <span class="lg-highlight-number">42</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-number">26</span>, <span class="lg-highlight-number">59</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-number">17</span> ]<!-- -->, <!-- -->[ <span class="lg-highlight-number">72</span>, <span class="lg-highlight-number">19</span>, <span class="lg-highlight-number">35</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>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Computing a Single Reduction
<a class="headerlink leafygreen-ui-1kepx47" href="#computing-a-single-reduction" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-a-single-reduction">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following example collapses the two dimensional arrays into a single array
<code class="leafygreen-ui-1l06pbn">
collapsed
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.arrayconcat.aggregate(\n [\n {\n $project: {\n collapsed: {\n $reduce: {\n input: \"$arr\",\n initialValue: [ ],\n in: { $concatArrays: [ \"$$value\", \"$$this\" ] }\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">arrayconcat</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">collapsed</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$reduce</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"$arr"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">initialValue</span>: <!-- -->[ <!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">in</span>: <!-- -->{ <span class="lg-highlight-attr">$concatArrays</span>: <!-- -->[ <span class="lg-highlight-string">"$$value"</span>, <span class="lg-highlight-string">"$$this"</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">
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, collapsed : [ 24, 55, 79, 14, 78, 35, 84, 90, 3, 50, 89, 70 ] }\n{ _id : 2, collapsed : [ 39, 32, 43, 7, 62, 17, 80, 64, 17, 88, 11, 73 ] }\n{ _id : 3, collapsed : [ 42, 26, 59, 17, 72, 19, 35 ] }\n{ _id : 4, collapsed : null }","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">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">1</span>, <!-- -->collapsed <!-- -->: <!-- -->[ <span class="lg-highlight-number">24</span>, <span class="lg-highlight-number">55</span>, <span class="lg-highlight-number">79</span>, <span class="lg-highlight-number">14</span>, <span class="lg-highlight-number">78</span>, <span class="lg-highlight-number">35</span>, <span class="lg-highlight-number">84</span>, <span class="lg-highlight-number">90</span>, <span class="lg-highlight-number">3</span>, <span class="lg-highlight-number">50</span>, <span class="lg-highlight-number">89</span>, <span class="lg-highlight-number">70</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">2</span>, <!-- -->collapsed <!-- -->: <!-- -->[ <span class="lg-highlight-number">39</span>, <span class="lg-highlight-number">32</span>, <span class="lg-highlight-number">43</span>, <span class="lg-highlight-number">7</span>, <span class="lg-highlight-number">62</span>, <span class="lg-highlight-number">17</span>, <span class="lg-highlight-number">80</span>, <span class="lg-highlight-number">64</span>, <span class="lg-highlight-number">17</span>, <span class="lg-highlight-number">88</span>, <span class="lg-highlight-number">11</span>, <span class="lg-highlight-number">73</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">3</span>, <!-- -->collapsed <!-- -->: <!-- -->[ <span class="lg-highlight-number">42</span>, <span class="lg-highlight-number">26</span>, <span class="lg-highlight-number">59</span>, <span class="lg-highlight-number">17</span>, <span class="lg-highlight-number">72</span>, <span class="lg-highlight-number">19</span>, <span class="lg-highlight-number">35</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">4</span>, <!-- -->collapsed <!-- -->: <span class="lg-highlight-literal">null</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Computing a Multiple Reductions
<a class="headerlink leafygreen-ui-1kepx47" href="#computing-a-multiple-reductions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-a-multiple-reductions">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following example performs the same two dimensional array collapse as the example above, but also
creates a new array containing only the first element of each array.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.arrayconcat.aggregate(\n [\n {\n $project: {\n results: {\n $reduce: {\n input: \"$arr\",\n initialValue: [ ],\n in: {\n collapsed: {\n $concatArrays: [ \"$$value.collapsed\", \"$$this\" ]\n },\n firstValues: {\n $concatArrays: [ \"$$value.firstValues\", { $slice: [ \"$$this\", 1 ] } ]\n }\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-property">arrayconcat</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">results</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$reduce</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"$arr"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">initialValue</span>: <!-- -->[ <!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">in</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">collapsed</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$concatArrays</span>: <!-- -->[ <span class="lg-highlight-string">"$$value.collapsed"</span>, <span class="lg-highlight-string">"$$this"</span> ]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">firstValues</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$concatArrays</span>: <!-- -->[ <span class="lg-highlight-string">"$$value.firstValues"</span>, <!-- -->{ <span class="lg-highlight-attr">$slice</span>: <!-- -->[ <span class="lg-highlight-string">"$$this"</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><tr 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, results : { collapsed : [ 24, 55, 79, 14, 78, 35, 84, 90, 3, 50, 89, 70 ], firstValues : [ 24, 14, 84, 50 ] } }\n{ _id : 2, results : { collapsed : [ 39, 32, 43, 7, 62, 17, 80, 64, 17, 88, 11, 73 ], firstValues : [ 39, 62, 17 ] } }\n{ _id : 3, results : { collapsed : [ 42, 26, 59, 17, 72, 19, 35 ], firstValues : [ 42, 26, 17, 72 ] } }\n{ _id : 4, results : null }","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">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">1</span>, <!-- -->results <!-- -->: <!-- -->{ <!-- -->collapsed <!-- -->: <!-- -->[ <span class="lg-highlight-number">24</span>, <span class="lg-highlight-number">55</span>, <span class="lg-highlight-number">79</span>, <span class="lg-highlight-number">14</span>, <span class="lg-highlight-number">78</span>, <span class="lg-highlight-number">35</span>, <span class="lg-highlight-number">84</span>, <span class="lg-highlight-number">90</span>, <span class="lg-highlight-number">3</span>, <span class="lg-highlight-number">50</span>, <span class="lg-highlight-number">89</span>, <span class="lg-highlight-number">70</span> ]<!-- -->, <!-- -->firstValues <!-- -->: <!-- -->[ <span class="lg-highlight-number">24</span>, <span class="lg-highlight-number">14</span>, <span class="lg-highlight-number">84</span>, <span class="lg-highlight-number">50</span> ] } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">2</span>, <!-- -->results <!-- -->: <!-- -->{ <!-- -->collapsed <!-- -->: <!-- -->[ <span class="lg-highlight-number">39</span>, <span class="lg-highlight-number">32</span>, <span class="lg-highlight-number">43</span>, <span class="lg-highlight-number">7</span>, <span class="lg-highlight-number">62</span>, <span class="lg-highlight-number">17</span>, <span class="lg-highlight-number">80</span>, <span class="lg-highlight-number">64</span>, <span class="lg-highlight-number">17</span>, <span class="lg-highlight-number">88</span>, <span class="lg-highlight-number">11</span>, <span class="lg-highlight-number">73</span> ]<!-- -->, <!-- -->firstValues <!-- -->: <!-- -->[ <span class="lg-highlight-number">39</span>, <span class="lg-highlight-number">62</span>, <span class="lg-highlight-number">17</span> ] } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">3</span>, <!-- -->results <!-- -->: <!-- -->{ <!-- -->collapsed <!-- -->: <!-- -->[ <span class="lg-highlight-number">42</span>, <span class="lg-highlight-number">26</span>, <span class="lg-highlight-number">59</span>, <span class="lg-highlight-number">17</span>, <span class="lg-highlight-number">72</span>, <span class="lg-highlight-number">19</span>, <span class="lg-highlight-number">35</span> ]<!-- -->, <!-- -->firstValues <!-- -->: <!-- -->[ <span class="lg-highlight-number">42</span>, <span class="lg-highlight-number">26</span>, <span class="lg-highlight-number">17</span>, <span class="lg-highlight-number">72</span> ] } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">4</span>, <!-- -->results <!-- -->: <span class="lg-highlight-literal">null</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/release-notes/5.0-downgrade-sharded-cluster/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Downgrade
<!-- -->
5.0
<!-- -->
Sharded Cluster to
<!-- -->
4.4
<a class="headerlink leafygreen-ui-1kepx47" href="#downgrade-newversion-sharded-cluster-to-oldversion" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="downgrade-newversion-sharded-cluster-to-oldversion">
</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="#downgrade-path" target="_self">
<span>
Downgrade Path
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#create-backup" target="_self">
<span>
Create Backup
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" 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>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
Before you attempt any downgrade, familiarize yourself with the content
of this document.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Downgrade Path
<a class="headerlink leafygreen-ui-1kepx47" href="#downgrade-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="downgrade-path">
</div>
</a>
</h2>
<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 you upgrade or downgrade a replica set, ensure all replica set
members are running. If you do not, the upgrade or downgrade will not
complete until all members are started.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If you need to downgrade from
<!-- -->
5.0
<!-- -->
, downgrade to the latest
patch release of
<!-- -->
4.4
<!-- -->
.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB only supports single-version downgrades. You cannot downgrade to a
release that is multiple versions behind your current release.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, you may downgrade a
<!-- -->
5.0-series
<!-- -->
to a
<!-- -->
4.4-series
<!-- -->
deployment.
However, further downgrading that
<!-- -->
4.4-series
<!-- -->
deployment to a
<!-- -->
4.2-series
<!-- -->
deployment is not supported.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Create Backup
<a class="headerlink leafygreen-ui-1kepx47" href="#create-backup" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-backup">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<em>
Optional but Recommended.
</em>
Create a backup of your database.
</p>
</section>
<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">
To downgrade from
<!-- -->
5.0
<!-- -->
to
<!-- -->
4.4
<!-- -->
, you must remove
incompatible features that are persisted and/or update incompatible
configuration settings. These include:
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
1. Cluster Default Read and Write Concerns
<a class="headerlink leafygreen-ui-1kepx47" href="#1.-cluster-default-read-and-write-concerns" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="1.-cluster-default-read-and-write-concerns">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB 5.0 changed the default value for cluster-wide read and write
concerns, and downgrading to MongoDB 4.4 might change those defaults
back. Consider manually configuring your cluster's default read and
write concern before downgrading:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To manually configure a default value for a cluster's read or write
concern, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setDefaultRWConcern/#mongodb-dbcommand-dbcmd.setDefaultRWConcern">
<code class="leafygreen-ui-1l06pbn">
setDefaultRWConcern
</code>
</a>
command.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If your cluster includes an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-arbiter/">
arbiter
</a>
, and you had previously disabled
<code class="leafygreen-ui-1l06pbn">
"Majority"
</code>
read concern to prevent cache pressure in certain
situations, you may want to configure
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--enableMajorityReadConcern">
<code class="leafygreen-ui-1l06pbn">
--enableMajorityReadConcern false
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-replication.enableMajorityReadConcern">
<code class="leafygreen-ui-1l06pbn">
replication.enableMajorityReadConcern: false
</code>
</a>
once you downgrade.
</p>
</li>
</ul>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
2. Document Fields with
<code class="leafygreen-ui-1l06pbn">
.
</code>
or
<code class="leafygreen-ui-1l06pbn">
$
</code>
Characters
<a class="headerlink leafygreen-ui-1kepx47" href="#2.-document-fields-with-.-or---characters" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="2.-document-fields-with-.-or---characters">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB 5.0 adds support for including the
<code class="leafygreen-ui-1l06pbn">
.
</code>
or
<code class="leafygreen-ui-1l06pbn">
$
</code>
characters in
document field names. You must delete any documents containing field
names that include the
<code class="leafygreen-ui-1l06pbn">
.
</code>
or
<code class="leafygreen-ui-1l06pbn">
$
</code>
characters before downgrading
to MongoDB 4.4.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
3. Slim-format Timezone Data Files
<a class="headerlink leafygreen-ui-1kepx47" href="#3.-slim-format-timezone-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="3.-slim-format-timezone-data-files">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB 5.0 enables support for slim-format timezone data files. If
using slim-format timezone data files in your deployment, as provided
to MongoDB with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--timeZoneInfo">
<code class="leafygreen-ui-1l06pbn">
--timeZoneInfo
</code>
</a>
command line option or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-processManagement.timeZoneInfo">
<code class="leafygreen-ui-1l06pbn">
processManagement.timeZoneInfo
</code>
</a>
configuration file setting, you must downgrade to MongoDB 4.4.7 or
later, or else revert your timezone data files to use the previous
non-slim-format data files.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
4. Resharding
<a class="headerlink leafygreen-ui-1kepx47" href="#4.-resharding" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="4.-resharding">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Ensure that any
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-reshard-a-collection/#std-label-sharding-resharding">
resharding operations
</a>
have
successfully completed before attempting any downgrade procedure. If a
recent resharding operation has failed due to a primary failover, you
must first run the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/cleanupReshardCollection/#mongodb-dbcommand-dbcmd.cleanupReshardCollection">
<code class="leafygreen-ui-1l06pbn">
cleanupReshardCollection
</code>
</a>
command before
downgrading the
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
of your sharded cluster.
</p>
<p class="leafygreen-ui-1kp3ins">
If a resharding operation is still running while you downgrade the
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
of your sharded cluster, the resharding
operation will abort.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-5.0-downgrade-feature-compatibility-sharded-cluster">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
5. Downgrade Feature Compatibility Version (fCV)
<a class="headerlink leafygreen-ui-1kepx47" href="#5.-downgrade-feature-compatibility-version--fcv-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="5.-downgrade-feature-compatibility-version--fcv-">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
First, verify the following:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Ensure that no initial sync is in progress. Running
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setFeatureCompatibilityVersion/#mongodb-dbcommand-dbcmd.setFeatureCompatibilityVersion">
<code class="leafygreen-ui-1l06pbn">
setFeatureCompatibilityVersion
</code>
</a>
command while an
initial sync is in progress will cause the initial sync to restart.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Ensure that no nodes have a
<code class="leafygreen-ui-1l06pbn">
newlyAdded
</code>
field in their
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/">
replica set configuration
</a>
.
Run the following command on each node in your sharded cluster to
verify this:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use local\ndb.system.replset.find( { \"members.newlyAdded\" : { $exists : 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">use local</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">system</span>.<span class="lg-highlight-property">replset</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-string">"members.newlyAdded"</span> <!-- -->: <!-- -->{ <!-- -->$exists<!-- --> <!-- -->: <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">
newlyAdded
</code>
field only appears in a node's replica set
configuration document during and shortly after an initial sync.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Ensure that no replica set member is in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-states/#mongodb-replstate-replstate.ROLLBACK">
<code class="leafygreen-ui-1l06pbn">
ROLLBACK
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-states/#mongodb-replstate-replstate.RECOVERING">
<code class="leafygreen-ui-1l06pbn">
RECOVERING
</code>
</a>
state.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Next, to downgrade the
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
of your sharded
cluster:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Connect a
<code class="leafygreen-ui-1l06pbn">
mongo
</code>
shell to 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
<!-- -->
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Downgrade the
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
to
<code class="leafygreen-ui-1l06pbn">
"4.4"
</code>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand({setFeatureCompatibilityVersion: \"4.4\"})","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">setFeatureCompatibilityVersion</span>: <span class="lg-highlight-string">"4.4"</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/command/setFeatureCompatibilityVersion/#mongodb-dbcommand-dbcmd.setFeatureCompatibilityVersion">
<code class="leafygreen-ui-1l06pbn">
setFeatureCompatibilityVersion
</code>
</a>
command performs writes
to an internal system collection and is idempotent. If for any reason
the command does not complete successfully, retry the command on 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
<!-- -->
.
</p>
<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">
While
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setFeatureCompatibilityVersion/#mongodb-dbcommand-dbcmd.setFeatureCompatibilityVersion">
<code class="leafygreen-ui-1l06pbn">
setFeatureCompatibilityVersion
</code>
</a>
is running on
the sharded cluster, chunk migrations, splits, and merges can
fail with
<code class="leafygreen-ui-1l06pbn">
ConflictingOperationInProgress
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setFeatureCompatibilityVersion/#mongodb-dbcommand-dbcmd.setFeatureCompatibilityVersion">
<code class="leafygreen-ui-1l06pbn">
setFeatureCompatibilityVersion
</code>
</a>
fails with a
<code class="leafygreen-ui-1l06pbn">
ManualInterventionRequired
</code>
error, and the cluster has
recently undergone a resharding operation that had failed due to
an election, you must run the
<code class="leafygreen-ui-1l06pbn">
cleanupReshardCollection
</code>
command before attempting
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setFeatureCompatibilityVersion/#mongodb-dbcommand-dbcmd.setFeatureCompatibilityVersion">
<code class="leafygreen-ui-1l06pbn">
setFeatureCompatibilityVersion
</code>
</a>
again.
</p>
</li>
</ul>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To ensure that all members of the sharded cluster reflect the
updated
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
, connect to each shard replica
set member and each config server replica set member and check the
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
:
</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">
For a sharded cluster that has access control enabled, to run the
following command against a shard replica set member, you must
connect to the member as a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-users/#std-label-shard-local-users">
shard local user
<!-- -->
.
</a>
</p>
</div>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 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">adminCommand</span>( <!-- -->{ <span class="lg-highlight-attr">getParameter</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">featureCompatibilityVersion</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">
All members should return a result that includes:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\"featureCompatibilityVersion\" : { \"version\" : \"4.4\" }","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">"featureCompatibilityVersion"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"version"</span> <!-- -->: <span class="lg-highlight-string">"4.4"</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 any member returns a
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
of
<code class="leafygreen-ui-1l06pbn">
"5.0"
</code>
,
wait for the member to reflect version
<code class="leafygreen-ui-1l06pbn">
"4.4"
</code>
before proceeding.
</p>
</li>
</ol>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Arbiters do not replicate the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-collections/#mongodb-data-admin.system.version">
<code class="leafygreen-ui-1l06pbn">
admin.system.version
</code>
</a>
collection.
Because of this, arbiters always have a feature compatibility version equal
to the downgrade version of the binary, regardless of the fCV value of the
replica set.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, an arbiter in a MongoDB
<!-- -->
5.0
<!-- -->
cluster, has an fCV
value of
<!-- -->
4.4
<!-- -->
.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on the returned
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
value, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setFeatureCompatibilityVersion/#std-label-view-fcv">
Get FeatureCompatibilityVersion
<!-- -->
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
6. Remove fCV
<!-- -->
5.0
<!-- -->
Persisted Features
<a class="headerlink leafygreen-ui-1kepx47" href="#6.-remove-fcv-newversion-persisted-features" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="6.-remove-fcv-newversion-persisted-features">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following steps are necessary only if fCV has ever been set to
<code class="leafygreen-ui-1l06pbn">
"5.0"
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
Remove all persisted
<!-- -->
5.0
<!-- -->
<a class="leafygreen-ui-tqgtui" href="/docs/manual/release-notes/5.0-compatibility/#std-label-5.0-compatibility-enabled">
features
</a>
that are incompatible with
<!-- -->
4.4
<!-- -->
. These include:
</p>
<dl>
<dt>
Time-series Collections
</dt>
<dd class="css-1ufxuu7">
Remove all
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries-collections/#std-label-manual-timeseries-collection">
time series collections
<!-- -->
.
</a>
</dd>
<dt>
Runtime Audit Filter Management
</dt>
<dd class="css-1ufxuu7">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Disable Runtime Audit Filter Management by setting
<code class="leafygreen-ui-1l06pbn">
auditLog.runtimeConfiguration
</code>
to
<code class="leafygreen-ui-1l06pbn">
false
</code>
in the node's
configuration file.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Update the audit filters for this
<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 in the local configuration file.
</p>
</li>
</ul>
</dd>
</dl>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
7. Remove
<!-- -->
5.0
<!-- -->
Features
<a class="headerlink leafygreen-ui-1kepx47" href="#7.-remove-newversion-features" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="7.-remove-newversion-features">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Remove all persisted features that use
<!-- -->
5.0
<!-- -->
features. These
include but are not limited to:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If any view definitions include 5.0 operators, such as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/dateAdd/#mongodb-expression-exp.-dateAdd">
<code class="leafygreen-ui-1l06pbn">
$dateAdd
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sampleRate/#mongodb-expression-exp.-sampleRate">
<code class="leafygreen-ui-1l06pbn">
$sampleRate
</code>
</a>
, they must be
removed. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/release-notes/5.0/#std-label-5.0-rel-notes-new-agg-operators">
New Aggregation Operators
</a>
for the full list.
</p>
</li>
</ul>
</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>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Downgrade a Sharded Cluster
<a class="headerlink leafygreen-ui-1kepx47" href="#downgrade-a-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="downgrade-a-sharded-cluster">
</div>
</a>
</h3>
<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 proceeding with the downgrade procedure, ensure that all
members, including delayed replica set members in the sharded
cluster, reflect the prerequisite changes. That is, check the
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
and the removal of incompatible
features for each node before downgrading.
</p>
</div>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Download the latest 4.4 binaries.Using either a package manager or a manual download, get the latest\nrelease in the 4.4 series. If using a package manager, add a new\nrepository for the 4.4 binaries, then perform the actual downgrade\nprocess.Before you upgrade or downgrade a replica set, ensure all replica set\nmembers are running. If you do not, the upgrade or downgrade will not\ncomplete until all members are started.If you need to downgrade from 5.0, downgrade to the latest\npatch release of 4.4."},{"@type":"HowToStep","text":"Disable the Balancer.Connect mongosh to a mongos instance in\nthe sharded cluster, and run sh.stopBalancer() to\ndisable the balancer:If a migration is in progress, the system will complete the\nin-progress migration before stopping the balancer. You can run\nsh.isBalancerRunning() to check the balancer's current\nstate.To verify that the balancer is disabled, run\nsh.getBalancerState(), which returns false if the balancer\nis disabled:For more information on disabling the balancer, see\nDisable the Balancer."},{"@type":"HowToStep","text":"Downgrade the mongos instances.Downgrade the binaries and restart."},{"@type":"HowToStep","text":"Downgrade each shard, one at a time.Downgrade the shards one at a time.Downgrade the shard's secondary\nmembers one at a time:Run the following command in mongosh to\nperform a clean shutdown, or refer to\nStop mongod Processes for additional ways to safely\nterminate the mongod process:Replace the 5.0\nbinary with the 4.4 binary and restart.Wait for the member to recover to SECONDARY state before\ndowngrading the next secondary member. To check\nthe member's state, connect mongosh to the shard\nand run rs.status() method.Repeat to downgrade for each secondary member.Downgrade the shard arbiter, if any.Skip this step if the replica set does not include an arbiter.Run the following command in mongosh to\nperform a clean shutdown, or refer to\nStop mongod Processes for additional ways to safely\nterminate the mongod process:Delete the contents of the arbiter data directory. The\nstorage.dbPath configuration setting or\n--dbpath command line option specify the\ndata directory of the arbiter mongod.Replace the 5.0 binary with the 4.4 binary and restart.Wait for the member to recover to ARBITER state. To check\nthe member's state, connect mongosh to the\nmember and run rs.status() method.Downgrade the shard's primary.Step down the replica set primary. Connect\nmongosh to the primary and use\nrs.stepDown() to step down the primary and force an\nelection of a new primary:Run rs.status().When the status shows that the primary has stepped down and\nanother member has assumed PRIMARY state, proceed.Run the following command from mongosh to\nperform a clean shutdown of the stepped-down primary, or refer to\nStop mongod Processes for additional ways to safely\nterminate the mongod process:Replace the 5.0\nbinary with the 4.4 binary and restart.Repeat for the remaining shards."},{"@type":"HowToStep","text":"Downgrade the config servers.Downgrade the secondary\nmembers of the config servers replica set (CSRS) one at a time:Run the following command in mongosh to\nperform a clean shutdown, or refer to\nStop mongod Processes for additional ways to safely\nterminate the mongod process:Replace the 5.0\nbinary with the 4.4 binary and restart.Wait for the member to recover to SECONDARY state before\ndowngrading the next secondary member. To check\nthe member's state, connect mongosh to the shard\nand run rs.status() method.Repeat to downgrade for each secondary member.Step down the config server primary.Connect mongosh to the primary and use\nrs.stepDown() to step down the primary and force an\nelection of a new primary:Run rs.status().When the status shows that the primary has stepped down and\nanother member has assumed PRIMARY state, proceed.Run the following command from mongosh to\nperform a clean shutdown of the stepped-down primary, or refer to\nStop mongod Processes for additional ways to safely\nterminate the mongod process:Replace the 5.0\nbinary with the 4.4 binary and restart."},{"@type":"HowToStep","text":"Re-enable the balancer.Once the downgrade of sharded cluster components is complete, connect\nmongosh to a mongos and\nre-enable the balancer.The mongosh method sh.startBalancer()\nalso enables auto-splitting for the sharded cluster."}],"name":"Downgrade a 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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Download the latest
<!-- -->
4.4
<!-- -->
binaries.
<a class="headerlink leafygreen-ui-1kepx47" href="#download-the-latest-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-the-latest-binaries">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Using either a package manager or a manual download, get the latest
release in the
<!-- -->
4.4
<!-- -->
series. If using a package manager, add a new
repository for the
<!-- -->
4.4
<!-- -->
binaries, then perform the actual downgrade
process.
</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">
Before you upgrade or downgrade a replica set, ensure all replica set
members are running. If you do not, the upgrade or downgrade will not
complete until all members are started.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If you need to downgrade from
<!-- -->
5.0
<!-- -->
, downgrade to the latest
patch release of
<!-- -->
4.4
<!-- -->
.
</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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
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>
</h4>
<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 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 in
the sharded cluster, and run
<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>
to
disable the balancer:
</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>
<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 a migration is in progress, the system will complete the
in-progress migration before stopping the balancer. You can run
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.isBalancerRunning/#mongodb-method-sh.isBalancerRunning">
<code class="leafygreen-ui-1l06pbn">
sh.isBalancerRunning()
</code>
</a>
to check the balancer's current
state.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
To verify that the balancer is disabled, run
<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>
, which returns false if the balancer
is disabled:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.getBalancerState()","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">getBalancerState</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 disabling the balancer, 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>
</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">
Downgrade the
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
instances.
<a class="headerlink leafygreen-ui-1kepx47" href="#downgrade-the-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="downgrade-the-mongos-instances">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Downgrade the binaries and restart.
</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">
Downgrade each shard, one at a time.
<a class="headerlink leafygreen-ui-1kepx47" href="#downgrade-each-shard-one-at-a-time" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="downgrade-each-shard-one-at-a-time">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Downgrade the shards one at a time.
</p>
<ol class="leafygreen-ui-rioki0" type="A">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Downgrade the shard's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-members/#std-label-replica-set-secondary-members">
secondary
</a>
members one at a time:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Run the following command 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
perform a clean shutdown, or refer to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-mongodb-processes/#std-label-terminate-mongod-processes">
Stop
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
Processes
</a>
for additional ways to safely
terminate 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:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { shutdown: 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">shutdown</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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Replace the
<!-- -->
5.0
<!-- -->
binary with the
<!-- -->
4.4
<!-- -->
binary and restart.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Wait for the member to recover to
<code class="leafygreen-ui-1l06pbn">
SECONDARY
</code>
state before
downgrading the next secondary member. To check
the member's state, 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 shard
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>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
Repeat to downgrade for each secondary member.
</p>
</li>
</ol>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Downgrade the shard
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-arbiter/#std-label-replica-set-arbiter-configuration">
arbiter
</a>
, if any.
</p>
<p class="leafygreen-ui-1kp3ins">
Skip this step if the replica set does not include an arbiter.
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Run the following command 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
perform a clean shutdown, or refer to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-mongodb-processes/#std-label-terminate-mongod-processes">
Stop
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
Processes
</a>
for additional ways to safely
terminate 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:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { shutdown: 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">shutdown</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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Delete the contents of the arbiter data directory. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-storage.dbPath">
<code class="leafygreen-ui-1l06pbn">
storage.dbPath
</code>
</a>
configuration setting or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--dbpath">
<code class="leafygreen-ui-1l06pbn">
--dbpath
</code>
</a>
command line option specify the
data directory of the arbiter
<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":"rm -rf /path/to/mongodb/datafiles/*","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-built_in">rm</span> -rf <!-- -->/path/to/mongodb/datafiles<!-- -->/*</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Replace the
<!-- -->
5.0
<!-- -->
binary with the
<!-- -->
4.4
<!-- -->
binary and restart.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Wait for the member to recover to
<code class="leafygreen-ui-1l06pbn">
ARBITER
</code>
state. To check
the member's state, 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
member 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>
method.
</p>
</li>
</ol>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Downgrade the shard's primary.
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Step down the replica set primary. 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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"rs.stepDown()","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>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
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>
<p class="leafygreen-ui-1kp3ins">
When the status shows that the primary has stepped down and
another member has assumed
<code class="leafygreen-ui-1l06pbn">
PRIMARY
</code>
state, proceed.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Run the following command 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>
to
perform a clean shutdown of the stepped-down primary, or refer to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-mongodb-processes/#std-label-terminate-mongod-processes">
Stop
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
Processes
</a>
for additional ways to safely
terminate 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:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { shutdown: 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">shutdown</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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Replace the
<!-- -->
5.0
<!-- -->
binary with the
<!-- -->
4.4
<!-- -->
binary and restart.
</p>
</li>
</ol>
</li>
</ol>
<p class="leafygreen-ui-1kp3ins">
Repeat for the remaining shards.
</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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Downgrade the config servers.
<a class="headerlink leafygreen-ui-1kepx47" href="#downgrade-the-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="downgrade-the-config-servers">
</div>
</a>
</h4>
<ol class="leafygreen-ui-rioki0" type="A">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Downgrade the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-members/#std-label-replica-set-secondary-members">
secondary
</a>
members of the config servers replica set (CSRS) one at a time:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Run the following command 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
perform a clean shutdown, or refer to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-mongodb-processes/#std-label-terminate-mongod-processes">
Stop
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
Processes
</a>
for additional ways to safely
terminate 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:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { shutdown: 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">shutdown</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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Replace the
<!-- -->
5.0
<!-- -->
binary with the
<!-- -->
4.4
<!-- -->
binary and restart.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Wait for the member to recover to
<code class="leafygreen-ui-1l06pbn">
SECONDARY
</code>
state before
downgrading the next secondary member. To check
the member's state, 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 shard
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>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
Repeat to downgrade for each secondary member.
</p>
</li>
</ol>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Step down the config server primary.
</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 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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"rs.stepDown()","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>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
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>
<p class="leafygreen-ui-1kp3ins">
When the status shows that the primary has stepped down and
another member has assumed
<code class="leafygreen-ui-1l06pbn">
PRIMARY
</code>
state, proceed.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Run the following command 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>
to
perform a clean shutdown of the stepped-down primary, or refer to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-mongodb-processes/#std-label-terminate-mongod-processes">
Stop
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
Processes
</a>
for additional ways to safely
terminate 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:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { shutdown: 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">shutdown</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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Replace the
<!-- -->
5.0
<!-- -->
binary with the
<!-- -->
4.4
<!-- -->
binary and restart.
</p>
</li>
</ol>
</li>
</ol>
</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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
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>
</h4>
<p class="leafygreen-ui-1kp3ins">
Once the downgrade of sharded cluster components is complete, 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 a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-sharded-cluster-balancer/#std-label-sharding-balancing-enable">
re-enable the balancer
<!-- -->
.
</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">
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/sh.startBalancer/#mongodb-method-sh.startBalancer">
<code class="leafygreen-ui-1l06pbn">
sh.startBalancer()
</code>
</a>
also enables auto-splitting for the sharded cluster.
</p>
</section>
</div>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/Session.withTransaction/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Session.withTransaction()
<a class="headerlink leafygreen-ui-1kepx47" href="#session.withtransaction--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.withtransaction--">
</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>
</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.withTransaction( <function> [, <options> ] )
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-Session.withTransaction" 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.withTransaction">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
<em>
New in mongosh v1.1.0
</em>
</p>
<p class="leafygreen-ui-1kp3ins">
Runs a specified lambda function within a transaction. If there is an
error, the method retries the:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
commit operation, if there is a failure to commit.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
entire transaction, if the error permits.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-Session.withTransaction" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
Session.withTransaction()
</code>
</span>
</a>
method accepts the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://mongodb.github.io/node-mongodb-native/4.8/interfaces/TransactionOptions.html" rel="noopener noreferrer">
<span>
transaction options
<!-- -->
.
</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>
<table class="css-11h39yh e1nex8bq0">
<colgroup>
<col class="field-name"/>
<col class="field-body"/>
</colgroup>
<tbody>
<tr class="css-x8snli">
<th>
Returns
<!-- -->
:
</th>
<td>
The value produced by the callback function.
</td>
</tr>
</tbody>
</table>
<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/commitTransaction/#mongodb-dbcommand-dbcmd.commitTransaction">
<code class="leafygreen-ui-1l06pbn">
commitTransaction
</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>
<p class="leafygreen-ui-1kp3ins">
The Node.js driver has a version of
<code class="leafygreen-ui-1l06pbn">
Session.withTransaction()
</code>
that is
known as the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/node/current/fundamentals/transactions/#callback-api" target="_self">
<span>
Callback API
</span>
</a>
.
The
<code class="leafygreen-ui-1l06pbn">
Callback API
</code>
also accepts an callback, however the return type
for the Node.js method must be a Promise. The
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
<code class="leafygreen-ui-1l06pbn">
Session.withTransaction()
</code>
method does not require a Promise.
Example
-------
</p>
<p class="leafygreen-ui-1kp3ins">
The following example creates the
<code class="leafygreen-ui-1l06pbn">
balances
</code>
collection and uses a
transaction to transfer money between two customers.
</p>
<p class="leafygreen-ui-1kp3ins">
Create the
<code class="leafygreen-ui-1l06pbn">
balances
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use accounts\ndb.balances.insertMany( [\n { customer: \"Pat\", balance: Decimal128( \"35.88\" ) },\n { customer: \"Sasha\", balance: Decimal128( \"5.23\" ) }\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 accounts</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">balances</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">customer</span>: <span class="lg-highlight-string">"Pat"</span>, <span class="lg-highlight-attr">balance</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>( <span class="lg-highlight-string">"35.88"</span> ) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">customer</span>: <span class="lg-highlight-string">"Sasha"</span>, <span class="lg-highlight-attr">balance</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>( <span class="lg-highlight-string">"5.23"</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">
Initialize some variables that are used in the transaction:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var fromAccount = \"Pat\"\nvar toAccount = \"Sasha\"\nvar transferAmount = 1\n\nvar dbName = \"accounts\"\nvar collectionName = \"balances\"","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> fromAccount <!-- -->= <span class="lg-highlight-string">"Pat"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> toAccount <!-- -->= <span class="lg-highlight-string">"Sasha"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> transferAmount <!-- -->= <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-keyword">var</span> dbName <!-- -->= <span class="lg-highlight-string">"accounts"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">var</span> collectionName <!-- -->= <span class="lg-highlight-string">"balances"</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">
Start a session, then run a transaction to update the accounts:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var session = db.getMongo().startSession( { readPreference: { mode: \"primary\" } } );\nsession.withTransaction( async() => {\n\n const sessionCollection = session.getDatabase(dbName).getCollection(collectionName);\n\n // Check needed values\n var checkFromAccount = sessionCollection.findOne(\n {\n \"customer\": fromAccount,\n \"balance\": { $gte: transferAmount }\n }\n )\n if( checkFromAccount === null ){\n throw new Error( \"Problem with sender account\" )\n }\n\n var checkToAccount = sessionCollection.findOne(\n { \"customer\": toAccount }\n )\n if( checkToAccount === null ){\n throw new Error( \"Problem with receiver account\" )\n }\n\n // Transfer the funds\n sessionCollection.updateOne(\n { \"customer\": toAccount },\n { $inc: { \"balance\": transferAmount } }\n )\n sessionCollection.updateOne(\n { \"customer\": fromAccount },\n { $inc: { \"balance\": -1 * transferAmount } }\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> 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">session.<span class="lg-highlight-title lg-highlight-function">withTransaction</span>( <span class="lg-highlight-title lg-highlight-function">async</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">const</span> sessionCollection <!-- -->= <!-- -->session.<span class="lg-highlight-title lg-highlight-function">getDatabase</span>(<!-- -->dbName).<span class="lg-highlight-title lg-highlight-function">getCollection</span>(<!-- -->collectionName)<!-- -->;</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">// Check needed values</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">var</span> checkFromAccount <!-- -->= <!-- -->sessionCollection.<span class="lg-highlight-title lg-highlight-function">findOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"customer"</span>: <!-- -->fromAccount<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"balance"</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <!-- -->transferAmount }</td></tr><tr 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-keyword">if</span>( <!-- -->checkFromAccount <!-- -->=== <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">Error</span>( <span class="lg-highlight-string">"Problem with sender account"</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">var</span> checkToAccount <!-- -->= <!-- -->sessionCollection.<span class="lg-highlight-title lg-highlight-function">findOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"customer"</span>: <!-- -->toAccount }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> )</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">if</span>( <!-- -->checkToAccount <!-- -->=== <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">Error</span>( <span class="lg-highlight-string">"Problem with receiver account"</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">// Transfer the funds</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> sessionCollection.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"customer"</span>: <!-- -->toAccount }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$inc</span>: <!-- -->{ <span class="lg-highlight-string">"balance"</span>: <!-- -->transferAmount } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> )</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> sessionCollection.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"customer"</span>: <!-- -->fromAccount }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$inc</span>: <!-- -->{ <span class="lg-highlight-string">"balance"</span>: <!-- -->-<span class="lg-highlight-number">1</span> <!-- -->* <!-- -->transferAmount } }</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></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 lambda function includes initial checks to validate the operation
before updating the
<code class="leafygreen-ui-1l06pbn">
balances
</code>
collection.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB automatically completes the transaction.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If both
<code class="leafygreen-ui-1l06pbn">
updateOne()
</code>
operations succeed,
<code class="leafygreen-ui-1l06pbn">
Session.withTransaction()
</code>
commits the transaction when the callback
returns.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If an exception is thrown inside the callback,
<code class="leafygreen-ui-1l06pbn">
Session.withTransaction()
</code>
ends the transaction and rolls back any
uncommitted changes.
</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">
By default, MongoDB ends transactions that run for more than 60
seconds. If you want to extend the default timeout to experiment with
transactions 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>
, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions-production-consideration/#std-label-transaction-limit">
Runtime Limit
<!-- -->
.
</a>
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/log-messages/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Log Messages
<a class="headerlink leafygreen-ui-1kepx47" href="#log-messages" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="log-messages">
</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="#structured-logging" target="_self">
<span>
Structured Logging
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#log-message-field-types" target="_self">
<span>
Log Message Field Types
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#verbosity-levels" target="_self">
<span>
Verbosity Levels
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#log-redaction" target="_self">
<span>
Log Redaction
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#parsing-structured-log-messages" target="_self">
<span>
Parsing Structured Log Messages
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#log-message-examples" target="_self">
<span>
Log Message Examples
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#linux-syslog-limitations" target="_self">
<span>
Linux Syslog Limitations
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#download-your-logs" target="_self">
<span>
Download Your Logs
</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">
As part of normal operation, MongoDB maintains a running log of events,
including entries such as incoming connections, commands run, and issues
encountered. Generally, log messages are useful for diagnosing issues,
monitoring your deployment, and tuning performance.
</p>
<p class="leafygreen-ui-1kp3ins">
To get your log messages, you can use any of the
following methods:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
View logs in your configured
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-destinations" target="_self">
<span>
log destination
<!-- -->
.
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Run the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getLog/#mongodb-dbcommand-dbcmd.getLog">
<code class="leafygreen-ui-1l06pbn">
getLog
</code>
</a>
command.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Download logs through
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/" target="_self">
<span>
MongoDB Atlas
</span>
</a>
.
To learn more, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-messages-atlas" target="_self">
<span>
Download Your Logs
<!-- -->
.
</span>
</a>
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Structured Logging
<a class="headerlink leafygreen-ui-1kepx47" href="#structured-logging" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="structured-logging">
</div>
</a>
</h2>
<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>
/
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances output all log messages
in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-json-output-format" target="_self">
<span>
structured JSON format
</span>
</a>
. Log entries
are written as a series of key-value pairs, where each key indicates a log
message field type, such as "severity", and each corresponding value records
the associated logging information for that field type, such as "informational".
Previously, log entries were output as plaintext.
</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 is an example log message in JSON format as it would
appear in the MongoDB log file:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\"t\":{\"$date\":\"2020-05-01T15:16:17.180+00:00\"},\"s\":\"I\", \"c\":\"NETWORK\", \"id\":12345, \"ctx\":\"listener\", \"svc\": \"R\", \"msg\":\"Listening on\", \"attr\":{\"address\":\"127.0.0.1\"}}","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{<span class="lg-highlight-string">"t"</span>:<!-- -->{<span class="lg-highlight-string">"$date"</span>:<span class="lg-highlight-string">"2020-05-01T15:16:17.180+00:00"</span>}<!-- -->,<span class="lg-highlight-string">"s"</span>:<span class="lg-highlight-string">"I"</span>, <span class="lg-highlight-string">"c"</span>:<span class="lg-highlight-string">"NETWORK"</span>, <span class="lg-highlight-string">"id"</span>:<span class="lg-highlight-number">12345</span>, <span class="lg-highlight-string">"ctx"</span>:<span class="lg-highlight-string">"listener"</span>, <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>, <span class="lg-highlight-string">"msg"</span>:<span class="lg-highlight-string">"Listening on"</span>, <span class="lg-highlight-string">"attr"</span>:<!-- -->{<span class="lg-highlight-string">"address"</span>:<span class="lg-highlight-string">"127.0.0.1"</span>}}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
JSON log entries can be
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-pretty-printing" target="_self">
<span>
pretty-printed
</span>
</a>
for readability. Here is the same log
entry pretty-printed:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": {\n \"$date\": \"2020-05-01T15:16:17.180+00:00\"\n },\n \"s\": \"I\",\n \"c\": \"NETWORK\",\n \"id\": 12345,\n \"ctx\": \"listener\",\n \"svc\": \"R\",\n \"msg\": \"Listening on\",\n \"attr\": {\n \"address\": \"127.0.0.1\"\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">"t"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$date"</span>: <span class="lg-highlight-string">"2020-05-01T15:16:17.180+00:00"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>: <span class="lg-highlight-string">"I"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"NETWORK"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"id"</span>: <span class="lg-highlight-number">12345</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ctx"</span>: <span class="lg-highlight-string">"listener"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"msg"</span>: <span class="lg-highlight-string">"Listening on"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"address"</span>: <span class="lg-highlight-string">"127.0.0.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">
In this log entry, for example, the key
<code class="leafygreen-ui-1l06pbn">
s
</code>
, representing
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-severity-levels" target="_self">
<span>
severity
</span>
</a>
, has a corresponding value of
<code class="leafygreen-ui-1l06pbn">
I
</code>
, representing "Informational", and the key
<code class="leafygreen-ui-1l06pbn">
c
</code>
, representing
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-components" target="_self">
<span>
component
</span>
</a>
, has a corresponding value
of
<code class="leafygreen-ui-1l06pbn">
NETWORK
</code>
, indicating that the "network" component was
responsible for this particular message. The various field types are
presented in detail in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-field-types" target="_self">
<span>
Log Message Field Types
</span>
</a>
section.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Structured logging with key-value pairs allows for efficient parsing
by automated tools or log ingestion services, and makes programmatic
search and analysis of log messages easier to perform. Examples of
analyzing structured log messages can be found in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-parsing" target="_self">
<span>
Parsing Structured Log Messages
</span>
</a>
section.
</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
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
quits if it's unable to write to the log file. To
ensure that
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
can write to the log file, verify that the log
volume has space on the disk and the logs are rotated.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-json-output-format">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
JSON Log Output Format
<a class="headerlink leafygreen-ui-1kepx47" href="#json-log-output-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="json-log-output-format">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
All log output is in JSON format including output sent to:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Log file
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Syslog
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Stdout (standard out)
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-destinations" target="_self">
<span>
log destinations
</span>
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Output from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getLog/#mongodb-dbcommand-dbcmd.getLog">
<code class="leafygreen-ui-1l06pbn">
getLog
</code>
</a>
command is also in JSON format.
</p>
<p class="leafygreen-ui-1kp3ins">
Each log entry is output as a self-contained JSON object which follows
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#std-label-mongodb-extended-json-v2">
Relaxed Extended JSON v2.0
</a>
specification, and has the following layout and field order:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": <datetime>, // timestamp\n \"s\": <string>, // severity\n \"c\": <string>, // component\n \"id\": <integer>, // unique identifier\n \"ctx\": <string>, // context\n \"svc\": <string>, // service\n \"msg\": <string>, // message body\n \"attr\": <object>, // additional attributes (optional)\n \"tags\": <array>, // tags (optional)\n \"truncated\": <object>, // truncation info (if truncated)\n \"size\": <object> // original size of entry (if truncated)\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">"t"</span>: <<span class="lg-highlight-title lg-highlight-class">Datetime</span>><!-- -->,<!-- --> <span class="lg-highlight-comment">// timestamp</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>: <<span class="lg-highlight-title lg-highlight-class">String</span>><!-- -->,<!-- --> <span class="lg-highlight-comment">// severity</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <<span class="lg-highlight-title lg-highlight-class">String</span>><!-- -->,<!-- --> <span class="lg-highlight-comment">// component</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">Integer</span>><!-- -->,<!-- --> <span class="lg-highlight-comment">// unique identifier</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ctx"</span>: <<span class="lg-highlight-title lg-highlight-class">String</span>><!-- -->,<!-- --> <span class="lg-highlight-comment">// context</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"svc"</span>: <<span class="lg-highlight-title lg-highlight-class">String</span>><!-- -->,<!-- --> <span class="lg-highlight-comment">// service</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"msg"</span>: <<span class="lg-highlight-title lg-highlight-class">String</span>><!-- -->,<!-- --> <span class="lg-highlight-comment">// message body</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <<span class="lg-highlight-title lg-highlight-class">Object</span>><!-- -->,<!-- --> <span class="lg-highlight-comment">// additional attributes (optional)</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tags"</span>: <!-- --><Array of strings>, // tags (optional)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "truncated": <Object>, // truncation info (if truncated)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "size": <Object> // original size of entry (if truncated)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Field descriptions:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:10%"/>
<col style="width:10%"/>
<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>
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">
t
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Datetime
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Timestamp of the log message in ISO-8601 format. For an example,
see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-timestamp" target="_self">
<span>
Timestamp
<!-- -->
.
</span>
</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">
s
</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>
Short severity code of the log message. For an example, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-severity-levels" target="_self">
<span>
Severity
<!-- -->
.
</span>
</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">
c
</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>
Full component string for the log message. For an example, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-components" target="_self">
<span>
Components
<!-- -->
.
</span>
</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">
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>
Unique identifier for the log statement. For an example, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-parsing-example-filter-id" target="_self">
<span>
Filtering by Known Log ID
<!-- -->
.
</span>
</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">
ctx
</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>
Name of the thread that caused the log statement.
</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">
svc
</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>
Name of the service in whose context the log statement was made. Will be
<code class="leafygreen-ui-1l06pbn">
S
</code>
for "shard",
<code class="leafygreen-ui-1l06pbn">
R
</code>
"router", or
<code class="leafygreen-ui-1l06pbn">
-
</code>
for "unknown" or "none".
</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">
msg
</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>
Log output message passed from the server or driver. If
necessary, the message is
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-json-escaping" target="_self">
<span>
escaped
</span>
</a>
according to the JSON specification.
</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">
attr
</code>
</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 class="line-block">
<div class="line">
One or more key-value pairs for additional log attributes. If a
</div>
<div class="line">
log message does not include any additional attributes, the
</div>
<div class="line">
<code class="leafygreen-ui-1l06pbn">
attr
</code>
object is omitted.
</div>
</div>
<div class="line-block">
<div class="line">
Attribute values may be referenced by their key name in the
</div>
<div class="line">
<code class="leafygreen-ui-1l06pbn">
msg
</code>
message body, depending on the message. If necessary, the
</div>
<div class="line">
attributes are
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-json-escaping" target="_self">
<span>
escaped
</span>
</a>
</div>
<div class="line">
according to the JSON specification.
</div>
</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">
tags
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Array of strings
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Strings representing any tags applicable to the log statement.
For example,
<code class="leafygreen-ui-1l06pbn">
["startupWarnings"]
</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">
truncated
</code>
</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>
Information about the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-truncation" target="_self">
<span>
log message truncation
</span>
</a>
, if applicable. Only included if the
log entry contains at least one truncated
<code class="leafygreen-ui-1l06pbn">
attr
</code>
attribute.
</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">
size
</code>
</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>
Original size of a log entry if it has been
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-truncation" target="_self">
<span>
truncated
</span>
</a>
. Only included if the log entry
contains at least one truncated
<code class="leafygreen-ui-1l06pbn">
attr
</code>
attribute.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-json-escaping">
</span>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Escaping
<a class="headerlink leafygreen-ui-1kepx47" href="#escaping" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="escaping">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The
<strong>
message
</strong>
and
<strong>
attributes
</strong>
fields will escape control
characters as necessary according to the
Relaxed Extended JSON v2.0 specification:
</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>
Character Represented
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Escape Sequence
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Quotation Mark (
<code class="leafygreen-ui-1l06pbn">
"
</code>
)
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
\"
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Backslash (
<code class="leafygreen-ui-1l06pbn">
\
</code>
)
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
\\
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Backspace (
<code class="leafygreen-ui-1l06pbn">
0x08
</code>
)
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
\b
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Formfeed (
<code class="leafygreen-ui-1l06pbn">
0x0C
</code>
)
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
\f
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Newline (
<code class="leafygreen-ui-1l06pbn">
0x0A
</code>
)
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
\n
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Carriage return (
<code class="leafygreen-ui-1l06pbn">
0x0D
</code>
)
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
\r
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Horizontal tab (
<code class="leafygreen-ui-1l06pbn">
0x09
</code>
)
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
\t
</code>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
Control characters not listed above are escaped with
<code class="leafygreen-ui-1l06pbn">
\uXXXX
</code>
where
"XXXX" is the unicode codepoint in hexadecimal. Bytes with invalid
UTF-8 encoding are replaced with the unicode replacement character
represented by
<code class="leafygreen-ui-1l06pbn">
\ufffd
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
An example of message escaping is provided in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-json-examples" target="_self">
<span>
examples section
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-truncation">
</span>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Truncation
<a class="headerlink leafygreen-ui-1kepx47" href="#truncation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="truncation">
</div>
</a>
</h4>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
7.3
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Any
<strong>
attributes
</strong>
that exceed the maximum size defined with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.maxLogSizeKB">
<code class="leafygreen-ui-1l06pbn">
maxLogSizeKB
</code>
</a>
(default: 10 KB) are truncated. Truncated
attributes omit log data beyond the configured limit, but retain the
JSON formatting of the entry to ensure that the entry remains parsable.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, the following JSON object represents a
<code class="leafygreen-ui-1l06pbn">
command
</code>
attribute
that contains 5000 elements in the
<code class="leafygreen-ui-1l06pbn">
$in
</code>
field without truncation.
</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 example log entries are reformatted for readability.
</p>
</div>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"command\": {\n \"find\": \"mycoll\",\n \"filter\": {\n \"value1\": {\n \"$in\": [0, 1, 2, 3, ... 4999]\n },\n \"value2\": \"foo\"\n },\n \"sort\": { \"value1\": 1 },\n \"lsid\":{\"id\":{\"$uuid\":\"80a99e49-a850-467b-a26d-aeb2d8b9f42b\"}},\n \"$db\": \"testdb\"\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-string">"command"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"find"</span>: <span class="lg-highlight-string">"mycoll"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"filter"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"value1"</span>: <!-- -->{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$in"</span>: <!-- -->[<span class="lg-highlight-number">0</span>, <span class="lg-highlight-number">1</span>, <span class="lg-highlight-number">2</span>, <span class="lg-highlight-number">3</span>, <!-- -->...<!-- --> <span class="lg-highlight-number">4999</span>]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"value2"</span>: <span class="lg-highlight-string">"foo"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"sort"</span>: <!-- -->{ <span class="lg-highlight-string">"value1"</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"lsid"</span>:<!-- -->{<span class="lg-highlight-string">"id"</span>:<!-- -->{<span class="lg-highlight-string">"$uuid"</span>:<span class="lg-highlight-string">"80a99e49-a850-467b-a26d-aeb2d8b9f42b"</span>}}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$db"</span>: <span class="lg-highlight-string">"testdb"</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 this example, the
<code class="leafygreen-ui-1l06pbn">
$in
</code>
array is truncated at the 376th element because the
size of the
<code class="leafygreen-ui-1l06pbn">
command
</code>
attribute would exceed
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.maxLogSizeKB">
<code class="leafygreen-ui-1l06pbn">
maxLogSizeKB
</code>
</a>
if
it included the subsequent elements. The remainder of the
<code class="leafygreen-ui-1l06pbn">
command
</code>
attribute is omitted. The truncated log entry resembles the following output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": { \"$date\": \"2021-03-17T20:30:07.212+01:00\" },\n \"s\": \"I\",\n \"c\": \"COMMAND\",\n \"id\": 51803,\n \"ctx\": \"conn9\",\n \"msg\": \"Slow query\",\n \"attr\": {\n \"command\": {\n \"find\": \"mycoll\",\n \"filter\": {\n \"value1\": {\n \"$in\": [ 0, 1, ..., 376 ] // Values in array omitted for brevity\n }\n }\n },\n ... // Other attr fields omitted for brevity\n },\n \"truncated\": {\n \"command\": {\n \"truncated\": {\n \"filter\": {\n \"truncated\": {\n \"value1\": {\n \"truncated\": {\n \"$in\": {\n \"truncated\": {\n \"377\": {\n \"type\": \"double\",\n \"size\": 8\n }\n },\n \"omitted\": 4623\n }\n }\n }\n },\n \"omitted\": 1\n }\n },\n \"omitted\": 3\n }\n },\n \"size\": {\n \"command\": 21692\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-string">"t"</span>: <!-- -->{ <span class="lg-highlight-string">"$date"</span>: <span class="lg-highlight-string">"2021-03-17T20:30:07.212+01:00"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>: <span class="lg-highlight-string">"I"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"COMMAND"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"id"</span>: <span class="lg-highlight-number">51803</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ctx"</span>: <span class="lg-highlight-string">"conn9"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"msg"</span>: <span class="lg-highlight-string">"Slow query"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"command"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"find"</span>: <span class="lg-highlight-string">"mycoll"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"filter"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"value1"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$in"</span>: <!-- -->[ <span class="lg-highlight-number">0</span>, <span class="lg-highlight-number">1</span>, <!-- -->...<!-- -->, <span class="lg-highlight-number">376</span> ] <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Values in array </span><span class="lg-highlight-comment">omitted for brevity</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-comment">//</span><span class="lg-highlight-comment"> Other attr fields </span><span class="lg-highlight-comment">omitted for brevity</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"truncated"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"command"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"truncated"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"filter"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"truncated"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"value1"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"truncated"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$in"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"truncated"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"377"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"double"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"size"</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><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"omitted"</span>: <span class="lg-highlight-number">4623</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"> <span class="lg-highlight-string">"omitted"</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-string">"omitted"</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"> <span class="lg-highlight-string">"size"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"command"</span>: <span class="lg-highlight-number">21692</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">
Log entries containing one or more truncated attributes include nested
<code class="leafygreen-ui-1l06pbn">
truncated
</code>
objects, which provide the following information for each
truncated attribute in the log entry:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The attribute that was truncated
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The specific sub-object of that attribute that triggered truncation, if
applicable
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The data
<code class="leafygreen-ui-1l06pbn">
type
</code>
of the truncated field
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
size
</code>
, in bytes, of the element that triggers truncation
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The number of elements that were
<code class="leafygreen-ui-1l06pbn">
omitted
</code>
under each sub-object due to
truncation
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Log entries with truncated attributes may also include an additional
<code class="leafygreen-ui-1l06pbn">
size
</code>
field at the end of the entry which indicates the original
size of the attribute before truncation, in this case
<code class="leafygreen-ui-1l06pbn">
21692
</code>
or about
22KB. This final
<code class="leafygreen-ui-1l06pbn">
size
</code>
field is only shown if it is different from
the
<code class="leafygreen-ui-1l06pbn">
size
</code>
field in the
<code class="leafygreen-ui-1l06pbn">
truncated
</code>
object, i.e. if the total object
size of the attribute is different from the size of the truncated
sub-object, as is the case in the example above.
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Padding
<a class="headerlink leafygreen-ui-1kepx47" href="#padding" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="padding">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
When output to the
<em>
file
</em>
or the
<em>
syslog
</em>
log destinations, padding is
added after the
<strong>
severity
</strong>
,
<strong>
context
</strong>
, and
<strong>
id
</strong>
fields to increase
readability when viewed with a fixed-width font.
</p>
<p class="leafygreen-ui-1kp3ins">
The following MongoDB log file excerpt demonstrates this padding:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\"t\":{\"$date\":\"2020-05-18T20:18:12.724+00:00\"},\"s\":\"I\", \"c\":\"CONTROL\", \"id\":23285, \"ctx\":\"main\", \"svc\": \"R\", \"msg\":\"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'\"}\n{\"t\":{\"$date\":\"2020-05-18T20:18:12.734+00:00\"},\"s\":\"W\", \"c\":\"ASIO\", \"id\":22601, \"ctx\":\"main\", \"svc\": \"R\", \"msg\":\"No TransportLayer configured during NetworkInterface startup\"}\n{\"t\":{\"$date\":\"2020-05-18T20:18:12.734+00:00\"},\"s\":\"I\", \"c\":\"NETWORK\", \"id\":4648601, \"ctx\":\"main\", \"svc\": \"R\", \"msg\":\"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize.\"}\n{\"t\":{\"$date\":\"2020-05-18T20:18:12.814+00:00\"},\"s\":\"I\", \"c\":\"STORAGE\", \"id\":4615611, \"ctx\":\"initandlisten\", \"svc\": \"R\", \"msg\":\"MongoDB starting\", \"attr\":{\"pid\":10111,\"port\":27001,\"dbPath\":\"/var/lib/mongo\",\"architecture\":\"64-bit\",\"host\":\"centos8\"}}\n{\"t\":{\"$date\":\"2020-05-18T20:18:12.814+00:00\"},\"s\":\"I\", \"c\":\"CONTROL\", \"id\":23403, \"ctx\":\"initandlisten\", \"svc\": \"R\", \"msg\":\"Build Info\", \"attr\":{\"buildInfo\":{\"version\":\"4.4.0\",\"gitVersion\":\"328c35e4b883540675fb4b626c53a08f74e43cf0\",\"openSSLVersion\":\"OpenSSL 1.1.1c FIPS 28 May 2019\",\"modules\":[],\"allocator\":\"tcmalloc\",\"environment\":{\"distmod\":\"rhel80\",\"distarch\":\"x86_64\",\"target_arch\":\"x86_64\"}}}}\n{\"t\":{\"$date\":\"2020-05-18T20:18:12.814+00:00\"},\"s\":\"I\", \"c\":\"CONTROL\", \"id\":51765, \"ctx\":\"initandlisten\", \"svc\": \"R\", \"msg\":\"Operating System\", \"attr\":{\"os\":{\"name\":\"CentOS Linux release 8.0.1905 (Core) \",\"version\":\"Kernel 4.18.0-80.11.2.el8_0.x86_64\"}}}","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">"t"</span>:<!-- -->{<span class="lg-highlight-string">"$date"</span>:<span class="lg-highlight-string">"2020-05-18T20:18:12.724+00:00"</span>}<!-- -->,<span class="lg-highlight-string">"s"</span>:<span class="lg-highlight-string">"I"</span>, <span class="lg-highlight-string">"c"</span>:<span class="lg-highlight-string">"CONTROL"</span>, <span class="lg-highlight-string">"id"</span>:<span class="lg-highlight-number">23285</span>, <span class="lg-highlight-string">"ctx"</span>:<span class="lg-highlight-string">"main"</span>, <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>, <span class="lg-highlight-string">"msg"</span>:<span class="lg-highlight-string">"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"</span>}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{<span class="lg-highlight-string">"t"</span>:<!-- -->{<span class="lg-highlight-string">"$date"</span>:<span class="lg-highlight-string">"2020-05-18T20:18:12.734+00:00"</span>}<!-- -->,<span class="lg-highlight-string">"s"</span>:<span class="lg-highlight-string">"W"</span>, <span class="lg-highlight-string">"c"</span>:<span class="lg-highlight-string">"ASIO"</span>, <span class="lg-highlight-string">"id"</span>:<span class="lg-highlight-number">22601</span>, <span class="lg-highlight-string">"ctx"</span>:<span class="lg-highlight-string">"main"</span>, <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>, <span class="lg-highlight-string">"msg"</span>:<span class="lg-highlight-string">"No TransportLayer configured during NetworkInterface startup"</span>}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{<span class="lg-highlight-string">"t"</span>:<!-- -->{<span class="lg-highlight-string">"$date"</span>:<span class="lg-highlight-string">"2020-05-18T20:18:12.734+00:00"</span>}<!-- -->,<span class="lg-highlight-string">"s"</span>:<span class="lg-highlight-string">"I"</span>, <span class="lg-highlight-string">"c"</span>:<span class="lg-highlight-string">"NETWORK"</span>, <span class="lg-highlight-string">"id"</span>:<span class="lg-highlight-number">4648601</span>, <span class="lg-highlight-string">"ctx"</span>:<span class="lg-highlight-string">"main"</span>, <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>, <span class="lg-highlight-string">"msg"</span>:<span class="lg-highlight-string">"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."</span>}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{<span class="lg-highlight-string">"t"</span>:<!-- -->{<span class="lg-highlight-string">"$date"</span>:<span class="lg-highlight-string">"2020-05-18T20:18:12.814+00:00"</span>}<!-- -->,<span class="lg-highlight-string">"s"</span>:<span class="lg-highlight-string">"I"</span>, <span class="lg-highlight-string">"c"</span>:<span class="lg-highlight-string">"STORAGE"</span>, <span class="lg-highlight-string">"id"</span>:<span class="lg-highlight-number">4615611</span>, <span class="lg-highlight-string">"ctx"</span>:<span class="lg-highlight-string">"initandlisten"</span>, <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>, <span class="lg-highlight-string">"msg"</span>:<span class="lg-highlight-string">"MongoDB starting"</span>, <span class="lg-highlight-string">"attr"</span>:<!-- -->{<span class="lg-highlight-string">"pid"</span>:<span class="lg-highlight-number">10111</span>,<span class="lg-highlight-string">"port"</span>:<span class="lg-highlight-number">27001</span>,<span class="lg-highlight-string">"dbPath"</span>:<span class="lg-highlight-string">"/var/lib/mongo"</span>,<span class="lg-highlight-string">"architecture"</span>:<span class="lg-highlight-string">"64-bit"</span>,<span class="lg-highlight-string">"host"</span>:<span class="lg-highlight-string">"centos8"</span>}}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{<span class="lg-highlight-string">"t"</span>:<!-- -->{<span class="lg-highlight-string">"$date"</span>:<span class="lg-highlight-string">"2020-05-18T20:18:12.814+00:00"</span>}<!-- -->,<span class="lg-highlight-string">"s"</span>:<span class="lg-highlight-string">"I"</span>, <span class="lg-highlight-string">"c"</span>:<span class="lg-highlight-string">"CONTROL"</span>, <span class="lg-highlight-string">"id"</span>:<span class="lg-highlight-number">23403</span>, <span class="lg-highlight-string">"ctx"</span>:<span class="lg-highlight-string">"initandlisten"</span>, <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>, <span class="lg-highlight-string">"msg"</span>:<span class="lg-highlight-string">"Build Info"</span>, <span class="lg-highlight-string">"attr"</span>:<!-- -->{<span class="lg-highlight-string">"buildInfo"</span>:<!-- -->{<span class="lg-highlight-string">"version"</span>:<span class="lg-highlight-string">"4.4.0"</span>,<span class="lg-highlight-string">"gitVersion"</span>:<span class="lg-highlight-string">"328c35e4b883540675fb4b626c53a08f74e43cf0"</span>,<span class="lg-highlight-string">"openSSLVersion"</span>:<span class="lg-highlight-string">"OpenSSL 1.1.1c FIPS 28 May 2019"</span>,<span class="lg-highlight-string">"modules"</span>:<!-- -->[<!-- -->]<!-- -->,<span class="lg-highlight-string">"allocator"</span>:<span class="lg-highlight-string">"tcmalloc"</span>,<span class="lg-highlight-string">"environment"</span>:<!-- -->{<span class="lg-highlight-string">"distmod"</span>:<span class="lg-highlight-string">"rhel80"</span>,<span class="lg-highlight-string">"distarch"</span>:<span class="lg-highlight-string">"x86_64"</span>,<span class="lg-highlight-string">"target_arch"</span>:<span class="lg-highlight-string">"x86_64"</span>}}}}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{<span class="lg-highlight-string">"t"</span>:<!-- -->{<span class="lg-highlight-string">"$date"</span>:<span class="lg-highlight-string">"2020-05-18T20:18:12.814+00:00"</span>}<!-- -->,<span class="lg-highlight-string">"s"</span>:<span class="lg-highlight-string">"I"</span>, <span class="lg-highlight-string">"c"</span>:<span class="lg-highlight-string">"CONTROL"</span>, <span class="lg-highlight-string">"id"</span>:<span class="lg-highlight-number">51765</span>, <span class="lg-highlight-string">"ctx"</span>:<span class="lg-highlight-string">"initandlisten"</span>, <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>, <span class="lg-highlight-string">"msg"</span>:<span class="lg-highlight-string">"Operating System"</span>, <span class="lg-highlight-string">"attr"</span>:<!-- -->{<span class="lg-highlight-string">"os"</span>:<!-- -->{<span class="lg-highlight-string">"name"</span>:<span class="lg-highlight-string">"CentOS Linux release 8.0.1905 (Core) "</span>,<span class="lg-highlight-string">"version"</span>:<span class="lg-highlight-string">"Kernel 4.18.0-80.11.2.el8_0.x86_64"</span>}}}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-pretty-printing">
</span>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Pretty Printing
<a class="headerlink leafygreen-ui-1kepx47" href="#pretty-printing" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="pretty-printing">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
When working with MongoDB structured logging, the third-party
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://stedolan.github.io/jq/" rel="noopener noreferrer">
<span>
jq command-line utility
</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>
is a useful
tool that allows for easy pretty-printing of log entries, and powerful
key-based matching and filtering.
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
jq
</code>
is an open-source JSON parser, and is available for
Linux, Windows, and macOS.
</p>
<p class="leafygreen-ui-1kp3ins">
You can use
<code class="leafygreen-ui-1l06pbn">
jq
</code>
to pretty-print log entries as follows:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Pretty-print the entire log file:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"cat mongod.log | jq","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">cat mongod.<span class="lg-highlight-property">log</span> <!-- -->| <!-- -->jq</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Pretty-print the most recent log entry:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"cat mongod.log | tail -1 | jq","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">cat mongod.<span class="lg-highlight-property">log</span> <!-- -->| <!-- -->tail <!-- -->-<span class="lg-highlight-number">1</span> <!-- -->| <!-- -->jq</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
More examples of working with MongoDB structured logs are available in
the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-parsing" target="_self">
<span>
Parsing Structured Log Messages
</span>
</a>
section.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-destinations">
</span>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Configuring Log Message Destinations
<a class="headerlink leafygreen-ui-1kepx47" href="#configuring-log-message-destinations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="configuring-log-message-destinations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB log messages can be output to
<em>
file
</em>
,
<em>
syslog
</em>
, or
<em>
stdout
</em>
(standard output).
</p>
<p class="leafygreen-ui-1kp3ins">
To configure the log output destination, use one of the following
settings, either in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#std-label-configuration-options">
configuration file
</a>
or on the command-line:
</p>
<dl>
<dt>
<strong>
Configuration file:
</strong>
</dt>
<dd class="css-1ufxuu7">
<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/configuration-options/#mongodb-setting-systemLog.destination">
<code class="leafygreen-ui-1l06pbn">
systemLog.destination
</code>
</a>
option for
<em>
file
</em>
or
<em>
syslog
</em>
</p>
</li>
</ul>
</dd>
<dt>
<strong>
Command-line:
</strong>
</dt>
<dd class="css-1ufxuu7">
<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/mongod/#std-option-mongod.--logpath">
<code class="leafygreen-ui-1l06pbn">
--logpath
</code>
</a>
option for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
for
<em>
file
</em>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--syslog">
<code class="leafygreen-ui-1l06pbn">
--syslog
</code>
</a>
option for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
for
<em>
syslog
</em>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#std-option-mongos.--logpath">
<code class="leafygreen-ui-1l06pbn">
--logpath
</code>
</a>
option for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
for
<em>
file
</em>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#std-option-mongos.--syslog">
<code class="leafygreen-ui-1l06pbn">
--syslog
</code>
</a>
option for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
for
<em>
syslog
</em>
</p>
</li>
</ul>
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
Not specifying either
<em>
file
</em>
or
<em>
syslog
</em>
sends all logging output to
<em>
stdout
</em>
.
</p>
<p class="leafygreen-ui-1kp3ins">
For the full list of logging settings and options see:
</p>
<dl>
<dt>
<strong>
Configuration file:
</strong>
</dt>
<dd class="css-1ufxuu7">
<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/#std-label-systemlog-options">
systemLog options list
</a>
</p>
</li>
</ul>
</dd>
<dt>
<strong>
Command-line:
</strong>
</dt>
<dd class="css-1ufxuu7">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-label-mongod-log-options-section">
Log options list
</a>
for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#std-label-mongos-log-options-section">
Log options list
</a>
for
<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>
</dd>
</dl>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Error messages sent to
<code class="leafygreen-ui-1l06pbn">
stderr
</code>
(standard error), such as fatal
errors during startup when not using the
<em>
file
</em>
or
<em>
syslog
</em>
log
destinations, or messages having to do with misconfigured logging
settings, are not affected by the log output destination setting, and
are printed to
<code class="leafygreen-ui-1l06pbn">
stderr
</code>
in plaintext format.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-field-types">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Log Message Field Types
<a class="headerlink leafygreen-ui-1kepx47" href="#log-message-field-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="log-message-field-types">
</div>
</a>
</h2>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-timestamp">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Timestamp
<a class="headerlink leafygreen-ui-1kepx47" href="#timestamp" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="timestamp">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The timestamp field type indicates the precise date and time at which
the logged event occurred.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": {\n \"$date\": \"2020-05-01T15:16:17.180+00:00\"\n },\n \"s\": \"I\",\n \"c\": \"NETWORK\",\n \"id\": 12345,\n \"ctx\": \"listener\",\n \"svc\": \"R\",\n \"msg\": \"Listening on\",\n \"attr\": {\n \"address\": \"127.0.0.1\"\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="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"t"</span>: <!-- -->{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$date"</span>: <span class="lg-highlight-string">"2020-05-01T15:16:17.180+00:00"</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-string">"s"</span>: <span class="lg-highlight-string">"I"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"NETWORK"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"id"</span>: <span class="lg-highlight-number">12345</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ctx"</span>: <span class="lg-highlight-string">"listener"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"msg"</span>: <span class="lg-highlight-string">"Listening on"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"address"</span>: <span class="lg-highlight-string">"127.0.0.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">
When logging to
<em>
file
</em>
or to
<em>
syslog
</em>
<a class="footnote-reference header-buffer" href="#footnote-syslog-ts" id="ref-syslog-ts-id1">
[
<!-- -->
1
<!-- -->
]
</a>
, the default
format for the timestamp is
<code class="leafygreen-ui-1l06pbn">
iso8601-local
</code>
. To modify the
timestamp format, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--timeStampFormat">
<code class="leafygreen-ui-1l06pbn">
--timeStampFormat
</code>
</a>
runtime option or the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.timeStampFormat">
<code class="leafygreen-ui-1l06pbn">
systemLog.timeStampFormat
</code>
</a>
setting.
</p>
<p class="leafygreen-ui-1kp3ins">
See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-parsing-example-filter-timestamp" target="_self">
<span>
Filtering by Date Range
</span>
</a>
for log parsing
examples that filter on the timestamp field.
</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
<code class="leafygreen-ui-1l06pbn">
ctime
</code>
timestamp format is no longer supported.
</p>
</div>
</div>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-syslog-ts" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<a href="#ref-syslog-ts-id1">
1
</a>
]
</td>
<td class="leafygreen-ui-17r2xs0">
<!-- -->
If logging to
<em>
syslog
</em>
, the
<code class="leafygreen-ui-1l06pbn">
syslog
</code>
daemon generates timestamps
when it logs a message, not when MongoDB issues the message. This
can lead to misleading timestamps for log entries, especially when
the system is under heavy load.
</td>
</tr>
</tbody>
</table>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-severity-levels">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Severity
<a class="headerlink leafygreen-ui-1kepx47" href="#severity" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="severity">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The severity field type indicates the severity level associated with the
logged event.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": {\n \"$date\": \"2020-05-01T15:16:17.180+00:00\"\n },\n \"s\": \"I\",\n \"c\": \"NETWORK\",\n \"id\": 12345,\n \"ctx\": \"listener\",\n \"svc\": \"R\",\n \"msg\": \"Listening on\",\n \"attr\": {\n \"address\": \"127.0.0.1\"\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">"t"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$date"</span>: <span class="lg-highlight-string">"2020-05-01T15:16:17.180+00:00"</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-string">"s"</span>: <span class="lg-highlight-string">"I"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"NETWORK"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"id"</span>: <span class="lg-highlight-number">12345</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ctx"</span>: <span class="lg-highlight-string">"listener"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"msg"</span>: <span class="lg-highlight-string">"Listening on"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"address"</span>: <span class="lg-highlight-string">"127.0.0.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">
Severity levels range from "Fatal" (most severe) to "Debug" (least
severe):
</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>
Level
</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">
F
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Fatal
</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">
E
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
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">
W
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Warning
</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">
I
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Informational, for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-messages-configure-verbosity" target="_self">
<span>
verbosity level
</span>
</a>
<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">
D1
</code>
-
<code class="leafygreen-ui-1l06pbn">
D5
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Debug, for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-messages-configure-verbosity" target="_self">
<span>
verbosity levels
</span>
</a>
>
<code class="leafygreen-ui-1l06pbn">
0
</code>
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB indicates the specific
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-messages-configure-verbosity" target="_self">
<span>
debug verbosity level
</span>
</a>
.
For example, if verbosity level is 2, MongoDB indicates
<code class="leafygreen-ui-1l06pbn">
D2
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
In previous versions, MongoDB log messages specified
<code class="leafygreen-ui-1l06pbn">
D
</code>
for all debug verbosity levels.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
You can specify the verbosity level of various components to determine
the amount of
<strong>
Informational
</strong>
and
<strong>
Debug
</strong>
messages MongoDB outputs.
Severity categories above these levels are always shown.
<a class="footnote-reference header-buffer" href="#footnote-slow-oplogs" id="ref-slow-oplogs-id2">
[
<!-- -->
2
<!-- -->
]
</a>
To set verbosity levels, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-messages-configure-verbosity" target="_self">
<span>
Configure Log Verbosity Levels
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-components">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Components
<a class="headerlink leafygreen-ui-1kepx47" href="#components" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="components">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The component field type indicates the category a logged event is a
member of, such as
<strong>
NETWORK
</strong>
or
<strong>
COMMAND
</strong>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": {\n \"$date\": \"2020-05-01T15:16:17.180+00:00\"\n },\n \"s\": \"I\",\n \"c\": \"NETWORK\",\n \"id\": 12345,\n \"ctx\": \"listener\",\n \"svc\": \"R\",\n \"msg\": \"Listening on\",\n \"attr\": {\n \"address\": \"127.0.0.1\"\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">"t"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$date"</span>: <span class="lg-highlight-string">"2020-05-01T15:16:17.180+00:00"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>: <span class="lg-highlight-string">"I"</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"NETWORK"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"id"</span>: <span class="lg-highlight-number">12345</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ctx"</span>: <span class="lg-highlight-string">"listener"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"msg"</span>: <span class="lg-highlight-string">"Listening on"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"address"</span>: <span class="lg-highlight-string">"127.0.0.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">
Each component is individually configurable via its own
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-messages-configure-verbosity" target="_self">
<span>
verbosity filter
</span>
</a>
. The
available components are as follows:
</p>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
ACCESS
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-ACCESS" 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-ACCESS">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to access control, such as authentication. To
specify the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-ACCESS" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
ACCESS
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.accessControl.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.accessControl.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
COMMAND
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-COMMAND" 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-COMMAND">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/#std-label-database-commands">
database commands
</a>
,
such as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/count/#mongodb-dbcommand-dbcmd.count">
<code class="leafygreen-ui-1l06pbn">
count
</code>
</a>
. To specify the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-COMMAND" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
COMMAND
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.command.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.command.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
CONTROL
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-CONTROL" 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-CONTROL">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to control activities, such as initialization. To
specify the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-CONTROL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
CONTROL
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.control.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.control.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
ELECTION
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-ELECTION" 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-ELECTION">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related specifically to replica set elections. To specify
the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-ELECTION" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
ELECTION
</code>
</span>
</a>
components, set the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.replication.election.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.replication.election.verbosity
</code>
</a>
parameter.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-REPL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
REPL
</code>
</span>
</a>
is the parent component of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-ELECTION" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
ELECTION
</code>
</span>
</a>
. If
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.replication.election.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.replication.election.verbosity
</code>
</a>
is
unset, MongoDB uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-REPL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
REPL
</code>
</span>
</a>
verbosity level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-ELECTION" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
ELECTION
</code>
</span>
</a>
components.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
FTDC
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-FTDC" 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-FTDC">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to the diagnostic data collection mechanism, such
as server statistics and status messages. To specify the log level
for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-FTDC" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
FTDC
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.ftdc.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.ftdc.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
GEO
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-GEO" 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-GEO">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to the parsing of geospatial shapes, such as
verifying the GeoJSON shapes. To specify the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-GEO" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
GEO
</code>
</span>
</a>
components, set the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.geo.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.geo.verbosity
</code>
</a>
parameter.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
INDEX
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-INDEX" 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-INDEX">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to indexing operations, such as
creating indexes. To specify the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-INDEX" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
INDEX
</code>
</span>
</a>
components, set the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.index.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.index.verbosity
</code>
</a>
parameter.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
INITSYNC
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-INITSYNC" 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-INITSYNC">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to initial sync operation. To specify the log level
for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-INITSYNC" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
INITSYNC
</code>
</span>
</a>
components, set the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.replication.initialSync.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.replication.initialSync.verbosity
</code>
</a>
parameter.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-REPL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
REPL
</code>
</span>
</a>
is the parent component of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-INITSYNC" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
INITSYNC
</code>
</span>
</a>
. If
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.replication.initialSync.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.replication.initialSync.verbosity
</code>
</a>
is
unset, MongoDB uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-REPL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
REPL
</code>
</span>
</a>
verbosity level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-INITSYNC" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
INITSYNC
</code>
</span>
</a>
components.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
JOURNAL
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-JOURNAL" 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-JOURNAL">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related specifically to storage journaling activities. To
specify the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-JOURNAL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
JOURNAL
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.journal.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.journal.verbosity
</code>
</a>
setting.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-STORAGE" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
STORAGE
</code>
</span>
</a>
is the parent component of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-JOURNAL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
JOURNAL
</code>
</span>
</a>
. If
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.journal.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.journal.verbosity
</code>
</a>
is
unset, MongoDB uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-STORAGE" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
STORAGE
</code>
</span>
</a>
verbosity level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-JOURNAL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
JOURNAL
</code>
</span>
</a>
components.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
NETWORK
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-NETWORK" 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-NETWORK">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to network activities, such as accepting
connections. To specify the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-NETWORK" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
NETWORK
</code>
</span>
</a>
components, set the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.network.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.network.verbosity
</code>
</a>
parameter.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
QUERY
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-QUERY" 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-QUERY">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to queries, including query planner activities.
To specify the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-QUERY" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
QUERY
</code>
</span>
</a>
components, set the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.query.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.query.verbosity
</code>
</a>
parameter.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
QUERYSTATS
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-QUERYSTATS" 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-QUERYSTATS">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/queryStats/#mongodb-pipeline-pipe.-queryStats">
<code class="leafygreen-ui-1l06pbn">
$queryStats
</code>
</a>
operations. To specify the log
level for
<code class="leafygreen-ui-1l06pbn">
QUERYSTATS
</code>
components, set the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.queryStats.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.queryStats.verbosity
</code>
</a>
parameter.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
RECOVERY
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-RECOVERY" 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-RECOVERY">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to storage recovery activities. To specify the log
level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-RECOVERY" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
RECOVERY
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.recovery.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.recovery.verbosity
</code>
</a>
setting.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-STORAGE" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
STORAGE
</code>
</span>
</a>
is the parent component of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-RECOVERY" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
RECOVERY
</code>
</span>
</a>
. If
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.recovery.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.recovery.verbosity
</code>
</a>
is
unset, MongoDB uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-STORAGE" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
STORAGE
</code>
</span>
</a>
verbosity level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-RECOVERY" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
RECOVERY
</code>
</span>
</a>
components.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
REPL
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-REPL" 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-REPL">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to replica sets, such as initial sync, heartbeats,
steady state replication, and rollback.
<a class="footnote-reference header-buffer" href="#footnote-slow-oplogs" id="ref-slow-oplogs-id3">
[
<!-- -->
2
<!-- -->
]
</a>
To specify
the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-REPL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
REPL
</code>
</span>
</a>
components, set the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.replication.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.replication.verbosity
</code>
</a>
parameter.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-REPL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
REPL
</code>
</span>
</a>
is the parent component of the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-ELECTION" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
ELECTION
</code>
</span>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-INITSYNC" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
INITSYNC
</code>
</span>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-REPL_HB" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
REPL_HB
</code>
</span>
</a>
, and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-ROLLBACK" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
ROLLBACK
</code>
</span>
</a>
components.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
REPL_HB
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-REPL_HB" 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-REPL_HB">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related specifically to replica set heartbeats. To specify
the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-REPL_HB" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
REPL_HB
</code>
</span>
</a>
components, set the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.replication.heartbeats.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.replication.heartbeats.verbosity
</code>
</a>
parameter.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-REPL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
REPL
</code>
</span>
</a>
is the parent component of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-REPL_HB" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
REPL_HB
</code>
</span>
</a>
. If
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.replication.heartbeats.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.replication.heartbeats.verbosity
</code>
</a>
is
unset, MongoDB uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-REPL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
REPL
</code>
</span>
</a>
verbosity level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-REPL_HB" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
REPL_HB
</code>
</span>
</a>
components.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
ROLLBACK
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-ROLLBACK" 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-ROLLBACK">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-rollbacks/#std-label-replica-set-rollbacks">
rollback
</a>
operations. To specify the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-ROLLBACK" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
ROLLBACK
</code>
</span>
</a>
components,
set the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.replication.rollback.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.replication.rollback.verbosity
</code>
</a>
parameter.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-REPL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
REPL
</code>
</span>
</a>
is the parent component of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-ROLLBACK" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
ROLLBACK
</code>
</span>
</a>
. If
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.replication.rollback.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.replication.rollback.verbosity
</code>
</a>
is
unset, MongoDB uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-REPL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
REPL
</code>
</span>
</a>
verbosity level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-ROLLBACK" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
ROLLBACK
</code>
</span>
</a>
components.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
SHARDING
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-SHARDING" 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-SHARDING">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to sharding activities, such as the startup 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>
. To specify the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-SHARDING" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
SHARDING
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.sharding.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.sharding.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
STORAGE
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-STORAGE" 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-STORAGE">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to storage activities, such as processes involved
in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/fsync/#mongodb-dbcommand-dbcmd.fsync">
<code class="leafygreen-ui-1l06pbn">
fsync
</code>
</a>
command. To specify the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-STORAGE" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
STORAGE
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.verbosity
</code>
</a>
setting.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-STORAGE" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
STORAGE
</code>
</span>
</a>
is the parent component of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-JOURNAL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
JOURNAL
</code>
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-RECOVERY" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
RECOVERY
</code>
.
</span>
</a>
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
TXN
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-TXN" 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-TXN">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
multi-document transactions
</a>
. To specify the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-TXN" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
TXN
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.transaction.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.transaction.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
WRITE
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-WRITE" 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-WRITE">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages related to write operations, such as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/update/#mongodb-dbcommand-dbcmd.update">
<code class="leafygreen-ui-1l06pbn">
update
</code>
</a>
commands. To specify the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-WRITE" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
WRITE
</code>
</span>
</a>
components,
use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.write.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.write.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
WT
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-WT" 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-WT">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.3
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Messages related to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
storage
engine. To specify the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-WT" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
WT
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.wt.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.wt.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
WTBACKUP
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-WTBACKUP" 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-WTBACKUP">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.3
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Messages related to backup operations performed by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
storage engine.
To specify the log level for the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-WTBACKUP" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
WTBACKUP
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.wt.wtBackup.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.wt.wtBackup.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
WTCHKPT
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-WTCHKPT" 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-WTCHKPT">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.3
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Messages related to checkpoint operations performed by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
storage engine.
To specify the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-WTCHKPT" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
WTCHKPT
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.wt.wtCheckpoint.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.wt.wtCheckpoint.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
WTCMPCT
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-WTCMPCT" 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-WTCMPCT">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.3
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Messages related to compaction operations performed by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
storage engine. To specify the log
level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-WTCMPCT" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
WTCMPCT
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.wt.wtCompact.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.wt.wtCompact.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
WTEVICT
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-WTEVICT" 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-WTEVICT">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.3
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Messages related to eviction operations performed by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
storage engine. To specify the log
level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-WTEVICT" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
WTEVICT
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.wt.wtEviction.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.wt.wtEviction.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
WTHS
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-WTHS" 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-WTHS">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.3
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Messages related to the history store of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
storage engine. To specify
the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-WTHS" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
WTHS
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.wt.wtHS.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.wt.wtHS.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
WTRECOV
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-WTRECOV" 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-WTRECOV">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.3
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Messages related to recovery operations performed by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
storage engine. To specify
the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-WTRECOV" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
WTRECOV
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.wt.wtRecovery.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.wt.wtRecovery.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
WTRTS
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-WTRTS" 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-WTRTS">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.3
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Messages related to rollback to stable (RTS) operations performed by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
storage engine. To specify
the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-WTRTS" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
WTRTS
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.wt.wtRTS.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.wt.wtRTS.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
WTSLVG
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-WTSLVG" 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-WTSLVG">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.3
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Messages related to salvage operations performed by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
storage engine. To specify
the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-WTSLVG" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
WTSLVG
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.wt.wtSalvage.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.wt.wtSalvage.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
WTTS
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-WTTS" 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-WTTS">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.3
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Messages related to timestamps used by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
storage engine. To specify
the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-WTTS" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
WTTS
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.wt.wtTimestamp.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.wt.wtTimestamp.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
WTTXN
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-WTTXN" 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-WTTXN">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.3
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Messages related to transactions performed by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
storage engine. To specify
the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-WTTXN" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
WTTXN
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.wt.wtTransaction.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.wt.wtTransaction.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
WTVRFY
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-WTVRFY" 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-WTVRFY">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.3
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Messages related to verification operations performed by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
storage engine. To specify
the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-WTVRFY" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
WTVRFY
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.wt.wtVerify.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.wt.wtVerify.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
WTWRTLOG
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-WTWRTLOG" 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-WTWRTLOG">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.3
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Messages related to log write operations performed by the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
storage engine. To specify
the log level for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-WTWRTLOG" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
WTWRTLOG
</code>
</span>
</a>
components, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.wt.wtWriteLog.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.wt.wtWriteLog.verbosity
</code>
</a>
setting.
</p>
</dd>
</dl>
<dl class="data">
<dt>
<code class="leafygreen-ui-1l06pbn">
-
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data--" 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--">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Messages not associated with a named component. Unnamed components
have the default log level specified in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.verbosity
</code>
</a>
setting. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.verbosity
</code>
</a>
setting is the default setting for
both named and unnamed components.
</p>
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-parsing-example-filter-component" target="_self">
<span>
Filtering by Component
</span>
</a>
for log parsing
examples that filter on the component field.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-messages-client-data">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Client Data
<a class="headerlink leafygreen-ui-1kepx47" href="#client-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="client-data">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
MongoDB Drivers
</span>
</a>
and client applications (including
<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>
) have the ability to send identifying information at the
time of connection to the server. After the connection is established, the
client does not send the identifying information again unless the connection is
dropped and reestablished.
</p>
<p class="leafygreen-ui-1kp3ins">
This identifying information is contained in the
<strong>
attributes
</strong>
field of the log entry. The exact information included varies by client.
</p>
<p class="leafygreen-ui-1kp3ins">
Below is a sample log message containing the client data document as
transmitted 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>
connection. The client
data is contained in the
<code class="leafygreen-ui-1l06pbn">
doc
</code>
object in the
<strong>
attributes
</strong>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\"t\":{\"$date\":\"2020-05-20T16:21:31.561+00:00\"},\"s\":\"I\", \"c\":\"NETWORK\", \"id\":51800, \"ctx\":\"conn202\", \"svc\": \"R\", \"msg\":\"client metadata\", \"attr\":{\"remote\":\"127.0.0.1:37106\",\"client\":\"conn202\",\"doc\":{\"application\":{\"name\":\"MongoDB Shell\"},\"driver\":{\"name\":\"MongoDB Internal Client\",\"version\":\"4.4.0\"},\"os\":{\"type\":\"Linux\",\"name\":\"CentOS Linux release 8.0.1905 (Core) \",\"architecture\":\"x86_64\",\"version\":\"Kernel 4.18.0-80.11.2.el8_0.x86_64\"}}}}","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{<span class="lg-highlight-string">"t"</span>:<!-- -->{<span class="lg-highlight-string">"$date"</span>:<span class="lg-highlight-string">"2020-05-20T16:21:31.561+00:00"</span>}<!-- -->,<span class="lg-highlight-string">"s"</span>:<span class="lg-highlight-string">"I"</span>, <span class="lg-highlight-string">"c"</span>:<span class="lg-highlight-string">"NETWORK"</span>, <span class="lg-highlight-string">"id"</span>:<span class="lg-highlight-number">51800</span>, <span class="lg-highlight-string">"ctx"</span>:<span class="lg-highlight-string">"conn202"</span>, <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>, <span class="lg-highlight-string">"msg"</span>:<span class="lg-highlight-string">"client metadata"</span>, <span class="lg-highlight-string">"attr"</span>:<!-- -->{<span class="lg-highlight-string">"remote"</span>:<span class="lg-highlight-string">"127.0.0.1:37106"</span>,<span class="lg-highlight-string">"client"</span>:<span class="lg-highlight-string">"conn202"</span>,<span class="lg-highlight-string">"doc"</span>:<!-- -->{<span class="lg-highlight-string">"application"</span>:<!-- -->{<span class="lg-highlight-string">"name"</span>:<span class="lg-highlight-string">"MongoDB Shell"</span>}<!-- -->,<span class="lg-highlight-string">"driver"</span>:<!-- -->{<span class="lg-highlight-string">"name"</span>:<span class="lg-highlight-string">"MongoDB Internal Client"</span>,<span class="lg-highlight-string">"version"</span>:<span class="lg-highlight-string">"4.4.0"</span>}<!-- -->,<span class="lg-highlight-string">"os"</span>:<!-- -->{<span class="lg-highlight-string">"type"</span>:<span class="lg-highlight-string">"Linux"</span>,<span class="lg-highlight-string">"name"</span>:<span class="lg-highlight-string">"CentOS Linux release 8.0.1905 (Core) "</span>,<span class="lg-highlight-string">"architecture"</span>:<span class="lg-highlight-string">"x86_64"</span>,<span class="lg-highlight-string">"version"</span>:<span class="lg-highlight-string">"Kernel 4.18.0-80.11.2.el8_0.x86_64"</span>}}}}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
When secondary members of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-members/#std-label-replica-set-members">
replica set
</a>
initiate
a connection to a primary, they send similar data. A sample log message
containing this initiation connection might appear as follows. The
client data is contained in the
<code class="leafygreen-ui-1l06pbn">
doc
</code>
object in the
<strong>
attributes
</strong>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\"t\":{\"$date\":\"2020-05-20T16:33:40.595+00:00\"},\"s\":\"I\", \"c\":\"NETWORK\", \"id\":51800, \"ctx\":\"conn214\", \"svc\": \"R\", \"msg\":\"client metadata\", \"attr\":{\"remote\":\"127.0.0.1:37176\",\"client\":\"conn214\",\"doc\":{\"driver\":{\"name\":\"NetworkInterfaceTL\",\"version\":\"4.4.0\"},\"os\":{\"type\":\"Linux\",\"name\":\"CentOS Linux release 8.0.1905 (Core) \",\"architecture\":\"x86_64\",\"version\":\"Kernel 4.18.0-80.11.2.el8_0.x86_64\"}}}}","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{<span class="lg-highlight-string">"t"</span>:<!-- -->{<span class="lg-highlight-string">"$date"</span>:<span class="lg-highlight-string">"2020-05-20T16:33:40.595+00:00"</span>}<!-- -->,<span class="lg-highlight-string">"s"</span>:<span class="lg-highlight-string">"I"</span>, <span class="lg-highlight-string">"c"</span>:<span class="lg-highlight-string">"NETWORK"</span>, <span class="lg-highlight-string">"id"</span>:<span class="lg-highlight-number">51800</span>, <span class="lg-highlight-string">"ctx"</span>:<span class="lg-highlight-string">"conn214"</span>, <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>, <span class="lg-highlight-string">"msg"</span>:<span class="lg-highlight-string">"client metadata"</span>, <span class="lg-highlight-string">"attr"</span>:<!-- -->{<span class="lg-highlight-string">"remote"</span>:<span class="lg-highlight-string">"127.0.0.1:37176"</span>,<span class="lg-highlight-string">"client"</span>:<span class="lg-highlight-string">"conn214"</span>,<span class="lg-highlight-string">"doc"</span>:<!-- -->{<span class="lg-highlight-string">"driver"</span>:<!-- -->{<span class="lg-highlight-string">"name"</span>:<span class="lg-highlight-string">"NetworkInterfaceTL"</span>,<span class="lg-highlight-string">"version"</span>:<span class="lg-highlight-string">"4.4.0"</span>}<!-- -->,<span class="lg-highlight-string">"os"</span>:<!-- -->{<span class="lg-highlight-string">"type"</span>:<span class="lg-highlight-string">"Linux"</span>,<span class="lg-highlight-string">"name"</span>:<span class="lg-highlight-string">"CentOS Linux release 8.0.1905 (Core) "</span>,<span class="lg-highlight-string">"architecture"</span>:<span class="lg-highlight-string">"x86_64"</span>,<span class="lg-highlight-string">"version"</span>:<span class="lg-highlight-string">"Kernel 4.18.0-80.11.2.el8_0.x86_64"</span>}}}}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
See the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-json-examples" target="_self">
<span>
examples section
</span>
</a>
for a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-pretty-printing" target="_self">
<span>
pretty-printed
</span>
</a>
example showing
client data.
</p>
<p class="leafygreen-ui-1kp3ins">
For a complete description of client information and required fields,
see the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://github.com/mongodb/specifications/blob/master/source/mongodb-handshake/handshake.rst" rel="noopener noreferrer">
<span>
MongoDB Handshake specification
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-verbosity-levels">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Verbosity Levels
<a class="headerlink leafygreen-ui-1kepx47" href="#verbosity-levels" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-levels">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can specify the logging verbosity level to increase or decrease
the amount of log messages MongoDB outputs. Verbosity levels can be
adjusted for all components together, or for specific
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-components" target="_self">
<span>
named components
</span>
</a>
individually.
</p>
<p class="leafygreen-ui-1kp3ins">
Verbosity affects log entries in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-severity-levels" target="_self">
<span>
severity
</span>
</a>
categories
<strong>
Informational
</strong>
and
<strong>
Debug
</strong>
only.
Severity categories above these levels are always shown.
</p>
<p class="leafygreen-ui-1kp3ins">
You might set verbosity levels to a high value to show detailed logging
for debugging or development, or to a low value to minimize writes to
the log on a vetted production deployment.
<a class="footnote-reference header-buffer" href="#footnote-slow-oplogs" id="ref-slow-oplogs-id4">
[
<!-- -->
2
<!-- -->
]
</a>
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
View Current Log Verbosity Level
<a class="headerlink leafygreen-ui-1kepx47" href="#view-current-log-verbosity-level" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="view-current-log-verbosity-level">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To view the current verbosity levels, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getLogComponents/#mongodb-method-db.getLogComponents">
<code class="leafygreen-ui-1l06pbn">
db.getLogComponents()
</code>
</a>
method:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getLogComponents()","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">getLogComponents</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">
Your output might resemble the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"verbosity\" : 0,\n \"accessControl\" : {\n \"verbosity\" : -1\n },\n \"command\" : {\n \"verbosity\" : -1\n },\n ...\n \"storage\" : {\n \"verbosity\" : -1,\n \"recovery\" : {\n \"verbosity\" : -1\n },\n \"journal\" : {\n \"verbosity\" : -1\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">"verbosity"</span> <!-- -->: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"accessControl"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"verbosity"</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">"command"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"verbosity"</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-string">"storage"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"verbosity"</span> <!-- -->: <!-- -->-<span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"recovery"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"verbosity"</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">"journal"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"verbosity"</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>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The initial
<code class="leafygreen-ui-1l06pbn">
verbosity
</code>
entry is the parent verbosity level for all
components, while the individual
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-components" target="_self">
<span>
named components
</span>
</a>
that follow, such as
<code class="leafygreen-ui-1l06pbn">
accessControl
</code>
,
indicate the specific verbosity level for that component, overriding the
global verbosity level for that particular component if set.
</p>
<p class="leafygreen-ui-1kp3ins">
A value of
<code class="leafygreen-ui-1l06pbn">
-1
</code>
, indicates that the component inherits the verbosity
level of their parent, if they have one (as with
<code class="leafygreen-ui-1l06pbn">
recovery
</code>
above,
inheriting from
<code class="leafygreen-ui-1l06pbn">
storage
</code>
), or the global verbosity level if they do
not (as with
<code class="leafygreen-ui-1l06pbn">
command
</code>
). Inheritance relationships for verbosity
levels are indicated in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-components" target="_self">
<span>
components section
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-messages-configure-verbosity">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Configure Log Verbosity Levels
<a class="headerlink leafygreen-ui-1kepx47" href="#configure-log-verbosity-levels" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-log-verbosity-levels">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can configure the verbosity level using: the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.verbosity
</code>
</a>
and
<code class="leafygreen-ui-1l06pbn">
systemLog.component.<name>.verbosity
</code>
settings, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.logComponentVerbosity">
<code class="leafygreen-ui-1l06pbn">
logComponentVerbosity
</code>
</a>
parameter, or the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.setLogLevel/#mongodb-method-db.setLogLevel">
<code class="leafygreen-ui-1l06pbn">
db.setLogLevel()
</code>
</a>
method.
<a class="footnote-reference header-buffer" href="#footnote-slow-oplogs" id="ref-slow-oplogs-id5">
[
<!-- -->
2
<!-- -->
]
</a>
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
<code class="leafygreen-ui-1l06pbn">
systemLog
</code>
Verbosity Settings
<a class="headerlink leafygreen-ui-1kepx47" href="#systemlog-verbosity-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="systemlog-verbosity-settings">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
To configure the default log level for all
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-components" target="_self">
<span>
components
</span>
</a>
, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.verbosity
</code>
</a>
setting. To configure the level of specific components, use the
<code class="leafygreen-ui-1l06pbn">
systemLog.component.<name>.verbosity
</code>
settings.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, the following configuration sets the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.verbosity
</code>
</a>
to
<code class="leafygreen-ui-1l06pbn">
1
</code>
, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.query.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.query.verbosity
</code>
</a>
to
<code class="leafygreen-ui-1l06pbn">
2
</code>
, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.verbosity
</code>
</a>
to
<code class="leafygreen-ui-1l06pbn">
2
</code>
, and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.journal.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.storage.journal.verbosity
</code>
</a>
to
<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":"systemLog:\n verbosity: 1\n component:\n query:\n verbosity: 2\n storage:\n verbosity: 2\n journal:\n verbosity: 1","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">systemLog</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">verbosity</span>: <span class="lg-highlight-number">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">component</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">query</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">verbosity</span>: <span class="lg-highlight-number">2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">storage</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">verbosity</span>: <span class="lg-highlight-number">2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">journal</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">verbosity</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">
You would set these values in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#std-label-configuration-options">
configuration file
</a>
or on the command line for 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>
instance.
</p>
<p class="leafygreen-ui-1kp3ins">
All components not specified explicitly in the configuration have a
verbosity level of
<code class="leafygreen-ui-1l06pbn">
-1
</code>
, indicating that they inherit the verbosity
level of their parent, if they have one, or the global verbosity level
(
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.verbosity
</code>
</a>
) if they do not.
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
<code class="leafygreen-ui-1l06pbn">
logComponentVerbosity
</code>
Parameter
<a class="headerlink leafygreen-ui-1kepx47" href="#logcomponentverbosity-parameter" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="logcomponentverbosity-parameter">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
To set the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.logComponentVerbosity">
<code class="leafygreen-ui-1l06pbn">
logComponentVerbosity
</code>
</a>
parameter, pass a
document with the verbosity settings to change.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, the following sets the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.verbosity">
<code class="leafygreen-ui-1l06pbn">
default verbosity level
</code>
</a>
to
<code class="leafygreen-ui-1l06pbn">
1
</code>
, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.query.verbosity">
<code class="leafygreen-ui-1l06pbn">
query
</code>
</a>
to
<code class="leafygreen-ui-1l06pbn">
2
</code>
, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.verbosity">
<code class="leafygreen-ui-1l06pbn">
storage
</code>
</a>
to
<code class="leafygreen-ui-1l06pbn">
2
</code>
, and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.storage.journal.verbosity">
<code class="leafygreen-ui-1l06pbn">
storage.journal
</code>
</a>
to
<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.adminCommand( {\n setParameter: 1,\n logComponentVerbosity: {\n verbosity: 1,\n query: {\n verbosity: 2\n },\n storage: {\n verbosity: 2,\n journal: {\n verbosity: 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-title lg-highlight-function">adminCommand</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">setParameter</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">logComponentVerbosity</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">verbosity</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">query</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">verbosity</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"> <span class="lg-highlight-attr">storage</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">verbosity</span>: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">journal</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">verbosity</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></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 would set these values 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>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
<code class="leafygreen-ui-1l06pbn">
db.setLogLevel()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#db.setloglevel--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.setloglevel--">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.setLogLevel/#mongodb-method-db.setLogLevel">
<code class="leafygreen-ui-1l06pbn">
db.setLogLevel()
</code>
</a>
method to update a single component
log level. For a component, you can specify verbosity level of
<code class="leafygreen-ui-1l06pbn">
0
</code>
to
<code class="leafygreen-ui-1l06pbn">
5
</code>
, or you can specify
<code class="leafygreen-ui-1l06pbn">
-1
</code>
to inherit the verbosity of the
parent. For example, the following sets the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.component.query.verbosity">
<code class="leafygreen-ui-1l06pbn">
systemLog.component.query.verbosity
</code>
</a>
to its parent verbosity
(i.e. default verbosity):
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.setLogLevel(-1, \"query\")","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">setLogLevel</span>(<!-- -->-<span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"query"</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 would set this value 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>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-slow-oplogs" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<!-- -->
2
<!-- -->
]
</td>
<td class="leafygreen-ui-17r2xs0">
<em>
(
<a href="#ref-slow-oplogs-id2">
1
</a>
,
<a href="#ref-slow-oplogs-id3">
2
</a>
,
<a href="#ref-slow-oplogs-id4">
3
</a>
,
<a href="#ref-slow-oplogs-id5">
4
</a>
,
<a href="#ref-slow-oplogs-id6">
5
</a>
)
</em>
<!-- -->
Secondary members of a replica set now
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-oplog/#std-label-slow-oplog-application">
log oplog entries
</a>
that take longer than the slow operation
threshold to apply. These slow oplog messages:
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
Are logged for the secondaries in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--logpath">
<code class="leafygreen-ui-1l06pbn">
diagnostic log
</code>
.
</a>
</li>
<li class="leafygreen-ui-d7p7b6">
Are logged under the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-data-REPL" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
REPL
</code>
</span>
</a>
component with the text
<code class="leafygreen-ui-1l06pbn">
applied op: <oplog entry> took <num>ms
</code>
.
</li>
<li class="leafygreen-ui-d7p7b6">
Do not depend on the log levels (either at the system or component
level)
</li>
<li class="leafygreen-ui-d7p7b6">
Do not depend on the profiling level.
</li>
<li class="leafygreen-ui-d7p7b6">
Are affected by
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-operationProfiling.slowOpSampleRate">
<code class="leafygreen-ui-1l06pbn">
slowOpSampleRate
</code>
.
</a>
</li>
</ul>
The profiler does not capture slow oplog entries.
</td>
</tr>
</tbody>
</table>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-slow-ops">
</span>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Logging Slow Operations
<a class="headerlink leafygreen-ui-1kepx47" href="#logging-slow-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="logging-slow-operations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Client operations (such as queries) appear in the log if their duration
exceeds the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/profile/#std-label-slowms-threshold-option">
slow operation threshold
</a>
or
when the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-verbosity-levels" target="_self">
<span>
log verbosity level
</span>
</a>
is 1
or higher.
<a class="footnote-reference header-buffer" href="#footnote-slow-oplogs" id="ref-slow-oplogs-id6">
[
<!-- -->
2
<!-- -->
]
</a>
These log entries include the full command
object associated with the operation.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-the-database-profiler/#std-label-database-profiler">
profiler entries
</a>
and the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-slow-ops" target="_self">
<span>
diagnostic log messages (i.e. mongod/mongos logmessages)
</span>
</a>
for read/write operations include:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
to help identify slow queries with the same
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-plan-cache-query-shape">
plan cache query shape
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, the pre-existing
<code class="leafygreen-ui-1l06pbn">
queryHash
</code>
field is renamed
to
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
. If you're using an earlier MongoDB version,
you'll see
<code class="leafygreen-ui-1l06pbn">
queryHash
</code>
instead of
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
planCacheKey
</code>
to provide more insight into the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/">
query plan
cache
</a>
for slow queries.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.0,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-the-database-profiler/#std-label-database-profiling-specify-slowms-threshold">
slow operation
</a>
log messages include a
<code class="leafygreen-ui-1l06pbn">
remote
</code>
field specifying client IP address.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.2, slow operation log messages include a
<code class="leafygreen-ui-1l06pbn">
queryFramework
</code>
field that indicates which query engine executed the
query:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
queryFramework: "classic"
</code>
indicates that the classic engine
executed the query.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
queryFramework: "sbe"
</code>
indicates that the slot-based query execution engine executed the
query.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.1,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-the-database-profiler/#std-label-database-profiling-specify-slowms-threshold">
slow operation
</a>
log messages include
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-messages-cache-refresh-times" target="_self">
<span>
cache refresh time fields
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.3,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-the-database-profiler/#std-label-database-profiling-specify-slowms-threshold">
slow operation
</a>
log messages and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#std-label-profiler">
database
profiler
</a>
entries include a
<code class="leafygreen-ui-1l06pbn">
cpuNanos
</code>
field that specifies the
total CPU time spent by a query operation in nanoseconds. The
<code class="leafygreen-ui-1l06pbn">
cpuNanos
</code>
field
is only available on Linux systems.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 7.0 (and 6.0.13, 5.0.24), the
<code class="leafygreen-ui-1l06pbn">
totalOplogSlotDurationMicros
</code>
in the slow query log message shows the
time between a write operation getting a commit timestamp to commit the
storage engine writes and actually committing.
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
supports
parallel writes. However, it commits write operations with commit
timestamps in any 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">
Consider the following writes with commit timestamps:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
writeA with Timestamp1
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
writeB with Timestamp2
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
writeC with Timestamp3
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Suppose writeB commits first at Timestamp2. Replication is paused
until writeA commits because writeA's oplog entry with Timestamp1 is
required for replication to copy the oplog to secondary replica set
members.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-pretty-printing" target="_self">
<span>
pretty-printed
</span>
</a>
example of a
slow operation log entry, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-json-examples" target="_self">
<span>
Log Message Examples
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, the
<code class="leafygreen-ui-1l06pbn">
queryShapeHash
</code>
field for a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-shapes/#std-label-query-shapes">
query
shape
</a>
is also included in the slow query log when
available.
</p>
<p class="leafygreen-ui-1kp3ins">
Also starting in MongoDB 8.0, the slow query output includes a
<code class="leafygreen-ui-1l06pbn">
queues
</code>
document that contains information about the operation's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/serverStatus/#std-label-server-status-queues">
queues
</a>
. Each queue in the
<code class="leafygreen-ui-1l06pbn">
queues
</code>
field
contains a
<code class="leafygreen-ui-1l06pbn">
totalTimeQueuedMicros
</code>
field that contains the total
cumulative time, in microseconds, that the operation spent in the
corresponding queue.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-messages-remoteOpWaitMillis">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Time Waiting for Shards Logged in
<code class="leafygreen-ui-1l06pbn">
remoteOpWaitMillis
</code>
Field
<a class="headerlink leafygreen-ui-1kepx47" href="#time-waiting-for-shards-logged-in-remoteopwaitmillis-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="time-waiting-for-shards-logged-in-remoteopwaitmillis-field">
</div>
</a>
</h3>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.0, you can use the
<code class="leafygreen-ui-1l06pbn">
remoteOpWaitMillis
</code>
log
field to obtain the wait time (in milliseconds) for results from
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard">
shards
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
remoteOpWaitMillis
</code>
is only logged:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If you configure
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-slow-ops" target="_self">
<span>
slow operations logging
<!-- -->
.
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
On the
<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/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
that merges the results.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
To determine if a merge operation or a shard issue is causing a slow
query, compare the
<code class="leafygreen-ui-1l06pbn">
workingMillis
</code>
and
<code class="leafygreen-ui-1l06pbn">
remoteOpWaitMillis
</code>
time
fields in the log.
<code class="leafygreen-ui-1l06pbn">
workingMillis
</code>
is the total time the query took
to complete. Specifically:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
workingMillis
</code>
is slightly longer than
<code class="leafygreen-ui-1l06pbn">
remoteOpWaitMillis
</code>
,
then most of the time was spent waiting for a shard response. For
example,
<code class="leafygreen-ui-1l06pbn">
workingMillis
</code>
of 17 and
<code class="leafygreen-ui-1l06pbn">
remoteOpWaitMillis
</code>
of 15.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
workingMillis
</code>
is significantly longer than
<code class="leafygreen-ui-1l06pbn">
remoteOpWaitMillis
</code>
, then most of the time was spent performing the
merge. For example,
<code class="leafygreen-ui-1l06pbn">
workingMillis
</code>
of 100 and
<code class="leafygreen-ui-1l06pbn">
remoteOpWaitMillis
</code>
of 15.
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-log-redaction">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Log Redaction
<a class="headerlink leafygreen-ui-1kepx47" href="#log-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="log-redaction">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Queryable Encryption Log Redaction
<a class="headerlink leafygreen-ui-1kepx47" href="#queryable-encryption-log-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="queryable-encryption-log-redaction">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/#std-label-qe-manual-feature-qe">
Queryable Encryption
</a>
, CRUD
operations against encrypted collections are omitted from the slow query
log. For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/reference/limitations/#std-label-qe-redaction">
Queryable Encryption redaction
<!-- -->
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Enterprise Log Redaction
<a class="headerlink leafygreen-ui-1kepx47" href="#enterprise-log-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="enterprise-log-redaction">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<em>
Available in MongoDB Enterprise only
</em>
</p>
<p class="leafygreen-ui-1kp3ins">
A
<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>
running with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.redactClientLogData">
<code class="leafygreen-ui-1l06pbn">
redactClientLogData
</code>
</a>
redacts any message accompanying a given log
event before logging, leaving only metadata, source files, or line numbers
related to the event.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.redactClientLogData">
<code class="leafygreen-ui-1l06pbn">
redactClientLogData
</code>
</a>
prevents
potentially sensitive information from entering the system log at the cost of
diagnostic detail.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, the following operation inserts a document into a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
running without log redaction. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
has the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-messages-configure-verbosity" target="_self">
<span>
log verbosity level
</span>
</a>
set to
<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.clients.insertOne( { \"name\" : \"Joe\", \"PII\" : \"Sensitive Information\" } )","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">clients</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>( <!-- -->{ <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Joe"</span>, <span class="lg-highlight-string">"PII"</span> <!-- -->: <span class="lg-highlight-string">"Sensitive Information"</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 produces the following log event:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": { \"$date\": \"2024-07-19T15:36:55.024-07:00\" },\n \"s\": \"I\",\n \"c\": \"COMMAND\",\n ...\n \"attr\": {\n \"type\": \"command\",\n ...\n \"appName\": \"mongosh 2.2.10\",\n \"command\": {\n \"insert\": \"clients\",\n \"documents\": [\n {\n \"name\": \"Joe\",\n \"PII\": \"Sensitive Information\",\n \"_id\": { \"$oid\": \"669aea8792c7fd822d3e1d8c\" }\n }\n ],\n \"ordered\": true,\n ...\n }\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">"t"</span>: <!-- -->{ <span class="lg-highlight-string">"$date"</span>: <span class="lg-highlight-string">"2024-07-19T15:36:55.024-07:00"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>: <span class="lg-highlight-string">"I"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"COMMAND"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"command"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"appName"</span>: <span class="lg-highlight-string">"mongosh 2.2.10"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"command"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"insert"</span>: <span class="lg-highlight-string">"clients"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"documents"</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-string">"Joe"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"PII"</span>: <span class="lg-highlight-string">"Sensitive Information"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span>: <!-- -->{ <span class="lg-highlight-string">"$oid"</span>: <span class="lg-highlight-string">"669aea8792c7fd822d3e1d8c"</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">"ordered"</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><tr 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">
When
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
runs with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.redactClientLogData">
<code class="leafygreen-ui-1l06pbn">
redactClientLogData
</code>
</a>
and
performs the same insert operation, it produces the following log event:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": { \"$date\": \"2024-07-19T15:36:55.024-07:00\" },\n \"s\": \"I\",\n \"c\": \"COMMAND\",\n ...\n \"attr\": {\n \"type\": \"command\",\n ...\n \"appName\": \"mongosh 2.2.10\",\n \"command\": {\n \"insert\": \"###\",\n \"documents\": [\n {\n \"name\": \"###\",\n \"PII\": \"###\",\n \"_id\": \"###\"\n }\n ],\n \"ordered\": \"###\",\n ...\n }\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">"t"</span>: <!-- -->{ <span class="lg-highlight-string">"$date"</span>: <span class="lg-highlight-string">"2024-07-19T15:36:55.024-07:00"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>: <span class="lg-highlight-string">"I"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"COMMAND"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"command"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"appName"</span>: <span class="lg-highlight-string">"mongosh 2.2.10"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"command"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"insert"</span>: <span class="lg-highlight-string">"###"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"documents"</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-string">"###"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"PII"</span>: <span class="lg-highlight-string">"###"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</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"> <span class="lg-highlight-string">"ordered"</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>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.redactClientLogData">
<code class="leafygreen-ui-1l06pbn">
redactClientLogData
</code>
</a>
in conjunction with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-encryption-at-rest/#std-label-security-encryption-at-rest">
Encryption at Rest
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-transport-encryption/#std-label-transport-encryption">
TLS/SSL (Transport Encryption)
</a>
to assist
compliance with regulatory requirements.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-parsing">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Parsing Structured Log Messages
<a class="headerlink leafygreen-ui-1kepx47" href="#parsing-structured-log-messages" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="parsing-structured-log-messages">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Log parsing is the act of programmatically searching through and
analyzing log files, often in an automated manner. With the introduction
of structured logging, log parsing is made simpler and
more powerful. For example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Log message fields are presented as key-value pairs. Log parsers can
query by specific keys of interest to efficiently filter results.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Log messages always contain the same message structure. Log parsers
can reliably extract information from any log message, without needing
to code for cases where information is missing or formatted
differently.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The following examples demonstrate common log parsing workflows when
working with MongoDB JSON log output.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Log Parsing Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#log-parsing-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="log-parsing-examples">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When working with MongoDB structured logging, the third-party
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://stedolan.github.io/jq/" rel="noopener noreferrer">
<span>
jq command-line utility
</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>
is a useful
tool that allows for easy pretty-printing of log entries, and powerful
key-based matching and filtering.
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
jq
</code>
is an open-source JSON parser, and is available for
Linux, Windows, and macOS.
</p>
<p class="leafygreen-ui-1kp3ins">
These examples use
<code class="leafygreen-ui-1l06pbn">
jq
</code>
to simplify log parsing.
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Counting Unique Messages
<a class="headerlink leafygreen-ui-1kepx47" href="#counting-unique-messages" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="counting-unique-messages">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following example shows the top 10 unique message values in a given
log file, sorted by frequency:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"jq -r \".msg\" /var/log/mongodb/mongod.log | sort | uniq -c | sort -rn | head -10","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">jq -r <span class="lg-highlight-string">".msg"</span> <!-- -->/var/log/mongodb/mongod.log<!-- --> | <span class="lg-highlight-built_in">sort</span> | <span class="lg-highlight-built_in">uniq</span> -c | <span class="lg-highlight-built_in">sort</span> -rn | <span class="lg-highlight-built_in">head</span> -10</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Monitoring Connections
<a class="headerlink leafygreen-ui-1kepx47" href="#monitoring-connections" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="monitoring-connections">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Remote client connections are shown in the log under the "remote" key
in the attribute object. The following counts all unique connections
over the course of the log file and presents them in descending order by
number of occurrences:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"jq -r '.attr.remote' /var/log/mongodb/mongod.log | grep -v 'null' | sort | uniq -c | sort -r","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">jq -r <span class="lg-highlight-string">'.attr.remote'</span> <!-- -->/var/log/mongodb/mongod.log<!-- --> | grep -v <span class="lg-highlight-string">'null'</span> | <span class="lg-highlight-built_in">sort</span> | <span class="lg-highlight-built_in">uniq</span> -c | <span class="lg-highlight-built_in">sort</span> -r</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Note that connections from the same IP address, but connecting over
different ports, are treated as different connections by this command.
You could limit output to consider IP addresses only, with the following
change:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"jq -r '.attr.remote' /var/log/mongodb/mongod.log | grep -v 'null' | awk -F':' '{print $1}' | sort | uniq -c | sort -r","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">jq -r <span class="lg-highlight-string">'.attr.remote'</span> <!-- -->/var/log/mongodb/mongod.log<!-- --> | grep -v <span class="lg-highlight-string">'null'</span> | awk -F<span class="lg-highlight-string">':'</span> <span class="lg-highlight-string">'{print $1}'</span> | <span class="lg-highlight-built_in">sort</span> | <span class="lg-highlight-built_in">uniq</span> -c | <span class="lg-highlight-built_in">sort</span> -r</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Analyzing Driver Connections
<a class="headerlink leafygreen-ui-1kepx47" href="#analyzing-driver-connections" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="analyzing-driver-connections">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following example counts all remote
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
MongoDB driver
</span>
</a>
connections, and
presents each driver type and version in descending order by number
of occurrences:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"jq -cr '.attr.doc.driver' /var/log/mongodb/mongod.log | grep -v null | sort | uniq -c | sort -rn","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">jq -cr <span class="lg-highlight-string">'.attr.doc.driver'</span> <!-- -->/var/log/mongodb/mongod.log<!-- --> | grep -v null | <span class="lg-highlight-built_in">sort</span> | <span class="lg-highlight-built_in">uniq</span> -c | <span class="lg-highlight-built_in">sort</span> -rn</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Analyzing Client Types
<a class="headerlink leafygreen-ui-1kepx47" href="#analyzing-client-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="analyzing-client-types">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following example analyzes the reported
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-messages-client-data" target="_self">
<span>
client data
</span>
</a>
of remote
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
MongoDB driver
</span>
</a>
connections and client applications, including
<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 prints a total for each unique operating system type that
connected, sorted by frequency:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"jq -r '.attr.doc.os.type' /var/log/mongodb/mongod.log | grep -v null | sort | uniq -c | sort -rn","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">jq -r <span class="lg-highlight-string">'.attr.doc.os.type'</span> <!-- -->/var/log/mongodb/mongod.log<!-- --> | grep -v null | <span class="lg-highlight-built_in">sort</span> | <span class="lg-highlight-built_in">uniq</span> -c | <span class="lg-highlight-built_in">sort</span> -rn</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 string "Darwin", as reported in this log field, represents a macOS
client.
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Analyzing Slow Queries
<a class="headerlink leafygreen-ui-1kepx47" href="#analyzing-slow-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="analyzing-slow-queries">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
With
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-slow-ops" target="_self">
<span>
slow operation logging
</span>
</a>
enabled, the
following returns only the slow operations that took above
2000 milliseconds:, for further analysis:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"jq 'select(.attr.workingMillis>=2000)' /var/log/mongodb/mongod.log","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">jq <span class="lg-highlight-string">'select(.attr.workingMillis>=2000)'</span> <!-- -->/var/log/mongodb/mongod.log</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Consult the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://stedolan.github.io/jq/manual/" rel="noopener noreferrer">
<span>
jq documentation
</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 more information on the
<code class="leafygreen-ui-1l06pbn">
jq
</code>
filters shown in this example.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-parsing-example-filter-component">
</span>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Filtering by Component
<a class="headerlink leafygreen-ui-1kepx47" href="#filtering-by-component" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="filtering-by-component">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Log components (the third field in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-json-output-format" target="_self">
<span>
JSON log output format
</span>
</a>
) indicate the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-components" target="_self">
<span>
general category
</span>
</a>
a given log message falls under. Filtering by
component is often a great starting place when parsing log messages for
relevant events.
</p>
<p class="leafygreen-ui-1kp3ins">
The following example prints only the log messages of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-components" target="_self">
<span>
component
</span>
</a>
type
<strong>
REPL
</strong>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"jq 'select(.c==\"REPL\")' /var/log/mongodb/mongod.log","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">jq <span class="lg-highlight-string">'select(.c=="REPL")'</span> <!-- -->/var/log/mongodb/mongod.log</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 prints all log messages
<em>
except
</em>
those of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-components" target="_self">
<span>
component
</span>
</a>
type
<strong>
REPL
</strong>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"jq 'select(.c!=\"REPL\")' /var/log/mongodb/mongod.log","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">jq <span class="lg-highlight-string">'select(.c!="REPL")'</span> <!-- -->/var/log/mongodb/mongod.log</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 print log messages of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-components" target="_self">
<span>
component
</span>
</a>
type
<strong>
REPL
</strong>
<em>
or
</em>
<strong>
STORAGE
</strong>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"jq 'select( .c as $c | [\"REPL\", \"STORAGE\"] | index($c) )' /var/log/mongodb/mongod.log","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">jq <span class="lg-highlight-string">'select( .c as $c | ["REPL", "STORAGE"] | index($c) )'</span> <!-- -->/var/log/mongodb/mongod.log</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Consult the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://stedolan.github.io/jq/manual/" rel="noopener noreferrer">
<span>
jq documentation
</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 more information on the
<code class="leafygreen-ui-1l06pbn">
jq
</code>
filters shown in this example.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-parsing-example-filter-id">
</span>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Filtering by Known Log ID
<a class="headerlink leafygreen-ui-1kepx47" href="#filtering-by-known-log-id" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="filtering-by-known-log-id">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Log IDs (the fifth field in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-json-output-format" target="_self">
<span>
JSON log output format
</span>
</a>
) map to specific log events, and can
be relied upon to remain stable over successive MongoDB releases.
</p>
<p class="leafygreen-ui-1kp3ins">
As an example, you might be interested in the following two log events,
showing a client connection followed by a disconnection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\"t\":{\"$date\":\"2020-06-01T13:06:59.027-0500\"},\"s\":\"I\", \"c\":\"NETWORK\", \"id\":22943, \"ctx\":\"listener\", \"svc\": \"R\", \"msg\":\"connection accepted from {session_remote} #{session_id} ({connectionCount}{word} now open)\", \"attr\":{\"session_remote\":\"127.0.0.1:61298\", \"session_id\":164,\"connectionCount\":11,\"word\":\" connections\"}}\n{\"t\":{\"$date\":\"2020-06-01T13:07:03.490-0500\"},\"s\":\"I\", \"c\":\"NETWORK\", \"id\":22944, \"ctx\":\"conn157\", \"svc\": \"R\", \"msg\":\"end connection {remote} ({connectionCount}{word} now open)\", \"attr\":{\"remote\":\"127.0.0.1:61298\",\"connectionCount\":10,\"word\":\" connections\"}}","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">"t"</span>:<!-- -->{<span class="lg-highlight-string">"$date"</span>:<span class="lg-highlight-string">"2020-06-01T13:06:59.027-0500"</span>}<!-- -->,<span class="lg-highlight-string">"s"</span>:<span class="lg-highlight-string">"I"</span>, <span class="lg-highlight-string">"c"</span>:<span class="lg-highlight-string">"NETWORK"</span>, <span class="lg-highlight-string">"id"</span>:<span class="lg-highlight-number">22943</span>, <span class="lg-highlight-string">"ctx"</span>:<span class="lg-highlight-string">"listener"</span>, <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>, <span class="lg-highlight-string">"msg"</span>:<span class="lg-highlight-string">"connection accepted from {session_remote} #{session_id} ({connectionCount}{word} now open)"</span>, <span class="lg-highlight-string">"attr"</span>:<!-- -->{<span class="lg-highlight-string">"session_remote"</span>:<span class="lg-highlight-string">"127.0.0.1:61298"</span>, <span class="lg-highlight-string">"session_id"</span>:<span class="lg-highlight-number">164</span>,<span class="lg-highlight-string">"connectionCount"</span>:<span class="lg-highlight-number">11</span>,<span class="lg-highlight-string">"word"</span>:<span class="lg-highlight-string">" connections"</span>}}</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{<span class="lg-highlight-string">"t"</span>:<!-- -->{<span class="lg-highlight-string">"$date"</span>:<span class="lg-highlight-string">"2020-06-01T13:07:03.490-0500"</span>}<!-- -->,<span class="lg-highlight-string">"s"</span>:<span class="lg-highlight-string">"I"</span>, <span class="lg-highlight-string">"c"</span>:<span class="lg-highlight-string">"NETWORK"</span>, <span class="lg-highlight-string">"id"</span>:<span class="lg-highlight-number">22944</span>, <span class="lg-highlight-string">"ctx"</span>:<span class="lg-highlight-string">"conn157"</span>, <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>, <span class="lg-highlight-string">"msg"</span>:<span class="lg-highlight-string">"end connection {remote} ({connectionCount}{word} now open)"</span>, <span class="lg-highlight-string">"attr"</span>:<!-- -->{<span class="lg-highlight-string">"remote"</span>:<span class="lg-highlight-string">"127.0.0.1:61298"</span>,<span class="lg-highlight-string">"connectionCount"</span>:<span class="lg-highlight-number">10</span>,<span class="lg-highlight-string">"word"</span>:<span class="lg-highlight-string">" connections"</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 log IDs for these two entries are
<code class="leafygreen-ui-1l06pbn">
22943
</code>
and
<code class="leafygreen-ui-1l06pbn">
22944
</code>
respectively. You could then filter your log output to show only these
log IDs, effectively showing only client connection activity, using the
following
<code class="leafygreen-ui-1l06pbn">
jq
</code>
syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"jq 'select( .id as $id | [22943, 22944] | index($id) )' /var/log/mongodb/mongod.log","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">jq <span class="lg-highlight-string">'select( .id as $id | [22943, 22944] | index($id) )'</span> <!-- -->/var/log/mongodb/mongod.log</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Consult the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://stedolan.github.io/jq/manual/" rel="noopener noreferrer">
<span>
jq documentation
</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 more information on the
<code class="leafygreen-ui-1l06pbn">
jq
</code>
filters shown in this example.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-parsing-example-filter-timestamp">
</span>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Filtering by Date Range
<a class="headerlink leafygreen-ui-1kepx47" href="#filtering-by-date-range" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="filtering-by-date-range">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Log output can be further refined by filtering on the timestamp field,
limiting log entries returned to a specific date range. For example,
the following returns all log entries that occurred on April 15th, 2020:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"jq 'select(.t[\"$date\"] >= \"2020-04-15T00:00:00.000\" and .t[\"$date\"] <= \"2020-04-15T23:59:59.999\")' /var/log/mongodb/mongod.log","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">jq <span class="lg-highlight-string">'select(.t["$date"] >= "2020-04-15T00:00:00.000" and .t["$date"] <= "2020-04-15T23:59:59.999")'</span> <!-- -->/var/log/mongodb/mongod.log</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Note that this syntax includes the full timestamp, including
milliseconds but excluding the timezone offset.
</p>
<p class="leafygreen-ui-1kp3ins">
Filtering by date range can be combined with any of the examples above,
creating weekly reports or yearly summaries for example. The following
syntax expands the "Monitoring Connections" example from earlier to
limit results to the month of May, 2020:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"jq 'select(.t[\"$date\"] >= \"2020-05-01T00:00:00.000\" and .t[\"$date\"] <= \"2020-05-31T23:59:59.999\" and .attr.remote)' /var/log/mongodb/mongod.log","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">jq <span class="lg-highlight-string">'select(.t["$date"] >= "2020-05-01T00:00:00.000" and .t["$date"] <= "2020-05-31T23:59:59.999" and .attr.remote)'</span> <!-- -->/var/log/mongodb/mongod.log</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Consult the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://stedolan.github.io/jq/manual/" rel="noopener noreferrer">
<span>
jq documentation
</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 more information on the
<code class="leafygreen-ui-1l06pbn">
jq
</code>
filters shown in this example.
</p>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Log Ingestion Services
<a class="headerlink leafygreen-ui-1kepx47" href="#log-ingestion-services" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="log-ingestion-services">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Log ingestion services are third-party products that intake and
aggregate log files, usually from a distributed cluster of systems, and
provide ongoing analysis of that data in a central location.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-json-output-format" target="_self">
<span>
JSON log format
</span>
</a>
allows for more
flexibility when working with log ingestion and analysis services. Whereas
plaintext logs generally require some manner of transformation before being
eligible for use with these products, JSON files can often be consumed out of
the box, depending on the service. Further, JSON-formatted logs offer more
control when performing filtering for these services, as the
key-value structure offers the ability to specifically import only the
fields of interest, while omitting the rest.
</p>
<p class="leafygreen-ui-1kp3ins">
Consult the documentation for your chosen third-party log ingestion
service for more information.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-json-examples">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Log Message Examples
<a class="headerlink leafygreen-ui-1kepx47" href="#log-message-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="log-message-examples">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following examples show log messages in JSON output
format.
</p>
<p class="leafygreen-ui-1kp3ins">
These log messages are presented in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-pretty-printing" target="_self">
<span>
pretty-printed format
</span>
</a>
for convenience.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Startup Warning
<a class="headerlink leafygreen-ui-1kepx47" href="#startup-warning" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="startup-warning">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
This example shows a startup warning:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": {\n \"$date\": \"2020-05-20T19:17:06.188+00:00\"\n },\n \"s\": \"W\",\n \"c\": \"CONTROL\",\n \"id\": 22120,\n \"ctx\": \"initandlisten\",\n \"svc\": \"R\",\n \"msg\": \"Access control is not enabled for the database. Read and write access to data and configuration is unrestricted\",\n \"tags\": [\n \"startupWarnings\"\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-string">"t"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$date"</span>: <span class="lg-highlight-string">"2020-05-20T19:17:06.188+00:00"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>: <span class="lg-highlight-string">"W"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"CONTROL"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"id"</span>: <span class="lg-highlight-number">22120</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ctx"</span>: <span class="lg-highlight-string">"initandlisten"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"msg"</span>: <span class="lg-highlight-string">"Access control is not enabled for the database. Read and write access to data and configuration is unrestricted"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tags"</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"startupWarnings"</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">
Client Connection
<a class="headerlink leafygreen-ui-1kepx47" href="#client-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="client-connection">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
This example shows a client connection that includes
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-messages-client-data" target="_self">
<span>
client data
<!-- -->
:
</span>
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": {\n \"$date\": \"2020-05-20T19:18:40.604+00:00\"\n },\n \"s\": \"I\",\n \"c\": \"NETWORK\",\n \"id\": 51800,\n \"ctx\": \"conn281\",\n \"svc\": \"R\",\n \"msg\": \"client metadata\",\n \"attr\": {\n \"remote\": \"192.168.14.15:37666\",\n \"client\": \"conn281\",\n \"doc\": {\n \"application\": {\n \"name\": \"MongoDB Shell\"\n },\n \"driver\": {\n \"name\": \"MongoDB Internal Client\",\n \"version\": \"4.4.0\"\n },\n \"os\": {\n \"type\": \"Linux\",\n \"name\": \"CentOS Linux release 8.0.1905 (Core) \",\n \"architecture\": \"x86_64\",\n \"version\": \"Kernel 4.18.0-80.11.2.el8_0.x86_64\"\n }\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">"t"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$date"</span>: <span class="lg-highlight-string">"2020-05-20T19:18:40.604+00:00"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>: <span class="lg-highlight-string">"I"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"NETWORK"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"id"</span>: <span class="lg-highlight-number">51800</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ctx"</span>: <span class="lg-highlight-string">"conn281"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"msg"</span>: <span class="lg-highlight-string">"client metadata"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"remote"</span>: <span class="lg-highlight-string">"192.168.14.15:37666"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"client"</span>: <span class="lg-highlight-string">"conn281"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"doc"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"application"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span>: <span class="lg-highlight-string">"MongoDB Shell"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"driver"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span>: <span class="lg-highlight-string">"MongoDB Internal Client"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"version"</span>: <span class="lg-highlight-string">"4.4.0"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"os"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"Linux"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span>: <span class="lg-highlight-string">"CentOS Linux release 8.0.1905 (Core) "</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"architecture"</span>: <span class="lg-highlight-string">"x86_64"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"version"</span>: <span class="lg-highlight-string">"Kernel 4.18.0-80.11.2.el8_0.x86_64"</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>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-json-examples-slow-op">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Slow Operation
<a class="headerlink leafygreen-ui-1kepx47" href="#slow-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="slow-operation">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, slow operations are logged based on the time
that MongoDB spends working on that operation, rather than the total
latency for the operation.
</p>
<p class="leafygreen-ui-1kp3ins">
You can use the metrics in the slow operation log to identify where an
operation spends time in its lifecycle, which helps identify possible
performance improvements.
</p>
<p class="leafygreen-ui-1kp3ins">
In the following example log message:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The amount of time spent waiting for resources while executing the
query is shown in these metrics:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
queues.execution.totalTimeQueuedMicros
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
timeAcquiringMicros
</code>
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
workingMillis
</code>
is the amount of time that MongoDB spends working on
the operation.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
durationMillis
</code>
is the operation's total latency.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\":{\n \"$date\":\"2024-06-01T13:24:10.034+00:00\"\n },\n \"s\":\"I\",\n \"c\":\"COMMAND\",\n \"id\":51803,\n \"ctx\":\"conn3\",\n \"msg\":\"Slow query\",\n \"attr\":{\n \"type\":\"command\",\n \"isFromUserConnection\":true,\n \"ns\":\"db.coll\",\n \"collectionType\":\"normal\",\n \"appName\":\"MongoDB Shell\",\n \"command\":{\n \"find\":\"coll\",\n \"filter\":{\n \"b\":-1\n },\n \"sort\":{\n \"splitPoint\":1\n },\n \"readConcern\":{ },\n \"$db\":\"db\"\n },\n \"planSummary\":\"COLLSCAN\",\n \"planningTimeMicros\":87,\n \"keysExamined\":0,\n \"docsExamined\":20889,\n \"hasSortStage\":true,\n \"nBatches\":1,\n \"cursorExhausted\":true,\n \"numYields\":164,\n \"nreturned\":99,\n \"planCacheShapeHash\":\"9C05019A\",\n \"planCacheKey\":\"C41063D6\",\n \"queryFramework\":\"classic\",\n \"reslen\":96,\n \"locks\":{\n \"ReplicationStateTransition\":{\n \"acquireCount\":{\n \"w\":3\n }\n },\n \"Global\":{\n \"acquireCount\":{\n \"r\":327,\n \"w\":1\n }\n },\n \"Database\":{\n \"acquireCount\":{\n \"r\":1\n },\n \"acquireWaitCount\":{\n \"r\":1\n },\n \"timeAcquiringMicros\":{\n \"r\":2814\n }\n },\n \"Collection\":{\n \"acquireCount\":{\n \"w\":1\n }\n }\n },\n \"flowControl\":{\n \"acquireCount\":1,\n \"acquireWaitCount\":1,\n \"timeAcquiringMicros\":8387\n },\n \"readConcern\":{\n \"level\":\"local\",\n \"provenance\":\"implicitDefault\"\n },\n \"storage\":{ },\n \"cpuNanos\":20987385,\n \"remote\":\"127.0.0.1:47150\",\n \"protocol\":\"op_msg\",\n \"queues\":{\n \"ingress\":{\n \"admissions\":7,\n \"totalTimeQueuedMicros\":0\n },\n \"execution\":{\n \"admissions\":328,\n \"totalTimeQueuedMicros\":2109\n }\n },\n \"workingMillis\":89,\n \"durationMillis\":101\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">"t"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$date"</span>:<span class="lg-highlight-string">"2024-06-01T13:24:10.034+00:00"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>:<span class="lg-highlight-string">"I"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>:<span class="lg-highlight-string">"COMMAND"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"id"</span>:<span class="lg-highlight-number">51803</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ctx"</span>:<span class="lg-highlight-string">"conn3"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"msg"</span>:<span class="lg-highlight-string">"Slow query"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"type"</span>:<span class="lg-highlight-string">"command"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"isFromUserConnection"</span>:<span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ns"</span>:<span class="lg-highlight-string">"db.coll"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"collectionType"</span>:<span class="lg-highlight-string">"normal"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"appName"</span>:<span class="lg-highlight-string">"MongoDB Shell"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"command"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"find"</span>:<span class="lg-highlight-string">"coll"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"filter"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"b"</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">"sort"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"splitPoint"</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">"readConcern"</span>:<!-- -->{ <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$db"</span>:<span class="lg-highlight-string">"db"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"planSummary"</span>:<span class="lg-highlight-string">"COLLSCAN"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"planningTimeMicros"</span>:<span class="lg-highlight-number">87</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"keysExamined"</span>:<span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"docsExamined"</span>:<span class="lg-highlight-number">20889</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hasSortStage"</span>:<span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nBatches"</span>:<span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"cursorExhausted"</span>:<span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numYields"</span>:<span class="lg-highlight-number">164</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nreturned"</span>:<span class="lg-highlight-number">99</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"planCacheShapeHash"</span>:<span class="lg-highlight-string">"9C05019A"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"planCacheKey"</span>:<span class="lg-highlight-string">"C41063D6"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"queryFramework"</span>:<span class="lg-highlight-string">"classic"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"reslen"</span>:<span class="lg-highlight-number">96</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"locks"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ReplicationStateTransition"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"acquireCount"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"w"</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"> <span class="lg-highlight-string">"Global"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"acquireCount"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"r"</span>:<span class="lg-highlight-number">327</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"w"</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-string">"Database"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"acquireCount"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"r"</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">"acquireWaitCount"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"r"</span>:<span class="lg-highlight-number">1</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-string">"timeAcquiringMicros"</span>:<!-- -->{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"r"</span>:<span class="lg-highlight-number">2814</span></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"> <span class="lg-highlight-string">"Collection"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"acquireCount"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"w"</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"> <span class="lg-highlight-string">"flowControl"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"acquireCount"</span>:<span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"acquireWaitCount"</span>:<span class="lg-highlight-number">1</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"timeAcquiringMicros"</span>:<span class="lg-highlight-number">8387</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>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"level"</span>:<span class="lg-highlight-string">"local"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"provenance"</span>:<span class="lg-highlight-string">"implicitDefault"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"storage"</span>:<!-- -->{ <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"cpuNanos"</span>:<span class="lg-highlight-number">20987385</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"remote"</span>:<span class="lg-highlight-string">"127.0.0.1:47150"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"protocol"</span>:<span class="lg-highlight-string">"op_msg"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"queues"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ingress"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"admissions"</span>:<span class="lg-highlight-number">7</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"totalTimeQueuedMicros"</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-string">"execution"</span>:<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"admissions"</span>:<span class="lg-highlight-number">328</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"totalTimeQueuedMicros"</span>:<span class="lg-highlight-number">2109</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }</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-string">"workingMillis"</span>:<span class="lg-highlight-number">89</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"durationMillis"</span>:<span class="lg-highlight-number">101</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">
Starting in MongoDB 8.0, the pre-existing
<code class="leafygreen-ui-1l06pbn">
queryHash
</code>
field is renamed
to
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
. If you're using an earlier MongoDB version,
you'll see
<code class="leafygreen-ui-1l06pbn">
queryHash
</code>
instead of
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Escaping
<a class="headerlink leafygreen-ui-1kepx47" href="#escaping-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="escaping-1">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
This example demonstrates
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-json-escaping" target="_self">
<span>
character escaping
</span>
</a>
, as shown in the
<code class="leafygreen-ui-1l06pbn">
setName
</code>
field of the
attribute object:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": {\n \"$date\": \"2020-05-20T19:11:09.268+00:00\"\n },\n \"s\": \"I\",\n \"c\": \"REPL\",\n \"id\": 21752,\n \"ctx\": \"ReplCoord-0\",\n \"svc\": \"R\",\n \"msg\": \"Scheduling remote command request\",\n \"attr\": {\n \"context\": \"vote request\",\n \"request\": \"RemoteCommand 229 -- target:localhost:27003 db:admin cmd:{ replSetRequestVotes: 1, setName: \\\"my-replica-name\\\", dryRun: true, term: 3, candidateIndex: 0, configVersion: 2, configTerm: 3, lastAppliedOpTime: { ts: Timestamp(1589915409, 1), t: 3 } }\"\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-string">"t"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$date"</span>: <span class="lg-highlight-string">"2020-05-20T19:11:09.268+00:00"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>: <span class="lg-highlight-string">"I"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"REPL"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"id"</span>: <span class="lg-highlight-number">21752</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ctx"</span>: <span class="lg-highlight-string">"ReplCoord-0"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"msg"</span>: <span class="lg-highlight-string">"Scheduling remote command request"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"context"</span>: <span class="lg-highlight-string">"vote request"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"request"</span>: <span class="lg-highlight-string">"RemoteCommand 229 -- target:localhost:27003 db:admin cmd:{ replSetRequestVotes: 1, setName: </span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">my-replica-name</span><span class="lg-highlight-string">\"</span><span class="lg-highlight-string">, dryRun: true, term: 3, candidateIndex: 0, configVersion: 2, configTerm: 3, lastAppliedOpTime: { ts: Timestamp(1589915409, 1), t: 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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-message-view-example">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
View
<a class="headerlink leafygreen-ui-1kepx47" href="#view" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="view">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.0,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-slow-ops" target="_self">
<span>
log messages for slow queries
</span>
</a>
on
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/views/#std-label-views-landing-page">
views
</a>
include a
<code class="leafygreen-ui-1l06pbn">
resolvedViews
</code>
field that contains the view details:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\"resolvedViews\": [ {\n \"viewNamespace\": <string>, // namespace and view name\n \"dependencyChain\": <array>, // view name and collection\n \"resolvedPipeline\": <array> // aggregation pipeline for view\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"><span class="lg-highlight-string">"resolvedViews"</span>: <!-- -->[ <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"viewNamespace"</span>: <<span class="lg-highlight-title lg-highlight-class">String</span>><!-- -->,<!-- --> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> namespace and view </span><span class="lg-highlight-comment">name</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"dependencyChain"</span>: <!-- --><Array of strings>, // view name and collection</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "resolvedPipeline": <Array of documents> // aggregation pipeline for view</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 example uses the
<code class="leafygreen-ui-1l06pbn">
test
</code>
database and creates a view
named
<code class="leafygreen-ui-1l06pbn">
myView
</code>
that sorts the documents in
<code class="leafygreen-ui-1l06pbn">
myCollection
</code>
by the
<code class="leafygreen-ui-1l06pbn">
firstName
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use test\ndb.createView( \"myView\", \"myCollection\", [ { $sort: { \"firstName\" : 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">use test</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">createView</span>( <span class="lg-highlight-string">"myView"</span>, <span class="lg-highlight-string">"myCollection"</span>, <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$sort</span>: <!-- -->{ <span class="lg-highlight-string">"firstName"</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">
Assume a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-slow-ops" target="_self">
<span>
slow query
</span>
</a>
is run on
<code class="leafygreen-ui-1l06pbn">
myView
</code>
.
The following example log message contains a
<code class="leafygreen-ui-1l06pbn">
resolvedViews
</code>
field for
<code class="leafygreen-ui-1l06pbn">
myView
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": {\n \"$date\": \"2021-09-30T17:53:54.646+00:00\"\n },\n \"s\": \"I\",\n \"c\": \"COMMAND\",\n \"id\": 51803,\n \"ctx\": \"conn249\",\n \"svc\": \"R\",\n \"msg\": \"Slow query\",\n \"attr\": {\n \"type\": \"command\",\n \"ns\": \"test.myView\",\n \"appName\": \"MongoDB Shell\",\n \"command\": {\n \"find\": \"myView\",\n \"filter\": {},\n \"lsid\": {\n \"id\": { \"$uuid\": \"ad176471-60e5-4e82-b977-156a9970d30f\" }\n },\n \"$db\": \"test\"\n },\n \"planSummary\":\"COLLSCAN\",\n \"resolvedViews\": [ {\n \"viewNamespace\": \"test.myView\",\n \"dependencyChain\": [ \"myView\", \"myCollection\" ],\n \"resolvedPipeline\": [ { \"$sort\": { \"firstName\": 1 } } ]\n } ],\n \"keysExamined\": 0,\n \"docsExamined\": 1,\n \"hasSortStage\": true,\n \"cursorExhausted\": true,\n \"numYields\": 0,\n \"nreturned\": 1,\n \"planCacheShapeHash\": \"3344645B\",\n \"planCacheKey\": \"1D3DE690\",\n \"queryFramework\": \"classic\"\n \"reslen\": 134,\n \"locks\": { \"ParallelBatchWriterMode\": { \"acquireCount\": { \"r\": 1 } },\n \"ReplicationStateTransition\": { \"acquireCount\": { \"w\": 1 } },\n \"Global\": { \"acquireCount\": { \"r\": 4 } },\n \"Database\": { \"acquireCount\": {\"r\": 1 } },\n \"Collection\": { \"acquireCount\": { \"r\": 1 } },\n \"Mutex\": { \"acquireCount\": { \"r\": 4 } } },\n \"storage\": {},\n \"remote\": \"127.0.0.1:34868\",\n \"protocol\": \"op_msg\",\n \"workingMillis\": 0,\n \"durationMillis\": 0\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-string">"t"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$date"</span>: <span class="lg-highlight-string">"2021-09-30T17:53:54.646+00:00"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>: <span class="lg-highlight-string">"I"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"COMMAND"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"id"</span>: <span class="lg-highlight-number">51803</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ctx"</span>: <span class="lg-highlight-string">"conn249"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"msg"</span>: <span class="lg-highlight-string">"Slow query"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"command"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ns"</span>: <span class="lg-highlight-string">"test.myView"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"appName"</span>: <span class="lg-highlight-string">"MongoDB Shell"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"command"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"find"</span>: <span class="lg-highlight-string">"myView"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"filter"</span>: <!-- -->{<!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"lsid"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"id"</span>: <!-- -->{ <span class="lg-highlight-string">"$uuid"</span>: <span class="lg-highlight-string">"ad176471-60e5-4e82-b977-156a9970d30f"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$db"</span>: <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">"planSummary"</span>:<span class="lg-highlight-string">"COLLSCAN"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"resolvedViews"</span>: <!-- -->[ <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"viewNamespace"</span>: <span class="lg-highlight-string">"test.myView"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"dependencyChain"</span>: <!-- -->[ <span class="lg-highlight-string">"myView"</span>, <span class="lg-highlight-string">"myCollection"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"resolvedPipeline"</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"$sort"</span>: <!-- -->{ <span class="lg-highlight-string">"firstName"</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">"keysExamined"</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"docsExamined"</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hasSortStage"</span>: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"cursorExhausted"</span>: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numYields"</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nreturned"</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"planCacheShapeHash"</span>: <span class="lg-highlight-string">"3344645B"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"planCacheKey"</span>: <span class="lg-highlight-string">"1D3DE690"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"queryFramework"</span>: <span class="lg-highlight-string">"classic"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"reslen"</span>: <span class="lg-highlight-number">134</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"locks"</span>: <!-- -->{ <span class="lg-highlight-string">"ParallelBatchWriterMode"</span>: <!-- -->{ <span class="lg-highlight-string">"acquireCount"</span>: <!-- -->{ <span class="lg-highlight-string">"r"</span>: <span class="lg-highlight-number">1</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ReplicationStateTransition"</span>: <!-- -->{ <span class="lg-highlight-string">"acquireCount"</span>: <!-- -->{ <span class="lg-highlight-string">"w"</span>: <span class="lg-highlight-number">1</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Global"</span>: <!-- -->{ <span class="lg-highlight-string">"acquireCount"</span>: <!-- -->{ <span class="lg-highlight-string">"r"</span>: <span class="lg-highlight-number">4</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Database"</span>: <!-- -->{ <span class="lg-highlight-string">"acquireCount"</span>: <!-- -->{<span class="lg-highlight-string">"r"</span>: <span class="lg-highlight-number">1</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Collection"</span>: <!-- -->{ <span class="lg-highlight-string">"acquireCount"</span>: <!-- -->{ <span class="lg-highlight-string">"r"</span>: <span class="lg-highlight-number">1</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Mutex"</span>: <!-- -->{ <span class="lg-highlight-string">"acquireCount"</span>: <!-- -->{ <span class="lg-highlight-string">"r"</span>: <span class="lg-highlight-number">4</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"storage"</span>: <!-- -->{<!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"remote"</span>: <span class="lg-highlight-string">"127.0.0.1:34868"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"protocol"</span>: <span class="lg-highlight-string">"op_msg"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"workingMillis"</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"durationMillis"</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></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, the pre-existing
<code class="leafygreen-ui-1l06pbn">
queryHash
</code>
field is renamed
to
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
. If you're using an earlier MongoDB version,
you'll see
<code class="leafygreen-ui-1l06pbn">
queryHash
</code>
instead of
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Authorization
<a class="headerlink leafygreen-ui-1kepx47" href="#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="authorization">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.0,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-slow-ops" target="_self">
<span>
log messages for slow queries
</span>
</a>
include a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#mongodb-data-system.profile.authorization">
<code class="leafygreen-ui-1l06pbn">
system.profile.authorization
</code>
</a>
section. These metrics help
determine if a request is delayed because of contention for the user
authorization cache.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\"authorization\": {\n \"startedUserCacheAcquisitionAttempts\": 1,\n \"completedUserCacheAcquisitionAttempts\": 1,\n \"userCacheWaitTimeMicros\": 508\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"><span class="lg-highlight-string">"authorization"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"startedUserCacheAcquisitionAttempts"</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"completedUserCacheAcquisitionAttempts"</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"userCacheWaitTimeMicros"</span>: <span class="lg-highlight-number">508</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-log-messages-session-workflow-example">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Session Workflow Log Message
<a class="headerlink leafygreen-ui-1kepx47" href="#session-workflow-log-message" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-workflow-log-message">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.3, a message is added to the log if the time to
send an operation response exceeds the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/profile/#std-label-slowms-threshold-option">
slowms threshold option
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The message is known as a session workflow log message and contains
various times to perform an operation in a database session.
</p>
<p class="leafygreen-ui-1kp3ins">
Example session workflow log message:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": {\n \"$date\": \"2022-12-14T17:22:44.233+00:00\"\n },\n \"s\": \"I\",\n \"c\": \"EXECUTOR\",\n \"id\": 6983000,\n \"ctx\": \"conn1\",\n \"svc\": \"R\",\n \"msg\": \"Slow network response send time\",\n \"attr\": {\n \"elapsed\": {\n \"totalMillis\": 109,\n \"activeMillis\": 30,\n \"receiveWorkMillis\": 2,\n \"processWorkMillis\": 10,\n \"sendResponseMillis\": 22,\n \"yieldMillis\": 15,\n \"finalizeMillis\": 30\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">"t"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$date"</span>: <span class="lg-highlight-string">"2022-12-14T17:22:44.233+00:00"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>: <span class="lg-highlight-string">"I"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"EXECUTOR"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"id"</span>: <span class="lg-highlight-number">6983000</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ctx"</span>: <span class="lg-highlight-string">"conn1"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"msg"</span>: <span class="lg-highlight-string">"Slow network response send time"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"elapsed"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"totalMillis"</span>: <span class="lg-highlight-number">109</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"activeMillis"</span>: <span class="lg-highlight-number">30</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"receiveWorkMillis"</span>: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"processWorkMillis"</span>: <span class="lg-highlight-number">10</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"sendResponseMillis"</span>: <span class="lg-highlight-number">22</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"yieldMillis"</span>: <span class="lg-highlight-number">15</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"finalizeMillis"</span>: <span class="lg-highlight-number">30</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 times are in milliseconds.
</p>
<p class="leafygreen-ui-1kp3ins">
A session workflow message is added to the log if
<code class="leafygreen-ui-1l06pbn">
sendResponseMillis
</code>
exceeds the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/profile/#std-label-slowms-threshold-option">
slowms threshold option
<!-- -->
.
</a>
</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="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
totalMillis
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Total time to perform the operation in the session, which
includes the time spent waiting for a message to be received.
</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">
activeMillis
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Time between receiving a message and completing the operation
associated with that message. Time includes sending a response
and performing any clean up.
</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">
receivedWorkMillis
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Time to receive the operation information over the network.
</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">
processWorkMillis
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Time to process the operation and create the response.
</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">
sendResponseMillis
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Time to send the response.
</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">
yieldMillis
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Time between releasing the worker thread and the thread being
used again.
</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">
finalize
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Time to end and close the session workflow.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-messages-connection-acquisition-to-wire">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Connection Acquisition To Wire Log Message
<a class="headerlink leafygreen-ui-1kepx47" href="#connection-acquisition-to-wire-log-message" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-acquisition-to-wire-log-message">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.3, a message is added to the log if the time that
an operation waited between acquisition of a server connection and
writing the bytes to send to the server over the network exceeds 1
millisecond.
</p>
<p class="leafygreen-ui-1kp3ins">
By default, the message is logged at the
<code class="leafygreen-ui-1l06pbn">
"I"
</code>
information level, and
at most once every second to avoid too many log messages. If you must
obtain every log message, change your log level to debug.
</p>
<p class="leafygreen-ui-1kp3ins">
If the operation wait time exceeds 1 millisecond and the message is
logged at the information level within the last second, then the next
message is logged at the debug level. Otherwise, the next message is
logged at the information level.
</p>
<p class="leafygreen-ui-1kp3ins">
Example log message:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": {\n \"$date\":\"2023-01-31T15:22:29.473+00:00\"\n },\n \"s\": \"I\",\n \"c\": \"NETWORK\",\n \"id\": 6496702,\n \"ctx\": \"ReplicaSetMonitor-TaskExecutor\",\n \"svc\": \"R\",\n \"msg\": \"Acquired connection for remote operation and completed writing to wire\",\n \"attr\": {\n \"durationMicros\": 1683\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-string">"t"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$date"</span>:<span class="lg-highlight-string">"2023-01-31T15:22:29.473+00:00"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>: <span class="lg-highlight-string">"I"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"NETWORK"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"id"</span>: <span class="lg-highlight-number">6496702</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ctx"</span>: <span class="lg-highlight-string">"ReplicaSetMonitor-TaskExecutor"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"msg"</span>: <span class="lg-highlight-string">"Acquired connection for remote operation and completed writing to wire"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"durationMicros"</span>: <span class="lg-highlight-number">1683</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 following table describes the
<code class="leafygreen-ui-1l06pbn">
durationMicros
</code>
field in
<code class="leafygreen-ui-1l06pbn">
attr
</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">
<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">
durationMicros
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Time in microseconds that the operation waited between
acquisition of a server connection and writing the bytes to send
to the server over the network.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-messages-cache-refresh-times">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Cache Refresh Times
<a class="headerlink leafygreen-ui-1kepx47" href="#cache-refresh-times" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="cache-refresh-times">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.1,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-slow-ops" target="_self">
<span>
log messages for slow queries
</span>
</a>
include the following cache refresh time fields:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
catalogCacheDatabaseLookupDurationMillis
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
catalogCacheCollectionLookupDurationMillis
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
databaseVersionRefreshDurationMillis
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
shardVersionRefreshMillis
</code>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 7.0,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-log-message-slow-ops" target="_self">
<span>
log messages for slow queries
</span>
</a>
also include the
<code class="leafygreen-ui-1l06pbn">
catalogCacheIndexLookupDurationMillis
</code>
field that indicates the
time that the operation spent fetching information from the index
cache. This release also renames the
<code class="leafygreen-ui-1l06pbn">
shardVersionRefreshMillis
</code>
field to
<code class="leafygreen-ui-1l06pbn">
placementVersionRefreshMillis
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
The following example includes:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
catalogCacheDatabaseLookupDurationMillis
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
catalogCacheCollectionLookupDurationMillis
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
catalogCacheIndexLookupDurationMillis
</code>
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": {\n \"$date\": \"2023-03-17T09:47:55.929+00:00\"\n },\n \"s\": \"I\",\n \"c\": \"COMMAND\",\n \"id\": 51803,\n \"ctx\": \"conn14\",\n \"svc\": \"R\",\n \"msg\": \"Slow query\",\n \"attr\": {\n \"type\": \"command\",\n \"ns\": \"db.coll\",\n \"appName\": \"MongoDB Shell\",\n \"command\": {\n \"insert\": \"coll\",\n \"ordered\": true,\n \"lsid\": {\n \"id\": {\n \"$uuid\": \"5d50b19c-8559-420a-a122-8834e012274a\"\n }\n },\n \"$clusterTime\": {\n \"clusterTime\": {\n \"$timestamp\": {\n \"t\": 1679046398,\n \"i\": 8\n }\n },\n \"signature\": {\n \"hash\": {\n \"$binary\": {\n \"base64\": \"AAAAAAAAAAAAAAAAAAAAAAAAAAA=\",\n \"subType\": \"0\"\n }\n },\n \"keyId\": 0\n }\n },\n \"$db\": \"db\"\n },\n \"catalogCacheDatabaseLookupDurationMillis\": 19,\n \"catalogCacheCollectionLookupDurationMillis\": 68,\n \"catalogCacheIndexLookupDurationMillis\": 16026,\n \"nShards\": 1,\n \"ninserted\": 1,\n \"numYields\": 232,\n \"reslen\": 96,\n \"readConcern\": {\n \"level\": \"local\",\n \"provenance\": \"implicitDefault\",\n },\n \"cpuNanos\": 29640339,\n \"remote\": \"127.0.0.1:48510\",\n \"protocol\": \"op_msg\",\n \"remoteOpWaitMillis\": 4078,\n \"workingMillis\": 20334,\n \"durationMillis\": 20334\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">"t"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$date"</span>: <span class="lg-highlight-string">"2023-03-17T09:47:55.929+00:00"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>: <span class="lg-highlight-string">"I"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"COMMAND"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"id"</span>: <span class="lg-highlight-number">51803</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ctx"</span>: <span class="lg-highlight-string">"conn14"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"svc"</span>: <span class="lg-highlight-string">"R"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"msg"</span>: <span class="lg-highlight-string">"Slow query"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"command"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ns"</span>: <span class="lg-highlight-string">"db.coll"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"appName"</span>: <span class="lg-highlight-string">"MongoDB Shell"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"command"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"insert"</span>: <span class="lg-highlight-string">"coll"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ordered"</span>: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"lsid"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"id"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$uuid"</span>: <span class="lg-highlight-string">"5d50b19c-8559-420a-a122-8834e012274a"</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">"$clusterTime"</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">"$timestamp"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"t"</span>: <span class="lg-highlight-number">1679046398</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"i"</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><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>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"$binary"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"base64"</span>: <span class="lg-highlight-string">"AAAAAAAAAAAAAAAAAAAAAAAAAAA="</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"subType"</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-string">"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-string">"$db"</span>: <span class="lg-highlight-string">"db"</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"> <span class="lg-highlight-string">"catalogCacheDatabaseLookupDurationMillis"</span>: <span class="lg-highlight-number">19</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"catalogCacheCollectionLookupDurationMillis"</span>: <span class="lg-highlight-number">68</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"catalogCacheIndexLookupDurationMillis"</span>: <span class="lg-highlight-number">16026</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nShards"</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ninserted"</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numYields"</span>: <span class="lg-highlight-number">232</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"reslen"</span>: <span class="lg-highlight-number">96</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"readConcern"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"level"</span>: <span class="lg-highlight-string">"local"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"provenance"</span>: <span class="lg-highlight-string">"implicitDefault"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"cpuNanos"</span>: <span class="lg-highlight-number">29640339</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"remote"</span>: <span class="lg-highlight-string">"127.0.0.1:48510"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"protocol"</span>: <span class="lg-highlight-string">"op_msg"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"remoteOpWaitMillis"</span>: <span class="lg-highlight-number">4078</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"workingMillis"</span>: <span class="lg-highlight-number">20334</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"durationMillis"</span>: <span class="lg-highlight-number">20334</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">
Linux Syslog Limitations
<a class="headerlink leafygreen-ui-1kepx47" href="#linux-syslog-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="linux-syslog-limitations">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
In a Linux system, messages are subject to the rules defined in the Linux
configuration file
<code class="file docutils literal">
<span class="pre">
/etc/systemd/journald.conf
</span>
</code>
. By default, log message
bursts are limited to 1000 messages within a 30 second period. To see more
messages, increase the
<code class="leafygreen-ui-1l06pbn">
RateLimitBurst
</code>
parameter in
<code class="file docutils literal">
<span class="pre">
/etc/systemd/journald.conf
</span>
</code>
.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-log-messages-atlas">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Download Your Logs
<a class="headerlink leafygreen-ui-1kepx47" href="#download-your-logs" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-your-logs">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can use MongoDB Atlas to download a zipped file containing
the logs for a selected hostname or process in your database
deployment. To learn more, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/mongodb-logs/" target="_self">
<span>
View and Download MongoDB Logs
<!-- -->
.
</span>
</a>
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/replica-set-architecture-geographically-distributed/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Replica Sets Distributed Across Two or More Data Centers
<a class="headerlink leafygreen-ui-1kepx47" href="#replica-sets-distributed-across-two-or-more-data-centers" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-distributed-across-two-or-more-data-centers">
</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="#distribution-of-the-members" target="_self">
<span>
Distribution of the Members
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#electability-of-members" target="_self">
<span>
Electability of Members
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#connectivity" target="_self">
<span>
Connectivity
</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">
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">
While
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
replica sets
</a>
provide basic protection
against single-instance failure, replica sets whose members are all
located in a single data center are susceptible to data center
failures. Power outages, network interruptions, and natural disasters
are all issues that can affect replica sets whose members are located
in a single facility.
</p>
<p class="leafygreen-ui-1kp3ins">
Distributing replica set members across geographically distinct data
centers adds redundancy and provides fault tolerance if one of the data
centers is unavailable.
</p>
<p class="leafygreen-ui-1kp3ins">
Some cloud providers use
<em>
regions
</em>
and
<em>
availability zones
</em>
. Regions are
separate geographic areas. Availability zones are the same as data
centers and are separate physical locations within a region. A replica
set has high availability if the nodes are in different availability
zones because it is unlikely that all zones will fail at the same time.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Distribution of the Members
<a class="headerlink leafygreen-ui-1kepx47" href="#distribution-of-the-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="distribution-of-the-members">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To protect your data in case of a data center failure, keep at least
one member in an alternate data center. If possible, use an odd number
of data centers, and choose a distribution of members that maximizes
the likelihood that even with a loss of a data center, the remaining
replica set members can form a majority or at minimum, provide a copy
of your data.
</p>
<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>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Three-member Replica Set
<a class="headerlink leafygreen-ui-1kepx47" href="#three-member-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="three-member-replica-set">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
For example, for a three-member replica set, some possible
distributions of members include:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Two data centers: two members to Data Center 1 and one member to Data
Center 2.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If Data Center 1 goes down, the replica set becomes read-only.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If Data Center 2 goes down, the replica set remains writable as
the members in Data Center 1 can hold an election.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Three data centers: one member to Data Center 1, one member to Data
Center 2, and one member to Data Center 3.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If any Data Center goes down, the replica set remains writable as
the remaining members can hold an election.
</p>
</li>
</ul>
</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">
Distributing replica set members across two data centers provides
benefit over a single data center. In a two data center
distribution,
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If one of the data centers goes down, the data is still available
for reads unlike a single data center distribution.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the data center with a minority of the members goes down, the
replica set can still serve write operations as well as read
operations.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
However, if the data center with the majority of the members goes
down, the replica set becomes read-only.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
If possible, distribute members across at least three data centers.
For config server replica sets (CSRS), the best practice is to
distribute across three (or more depending on the number of members)
centers. If the cost of the third data center is prohibitive, one
distribution possibility is to evenly distribute the data bearing
members across the two data centers and store the remaining member
in the cloud if your company policy allows.
</p>
</div>
</div>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Five-member Replica Set
<a class="headerlink leafygreen-ui-1kepx47" href="#five-member-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="five-member-replica-set">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
For a replica set with 5 members, some possible
distributions of members include:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Two data centers: three members to Data Center 1 and two members to
Data Center 2.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If Data Center 1 goes down, the replica set becomes read-only.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If Data Center 2 goes down, the replica set remains writeable as
the members in Data Center 1 can create a majority.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Three data centers: two member to Data Center 1, two members to Data
Center 2, and one member to site Data Center 3.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If any Data Center goes down, the replica set remains writeable as
the remaining members can hold an election.
</p>
</li>
</ul>
</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">
Distributing replica set members across two data centers provides
benefit over a single data center. In a two data center
distribution,
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If one of the data centers goes down, the data is still available
for reads unlike a single data center distribution.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the data center with a minority of the members goes down, the
replica set can still serve write operations as well as read
operations.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
However, if the data center with the majority of the members goes
down, the replica set becomes read-only.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
If possible, distribute members across at least three data centers.
For config server replica sets (CSRS), the best practice is to
distribute across three (or more depending on the number of members)
centers. If the cost of the third data center is prohibitive, one
distribution possibility is to evenly distribute the data bearing
members across the two data centers and store the remaining member
in the cloud if your company policy allows.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For example, the following 5 member replica set distributes its members
across three data centers.
</p>
<div class="figure css-5vkxs0" style="width:700px">
<img alt="Diagram of a 5 member replica set distributed across three data centers." class="leafygreen-ui-1qnf57e" height="240" src="/docs/manual/images/replica-set-three-data-centers.bakedsvg.svg" style="--border-color:#C1C7C6" width="740"/>
</div>
</section>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Electability of Members
<a class="headerlink leafygreen-ui-1kepx47" href="#electability-of-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="electability-of-members">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Some members of the replica set, such as members that have networking
restraint or limited resources, should not be able to become primary in
a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-failover">
failover
</a>
. Configure members that should not become primary to
have
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-priority-0-member/#std-label-replica-set-secondary-only-members">
priority 0
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
In some cases, you may prefer that the members in one data center be
elected primary before the members in the other data centers. You can
modify the
<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>
of the members such that the
members in the one data center has higher
<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>
than the members in the other data
centers.
</p>
<p class="leafygreen-ui-1kp3ins">
In the following example, the replica set members in
Data Center 1 have a higher priority than the members in Data Center 2
and 3; the members in Data Center 2 have a higher priority than the
member in Data Center 3:
</p>
<div class="figure css-5vkxs0" style="width:700px">
<img alt="Diagram of a 5 member replica set distributed across three data centers. Replica set includes members with priority 0.5 and priority 0." class="leafygreen-ui-1qnf57e" height="240" src="/docs/manual/images/replica-set-three-data-centers-priority.bakedsvg.svg" style="--border-color:#C1C7C6" width="740"/>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Connectivity
<a class="headerlink leafygreen-ui-1kepx47" href="#connectivity" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="connectivity">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Verify that your network configuration allows each member to connect to
every other member.
</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/tutorial/deploy-geographically-distributed-replica-set/#std-label-server-replica-set-deploy-geo">
Deploy a Geographically Redundant Self-Managed Replica Set
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/deploy-replica-set/#std-label-server-replica-set-deploy">
Deploy a Self-Managed Replica Set
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/add-replica-set-arbiter/#std-label-server-replica-set-deploy-arbiter">
Add an Arbiter to a Self-Managed Replica Set
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/expand-replica-set/#std-label-server-replica-set-deploy-expand">
Add Members to a Self-Managed Replica Set
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/literal/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$literal (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-literal--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="-literal--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">
$literal
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-literal" 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.-literal">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns a value without parsing. Use for values that the aggregation
pipeline may interpret as an expression.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-literal" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$literal
</code>
</span>
</a>
expression has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $literal: <value> }"}
</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">{ $literal: <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>
</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">
<value>
</code>
is an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-literal" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$literal
</code>
</span>
</a>
does not evaluate the expression but instead
returns the unparsed expression.
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:60%"/>
<col style="width:40%"/>
</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">
{ $literal: { $add: [ 2, 3 ] } }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ "$add" : [ 2, 3 ] }
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $literal: { $literal: 1 } }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ "$literal" : 1 }
</code>
</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">
Treat
<code class="leafygreen-ui-1l06pbn">
$
</code>
as a Literal
<a class="headerlink leafygreen-ui-1kepx47" href="#treat---as-a-literal" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="treat---as-a-literal">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
In
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
,
the dollar sign
<code class="leafygreen-ui-1l06pbn">
$
</code>
evaluates to a field path; i.e. provides access
to the field. For example, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/eq/#mongodb-expression-exp.-eq">
<code class="leafygreen-ui-1l06pbn">
$eq
</code>
</a>
expression
<code class="leafygreen-ui-1l06pbn">
$eq: [
"$price", "$1" ]
</code>
performs an equality check between the value in the
field named
<code class="leafygreen-ui-1l06pbn">
price
</code>
and the value in the field named
<code class="leafygreen-ui-1l06pbn">
1
</code>
in the
document.
</p>
<p class="leafygreen-ui-1kp3ins">
The following example uses a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-literal" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$literal
</code>
</span>
</a>
expression to treat
a string that contains a dollar sign
<code class="leafygreen-ui-1l06pbn">
"$1"
</code>
as a constant value.
</p>
<p class="leafygreen-ui-1kp3ins">
A
<code class="leafygreen-ui-1l06pbn">
storeInventory
</code>
collection has the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.storeInventory.insertMany( [\n { \"_id\" : 1, \"item\" : \"napkins\", price: \"$2.50\" },\n { \"_id\" : 2, \"item\" : \"coffee\", price: \"1\" },\n { \"_id\" : 3, \"item\" : \"soap\", price: \"$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-property">storeInventory</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">"napkins"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-string">"$2.50"</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">"coffee"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-string">"1"</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">"soap"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-string">"$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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.storeInventory.aggregate( [\n { $project: { costsOneDollar: { $eq: [ \"$price\", { $literal: \"$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">storeInventory</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">costsOneDollar</span>: <!-- -->{ <span class="lg-highlight-attr">$eq</span>: <!-- -->[ <span class="lg-highlight-string">"$price"</span>, <!-- -->{ <span class="lg-highlight-attr">$literal</span>: <span class="lg-highlight-string">"$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">
This operation projects a field named
<code class="leafygreen-ui-1l06pbn">
costsOneDollar
</code>
that holds a
boolean value, indicating whether the value of the
<code class="leafygreen-ui-1l06pbn">
price
</code>
field is
equal to the string
<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":"{ \"_id\" : 1, \"costsOneDollar\" : false }\n{ \"_id\" : 2, \"costsOneDollar\" : false }\n{ \"_id\" : 3, \"costsOneDollar\" : true }","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">"costsOneDollar"</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">"costsOneDollar"</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">3</span>, <span class="lg-highlight-string">"costsOneDollar"</span> <!-- -->: <span class="lg-highlight-literal">true</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Project a New Field with Value
<code class="leafygreen-ui-1l06pbn">
1
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#project-a-new-field-with-value-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="project-a-new-field-with-value-1">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
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 uses the expression
<code class="leafygreen-ui-1l06pbn">
<field>: 1
</code>
to
include the
<code class="leafygreen-ui-1l06pbn">
<field>
</code>
in the output. The following example uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-literal" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$literal
</code>
</span>
</a>
to return a new field set to the value of
<code class="leafygreen-ui-1l06pbn">
1
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
A
<code class="leafygreen-ui-1l06pbn">
books
</code>
collection has the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.books.insertMany([\n { \"_id\" : 1, \"title\" : \"Dracula\", \"condition\": \"new\" },\n { \"_id\" : 2, \"title\" : \"The Little Prince\", \"condition\": \"new\" }\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">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">"title"</span> <!-- -->: <span class="lg-highlight-string">"Dracula"</span>, <span class="lg-highlight-string">"condition"</span>: <span class="lg-highlight-string">"new"</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">"title"</span> <!-- -->: <span class="lg-highlight-string">"The Little Prince"</span>, <span class="lg-highlight-string">"condition"</span>: <span class="lg-highlight-string">"new"</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="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-literal" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
{ $literal: 1 }
</code>
</span>
</a>
expression returns a new
<code class="leafygreen-ui-1l06pbn">
editionNumber
</code>
field set to the value
<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.books.aggregate( [\n { $project: { \"title\": 1, \"editionNumber\": { $literal: 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">books</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-string">"title"</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"editionNumber"</span>: <!-- -->{ <span class="lg-highlight-attr">$literal</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 operation results in the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"title\" : \"Dracula\", \"editionNumber\" : 1 }\n{ \"_id\" : 2, \"title\" : \"The Little Prince\", \"editionNumber\" : 1 }","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-number">1</span>, <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"Dracula"</span>, <span class="lg-highlight-string">"editionNumber"</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">2</span>, <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"The Little Prince"</span>, <span class="lg-highlight-string">"editionNumber"</span> <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/commitReshardCollection/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
commitReshardCollection
<a class="headerlink leafygreen-ui-1kepx47" href="#commitreshardcollection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="commitreshardcollection">
</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="#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">
commitReshardCollection
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.commitReshardCollection" 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.commitReshardCollection">
</div>
</a>
</dt>
<dd>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
During a resharding operation, MongoDB does not block writes until
the estimated duration to complete the resharding operation is
below
<strong>
two seconds
</strong>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If the current estimate is above two seconds but the time frame is
acceptable to you, you can finish resharding faster. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.commitReshardCollection" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
commitReshardCollection
</code>
</span>
</a>
command blocks writes early and
forces the resharding operation to complete.
</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/sh.commitReshardCollection/#mongodb-method-sh.commitReshardCollection">
<code class="leafygreen-ui-1l06pbn">
sh.commitReshardCollection()
</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-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.adminCommand(\n {\n commitReshardCollection: \"<database>.<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">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">commitReshardCollection</span>: <span class="lg-highlight-string">"<database>.<collection>"</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="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
provides a wrapper method
<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>
</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">
Commit a Resharding Operation
<a class="headerlink leafygreen-ui-1kepx47" href="#commit-a-resharding-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="commit-a-resharding-operation">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following command forces the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-reshard-a-collection/#std-label-sharding-resharding">
resharding
operation
</a>
on the
<code class="leafygreen-ui-1l06pbn">
sales.orders
</code>
to block writes
and complete:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand({\n commitReshardCollection: \"sales.orders\"\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">commitReshardCollection</span>: <span class="lg-highlight-string">"sales.orders"</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">
<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/operator/aggregation/collStats/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$collStats (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-collstats--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="-collstats--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>
</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">
$collStats
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-pipeline-pipe.-collStats" 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.-collStats">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns statistics regarding a collection or view.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
stage 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 $collStats:\n {\n latencyStats: { histograms: <boolean> },\n storageStats: { scale: <number> },\n count: {},\n queryExecStats: {}\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">$collStats</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">latencyStats</span>: <!-- -->{ <span class="lg-highlight-attr">histograms</span>: <<!-- -->boolean<!-- -->> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">storageStats</span>: <!-- -->{ <span class="lg-highlight-attr">scale</span>: <<!-- -->number<!-- -->> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">count</span>: <!-- -->{<!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">queryExecStats</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">
$collStats
</code>
stage accepts an argument document with the
following
<em>
optional
</em>
fields:
</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>
Field 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="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
latencyStats
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Adds
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-latency-stats-document" target="_self">
<span>
latency statistics
</span>
</a>
to the
return document.
</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">
latencyStats.histograms
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Adds latency histogram information to the embedded documents
in
<code class="leafygreen-ui-1l06pbn">
latencyStats
</code>
if
<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">
storageStats
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Adds
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-storage-stats-document" target="_self">
<span>
storage statistics
</span>
</a>
to the
return document.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Specify an empty document (i.e.
<code class="leafygreen-ui-1l06pbn">
storageStats: {}
</code>
) to
use the default scale factor of 1 for the various size
data. Scale factor of 1 displays the returned sizes in
bytes.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Specify the scale factor (i.e.
<code class="leafygreen-ui-1l06pbn">
storageStats: { scale:
<number> }
</code>
) to use the specified scale factor for the
various size data. For example, to display kilobytes rather
than bytes, specify a scale value of 1024.
</p>
<p class="leafygreen-ui-1kp3ins">
If you specify a non-integer scale factor, MongoDB uses the integer
part of the specified factor. For example, if you specify a scale
factor of
<code class="leafygreen-ui-1l06pbn">
1023.999
</code>
, MongoDB uses
<code class="leafygreen-ui-1l06pbn">
1023
</code>
as the scale factor.
</p>
<p class="leafygreen-ui-1kp3ins">
The scale factor does not affect those sizes that specify
the unit of measurement in the field name, such as
<code class="leafygreen-ui-1l06pbn">
"bytes
currently in the cache"
</code>
.
</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">
count
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Adds the total number of documents in the collection to the
return document.
</p>
<p class="leafygreen-ui-1kp3ins">
The count is based on the collection's metadata, which
provides a fast but sometimes inaccurate count for sharded
clusters.
</p>
<p class="leafygreen-ui-1kp3ins">
See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-collstat-count" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
count
</code>
Field
</span>
</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">
queryExecStats
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Adds
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-collstat-queryexecstats" target="_self">
<span>
query execution statistics
</span>
</a>
to the return document.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
For a collection in a replica set or a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/sharding/#std-label-sharded-vs-non-sharded-collections">
non-sharded collection
</a>
in
a cluster,
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
outputs a single document. For a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/sharding/#std-label-sharded-vs-non-sharded-collections">
sharded collection
</a>
,
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
outputs one document per shard. The output document
includes the following fields:
</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>
Field 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="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>
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-namespace">
namespace
</a>
of the requested collection or view.
</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">
shard
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
The name of the shard the output document corresponds to.
</p>
<p class="leafygreen-ui-1kp3ins">
Only present when
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
runs on a sharded cluster. Both
<a class="leafygreen-ui-tqgtui" href="/docs/manual/sharding/#std-label-sharded-vs-non-sharded-collections">
sharded and non-sharded collections
</a>
will produce 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">
host
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The hostname and port of the
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
process which produced
the output document.
</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">
localTime
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The current time on the MongoDB server, expressed as UTC
milliseconds since the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-unix-epoch">
UNIX epoch
<!-- -->
.
</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">
latencyStats
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Statistics related to request latency for a collection or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/views/#std-label-views-landing-page">
view
</a>
. See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-latency-stats-document" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
latencyStats
</code>
Document
</span>
</a>
for details on this document.
</p>
<p class="leafygreen-ui-1kp3ins">
Only present when the
<code class="leafygreen-ui-1l06pbn">
latencyStats: {}
</code>
option is specified.
</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">
storageStats
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Statistics related to a collection's storage engine. See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-storage-stats-document" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
storageStats
</code>
Document
</span>
</a>
for details on this
document.
</p>
<p class="leafygreen-ui-1kp3ins">
The various size data is scaled by the specified factor (with
the exception of those sizes that specify the unit of
measurement in the field name).
</p>
<p class="leafygreen-ui-1kp3ins">
Only present when the
<code class="leafygreen-ui-1l06pbn">
storageStats
</code>
option is specified.
</p>
<p class="leafygreen-ui-1kp3ins">
Returns an error if applied to a view.
</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">
count
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
The total number of documents in the collection. This data is
also available in
<code class="leafygreen-ui-1l06pbn">
storageStats.count
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
The count is based on the collection's metadata, which
provides a fast but sometimes inaccurate count for sharded
clusters.
</p>
<p class="leafygreen-ui-1kp3ins">
Only present when the
<code class="leafygreen-ui-1l06pbn">
count: {}
</code>
option is specified. Returns
an error if applied to a view.
</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">
queryExecStats
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Statistics related to query execution for the collection.
</p>
<p class="leafygreen-ui-1kp3ins">
Only present when the
<code class="leafygreen-ui-1l06pbn">
queryExecStats: {}
</code>
option is
specified. Returns an error if applied to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/views/#std-label-views-landing-page">
view
<!-- -->
.
</a>
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</dd>
</dl>
<span class="leafygreen-ui-1qnjd6d" id="std-label-coll-stats-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">
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
must be the first stage in an aggregation pipeline, or
else the pipeline returns an error.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Accuracy After Unexpected Shutdown
<a class="headerlink leafygreen-ui-1kepx47" href="#accuracy-after-unexpected-shutdown" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="accuracy-after-unexpected-shutdown">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
After an unclean shutdown of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/">
Wired Tiger
</a>
storage engine,
<!-- -->
size and count
<!-- -->
statistics reported by
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
may be inaccurate.
</p>
<p class="leafygreen-ui-1kp3ins">
The amount of drift depends on the number of insert, update, or delete
operations performed between the last
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger-checkpoints">
checkpoint
</a>
and the unclean shutdown. Checkpoints
usually occur every 60 seconds. However,
<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
with non-default
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--syncdelay">
<code class="leafygreen-ui-1l06pbn">
--syncdelay
</code>
</a>
settings may have more or less frequent
checkpoints.
</p>
<p class="leafygreen-ui-1kp3ins">
Run
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/validate/#mongodb-dbcommand-dbcmd.validate">
<code class="leafygreen-ui-1l06pbn">
validate
</code>
</a>
on each collection on 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 restore statistics after an unclean shutdown.
</p>
<p class="leafygreen-ui-1kp3ins">
After an unclean shutdown:
</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/command/validate/#mongodb-dbcommand-dbcmd.validate">
<code class="leafygreen-ui-1l06pbn">
validate
</code>
</a>
updates the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-collstat-count" target="_self">
<span>
count statistic
</span>
</a>
in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collStats/#mongodb-dbcommand-dbcmd.collStats">
<code class="leafygreen-ui-1l06pbn">
collStats
</code>
</a>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collStats/#std-label-collStats-output">
output
</a>
with the latest value.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Other statistics like the number of documents inserted or removed in
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collStats/#mongodb-dbcommand-dbcmd.collStats">
<code class="leafygreen-ui-1l06pbn">
collStats
</code>
</a>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collStats/#std-label-collStats-output">
output
</a>
are
estimates.
</p>
</li>
</ul>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Redaction
<a class="headerlink leafygreen-ui-1kepx47" href="#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="redaction">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/#std-label-qe-manual-feature-qe">
Queryable Encryption
</a>
,
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
output redacts certain information for encrypted
collections:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The output omits
<code class="leafygreen-ui-1l06pbn">
"queryExecStats"
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The output omits
<code class="leafygreen-ui-1l06pbn">
"latencyStats"
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The output redacts
<code class="leafygreen-ui-1l06pbn">
"WiredTiger"
</code>
, if present, to include only the
<code class="leafygreen-ui-1l06pbn">
url
</code>
field.
</p>
</li>
</ul>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Transactions
<a class="headerlink leafygreen-ui-1kepx47" href="#transactions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="transactions">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
is not allowed in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
transactions
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-latency-stats-document">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
latencyStats
</code>
Document
<a class="headerlink leafygreen-ui-1kepx47" href="#latencystats-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="latencystats-document">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
latencyStats
</code>
embedded document only exists in the output if
you specify the
<code class="leafygreen-ui-1l06pbn">
latencyStats
</code>
option.
</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>
Field 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>
<code class="leafygreen-ui-1l06pbn">
reads
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Latency statistics for read requests.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
writes
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Latency statistics for write requests.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
commands
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Latency statistics for database commands.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
transactions
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Latency statistics for database transactions.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
Each of these fields contains an embedded document bearing the
following fields:
</p>
<div class="header-buffer" id="ref-inclusive_symbols-id1">
</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>
Field 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>
<code class="leafygreen-ui-1l06pbn">
latency
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A 64-bit integer giving the total combined
latency in microseconds.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
ops
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A 64-bit integer giving the total number of
operations performed on the collection since startup.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
histogram
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
An array of embedded documents, each representing a latency range.
Each document covers twice the previous document's range. For
lower values between 2048 microseconds and roughly 1 second,
the histogram includes half-steps.
</p>
<p class="leafygreen-ui-1kp3ins">
This field only exists given the
<code class="leafygreen-ui-1l06pbn">
latencyStats: { histograms: true }
</code>
option. Empty ranges with
a zero
<code class="leafygreen-ui-1l06pbn">
count
</code>
are omitted from the output.
</p>
<p class="leafygreen-ui-1kp3ins">
Each document bears the following fields:
</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>
Field Name
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Description
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
micros
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
A 64-bit integer giving the inclusive
lower time bound of the current latency range in
microseconds.
</p>
<p class="leafygreen-ui-1kp3ins">
The document's range spans between the previous document's
<code class="leafygreen-ui-1l06pbn">
micros
</code>
value, exclusive, and this document's
<code class="leafygreen-ui-1l06pbn">
micros
</code>
value, inclusive.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
count
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A 64-bit integer giving the number of
operations with latency less than or equal to
<code class="leafygreen-ui-1l06pbn">
micros
</code>
.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
For example, if
<code class="leafygreen-ui-1l06pbn">
collStats
</code>
returns the following histogram:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"histogram: [\n { micros: NumberLong(0), count: NumberLong(10) },\n { micros: NumberLong(2), count: NumberLong(1) },\n { micros: NumberLong(4096), count: NumberLong(1) },\n { micros: NumberLong(16384), count: NumberLong(1000) },\n { micros: NumberLong(49152), count: NumberLong(100) }\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-attr">histogram</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">micros</span>: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">0</span>)<!-- -->, <span class="lg-highlight-attr">count</span>: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">10</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">micros</span>: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">2</span>)<!-- -->, <span class="lg-highlight-attr">count</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-attr">micros</span>: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">4096</span>)<!-- -->, <span class="lg-highlight-attr">count</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-attr">micros</span>: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">16384</span>)<!-- -->, <span class="lg-highlight-attr">count</span>: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">1000</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">micros</span>: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">49152</span>)<!-- -->, <span class="lg-highlight-attr">count</span>: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">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">
This indicates that there were
<a class="footnote-reference header-buffer" href="#footnote-inclusive_symbols" id="ref-inclusive_symbols-id1">
[
<!-- -->
1
<!-- -->
]
</a>
:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
10 operations taking 2 microsecond or less
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
1 operation in the range [2, 4) microseconds
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
1 operation in the range [4096, 6144) microseconds
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
1000 operations in the range [16384, 24576) microseconds
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
100 operations in the range [49152, 65536) microseconds
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-inclusive_symbols" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<a href="#ref-inclusive_symbols-id1">
1
</a>
]
</td>
<td class="leafygreen-ui-17r2xs0">
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
The
<code class="leafygreen-ui-1l06pbn">
(
</code>
symbol notation on this page means the value is exclusive.
</li>
<li class="leafygreen-ui-d7p7b6">
The
<code class="leafygreen-ui-1l06pbn">
]
</code>
symbol notation on this page means the value is inclusive.
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p class="leafygreen-ui-1kp3ins">
For example, if you run
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
with the
<code class="leafygreen-ui-1l06pbn">
latencyStats: {}
</code>
option
on a
<code class="leafygreen-ui-1l06pbn">
matrices
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.matrices.aggregate( [ { $collStats: { latencyStats: { histograms: 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">matrices</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$collStats</span>: <!-- -->{ <span class="lg-highlight-attr">latencyStats</span>: <!-- -->{ <span class="lg-highlight-attr">histograms</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">
This query returns a result similar to the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"ns\" : \"test.matrices\",\n \"host\" : \"mongo.example.net:27017\",\n \"localTime\" : ISODate(\"2017-10-06T19:43:56.599Z\"),\n \"latencyStats\" :\n { \"reads\" :\n { \"histogram\" : [\n { \"micros\" : NumberLong(16),\n \"count\" : NumberLong(3) },\n { \"micros\" : NumberLong(32),\n \"count\" : NumberLong(1) },\n { \"micros\" : NumberLong(128),\n \"count\" : NumberLong(1) } ],\n \"latency\" : NumberLong(264),\n \"ops\" : NumberLong(5) },\n \"writes\" :\n { \"histogram\" : [\n { \"micros\" : NumberLong(32),\n \"count\" : NumberLong(1) },\n { \"micros\" : NumberLong(64),\n \"count\" : NumberLong(3) },\n { \"micros\" : NumberLong(24576),\n \"count\" : NumberLong(1) } ],\n \"latency\" : NumberLong(27659),\n \"ops\" : NumberLong(5) },\n \"commands\" :\n { \"histogram\" : [\n {\n \"micros\" : NumberLong(196608),\n \"count\" : NumberLong(1)\n }\n ],\n \"latency\" : NumberLong(0),\n \"ops\" : NumberLong(0) },\n \"transactions\" : {\n \"histogram\" : [ ],\n \"latency\" : NumberLong(0),\n \"ops\" : NumberLong(0)\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-string">"ns"</span> <!-- -->: <span class="lg-highlight-string">"test.matrices"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"host"</span> <!-- -->: <span class="lg-highlight-string">"mongo.example.net:27017"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"localTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2017-10-06T19:43:56.599Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"latencyStats"</span> <!-- -->:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"reads"</span> <!-- -->:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"histogram"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"micros"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">16</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">3</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"micros"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">32</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</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">"micros"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">128</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</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">"latency"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">264</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ops"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">5</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"writes"</span> <!-- -->:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"histogram"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"micros"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">32</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</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">"micros"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">64</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">3</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"micros"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">24576</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</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">"latency"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">27659</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ops"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">5</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"commands"</span> <!-- -->:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"histogram"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"micros"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">196608</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</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"> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"latency"</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"> <span class="lg-highlight-string">"ops"</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"> <span class="lg-highlight-string">"transactions"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"histogram"</span> <!-- -->: <!-- -->[ <!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"latency"</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"> <span class="lg-highlight-string">"ops"</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></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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-storage-stats-document">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
storageStats
</code>
Document
<a class="headerlink leafygreen-ui-1kepx47" href="#storagestats-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="storagestats-document">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
storageStats
</code>
embedded document only exists in the output if you
specify the
<code class="leafygreen-ui-1l06pbn">
storageStats
</code>
option.
</p>
<p class="leafygreen-ui-1kp3ins">
The contents of this document are dependent on the storage engine in use.
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collStats/#std-label-collStats-output">
Output
</a>
for a reference on this document.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, if you run
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
with the
<code class="leafygreen-ui-1l06pbn">
storageStats: {}
</code>
option on a
<code class="leafygreen-ui-1l06pbn">
matrices
</code>
collection using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger Storage Engine
<!-- -->
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.matrices.aggregate( [ { $collStats: { storageStats: { } } } ] )","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">matrices</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$collStats</span>: <!-- -->{ <span class="lg-highlight-attr">storageStats</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 query returns a result similar to the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"ns\" : \"test.matrices\",\n \"host\" : \"mongo.example.net:27017\",\n \"localTime\" : ISODate(\"2020-03-06T01:44:57.437Z\"),\n \"storageStats\" : {\n \"size\" : 608500363,\n \"count\" : 1104369,\n \"avgObjSize\" : 550,\n \"storageSize\" : 352878592,\n \"freeStorageSize\" : 2490380,\n \"capped\" : false,\n \"wiredTiger\" : {\n ...\n },\n \"nindexes\" : 2,\n \"indexDetails\" : {\n ...\n },\n \"indexBuilds\" : [\n \"_id_1_abc_1\"\n ],\n \"totalIndexSize\" : 260337664,\n \"totalSize\" : 613216256,\n \"indexSizes\" : {\n \"_id_\" : 9891840,\n \"_id_1_abc_1\" : 250445824\n },\n \"scaleFactor\" : 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">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ns"</span> <!-- -->: <span class="lg-highlight-string">"test.matrices"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"host"</span> <!-- -->: <span class="lg-highlight-string">"mongo.example.net:27017"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"localTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-03-06T01:44:57.437Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"storageStats"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"size"</span> <!-- -->: <span class="lg-highlight-number">608500363</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">1104369</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"avgObjSize"</span> <!-- -->: <span class="lg-highlight-number">550</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"storageSize"</span> <!-- -->: <span class="lg-highlight-number">352878592</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"freeStorageSize"</span> <!-- -->: <span class="lg-highlight-number">2490380</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"capped"</span> <!-- -->: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"wiredTiger"</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">"nindexes"</span> <!-- -->: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"indexDetails"</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">"indexBuilds"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id_1_abc_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">"totalIndexSize"</span> <!-- -->: <span class="lg-highlight-number">260337664</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"totalSize"</span> <!-- -->: <span class="lg-highlight-number">613216256</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"indexSizes"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id_"</span> <!-- -->: <span class="lg-highlight-number">9891840</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id_1_abc_1"</span> <!-- -->: <span class="lg-highlight-number">250445824</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"scaleFactor"</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>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collStats/#std-label-collStats-output">
Output
</a>
for a reference on this document.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
In-progress Indexes
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The returned
<code class="leafygreen-ui-1l06pbn">
storageStats
</code>
includes information on indexes being built. For details, 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/reference/command/collStats/#mongodb-data-collStats.nindexes">
<code class="leafygreen-ui-1l06pbn">
collStats.nindexes
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collStats/#mongodb-data-collStats.indexDetails">
<code class="leafygreen-ui-1l06pbn">
collStats.indexDetails
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collStats/#mongodb-data-collStats.indexBuilds">
<code class="leafygreen-ui-1l06pbn">
collStats.indexBuilds
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collStats/#mongodb-data-collStats.totalIndexSize">
<code class="leafygreen-ui-1l06pbn">
collStats.totalIndexSize
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collStats/#mongodb-data-collStats.indexSizes">
<code class="leafygreen-ui-1l06pbn">
collStats.indexSizes
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Performing
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
with the
<code class="leafygreen-ui-1l06pbn">
storageStats
</code>
option on a
view results in an error.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-collstat-count">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
count
</code>
Field
<a class="headerlink leafygreen-ui-1kepx47" href="#count-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="count-field">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
count
</code>
field only exists in the output if you specify the
<code class="leafygreen-ui-1l06pbn">
count
</code>
option.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, if you run
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
with the
<code class="leafygreen-ui-1l06pbn">
count: {}
</code>
option on
a
<code class="leafygreen-ui-1l06pbn">
matrices
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.matrices.aggregate( [ { $collStats: { count: { } } } ] )","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">matrices</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$collStats</span>: <!-- -->{ <span class="lg-highlight-attr">count</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 returns a result similar to the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"ns\" : \"test.matrices\",\n \"host\" : \"mongo.example.net:27017\",\n \"localTime\" : ISODate(\"2017-10-06T19:43:56.599Z\"),\n \"count\" : 1103869\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">"ns"</span> <!-- -->: <span class="lg-highlight-string">"test.matrices"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"host"</span> <!-- -->: <span class="lg-highlight-string">"mongo.example.net:27017"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"localTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2017-10-06T19:43:56.599Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">1103869</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-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The count is based on the collection's metadata, which provides a
fast but sometimes inaccurate count for sharded clusters.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The total number of documents in the collection is also available as
<code class="leafygreen-ui-1l06pbn">
storageStats.count
</code>
when
<code class="leafygreen-ui-1l06pbn">
storageStats: {}
</code>
is specified. For more
information, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-storage-stats-document" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
storageStats
</code>
Document
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-collstat-queryexecstats">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
queryExecStats
</code>
Document
<a class="headerlink leafygreen-ui-1kepx47" href="#queryexecstats-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="queryexecstats-document">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
queryExecStats
</code>
embedded document only exists in the output if
you specify the
<code class="leafygreen-ui-1l06pbn">
queryExecStats
</code>
option.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
collectionScans
</code>
field contains an embedded document bearing the
following fields:
</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>
Field 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>
<code class="leafygreen-ui-1l06pbn">
total
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A 64-bit integer giving the total number of queries that
performed a collection scan. The total consists of queries that
did and did not use a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/tailable-cursors/">
tailable cursor
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
nonTailable
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A 64-bit integer giving the number of queries that performed a
collection scan that did not use a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/tailable-cursors/">
tailable cursor
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
For example, if you run
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
with the
<code class="leafygreen-ui-1l06pbn">
queryExecStats: {}
</code>
option on a
<code class="leafygreen-ui-1l06pbn">
matrices
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.matrices.aggregate( [ { $collStats: { queryExecStats: { } } } ] )","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">matrices</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$collStats</span>: <!-- -->{ <span class="lg-highlight-attr">queryExecStats</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 returns a result similar to the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"ns\": \"test.matrices\",\n \"host\": \"mongo.example.net:27017\",\n \"localTime\": ISODate(\"2020-06-03T14:23:29.711Z\"),\n \"queryExecStats\": {\n \"collectionScans\": {\n \"total\": NumberLong(33),\n \"nonTailable\": NumberLong(31)\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">"ns"</span>: <span class="lg-highlight-string">"test.matrices"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"host"</span>: <span class="lg-highlight-string">"mongo.example.net:27017"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"localTime"</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-06-03T14:23:29.711Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"queryExecStats"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"collectionScans"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"total"</span>: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">33</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nonTailable"</span>: <span class="lg-highlight-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-number">31</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>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
on Sharded Collections
<a class="headerlink leafygreen-ui-1kepx47" href="#-collstats-on-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="-collstats-on-sharded-collections">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
outputs one document per shard when run on
<a class="leafygreen-ui-tqgtui" href="/docs/manual/sharding/#std-label-sharded-vs-non-sharded-collections">
sharded collections
</a>
. Each
output document contains a
<code class="leafygreen-ui-1l06pbn">
shard
</code>
field with the name of the shard
the document corresponds to.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, if you run
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
on a sharded collection with the
<code class="leafygreen-ui-1l06pbn">
count: {}
</code>
option on a collection named
<code class="leafygreen-ui-1l06pbn">
matrices
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.matrices.aggregate( [ { $collStats: { count: { } } } ] )","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">matrices</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$collStats</span>: <!-- -->{ <span class="lg-highlight-attr">count</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 returns a result similar to the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"ns\" : \"test.matrices\",\n \"shard\" : \"s1\",\n \"host\" : \"s1-mongo1.example.net:27017\",\n \"localTime\" : ISODate(\"2017-10-06T15:14:21.258Z\"),\n \"count\" : 661705\n}\n{\n \"ns\" : \"test.matrices\",\n \"shard\" : \"s2\",\n \"host\" : \"s2-mongo1.example.net:27017\",\n \"localTime\" : ISODate(\"2017-10-06T15:14:21.258Z\"),\n \"count\" : 442164\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">"ns"</span> <!-- -->: <span class="lg-highlight-string">"test.matrices"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"shard"</span> <!-- -->: <span class="lg-highlight-string">"s1"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"host"</span> <!-- -->: <span class="lg-highlight-string">"s1-mongo1.example.net:27017"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"localTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2017-10-06T15:14:21.258Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">661705</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">"ns"</span> <!-- -->: <span class="lg-highlight-string">"test.matrices"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"shard"</span> <!-- -->: <span class="lg-highlight-string">"s2"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"host"</span> <!-- -->: <span class="lg-highlight-string">"s2-mongo1.example.net:27017"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"localTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2017-10-06T15:14:21.258Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">442164</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/command/collStats/#mongodb-dbcommand-dbcmd.collStats">
<code class="leafygreen-ui-1l06pbn">
collStats
</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.stats/#mongodb-method-db.collection.stats">
<code class="leafygreen-ui-1l06pbn">
db.collection.stats()
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/backup-sharded-cluster-metadata/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Back Up Self-Managed Cluster Metadata
<a class="headerlink leafygreen-ui-1kepx47" href="#back-up-self-managed-cluster-metadata" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-self-managed-cluster-metadata">
</div>
</a>
</h1>
<p class="leafygreen-ui-1kp3ins">
This procedure 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>
instance of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-config-servers/#std-label-sharding-config-server">
config server
</a>
in order to create a
backup of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/sharding/#std-label-sharding-sharded-cluster">
sharded cluster's
</a>
metadata. The cluster's config servers store all of the cluster's
metadata, most importantly the mapping from
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-chunk">
chunks
</a>
to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard">
shards
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
When you perform this procedure, the cluster remains operational
<a class="footnote-reference header-buffer" href="#footnote-read-only" id="ref-read-only-id1">
[
<!-- -->
1
<!-- -->
]
</a>
.
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Disable the cluster balancer process temporarily. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-sharded-cluster-balancer/#std-label-sharding-balancing-disable-temporarily">
Disable the Balancer
</a>
for more information.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Shut down one of the config databases.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Create a full copy of the data files (i.e. the path specified by
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-storage.dbPath">
<code class="leafygreen-ui-1l06pbn">
dbPath
</code>
</a>
option for the config instance.)
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Restart the original configuration server.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Re-enable the balancer to allow the cluster to resume normal
balancing operations. See the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-sharded-cluster-balancer/#std-label-sharding-balancing-disable-temporarily">
Disable the Balancer
</a>
section for more
information on managing the balancer process.
</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/core/backups/">
Backup Methods for a Self-Managed Deployment
</a>
</p>
</div>
</div>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-read-only" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<a href="#ref-read-only-id1">
1
</a>
]
</td>
<td class="leafygreen-ui-17r2xs0">
<!-- -->
While one of the three config servers is unavailable,
the cluster cannot split any chunks nor can it migrate chunks
between shards. Your application will be able to write data to the
cluster. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharded-cluster-config-servers/#std-label-sharding-config-server">
Config Servers
</a>
for more information.
</td>
</tr>
</tbody>
</table>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/setEquals/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$setEquals (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-setequals--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="-setequals--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">
$setEquals
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-setEquals" 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.-setEquals">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Compares two or more arrays and returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
if they have the
same distinct elements and
<code class="leafygreen-ui-1l06pbn">
false
</code>
otherwise.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-setEquals" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$setEquals
</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":"{ $setEquals: [ <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">$setEquals</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">
The arguments can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
as long as they each resolve to an array.
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">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-setEquals" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$setEquals
</code>
</span>
</a>
performs set operation on arrays, treating arrays
as sets. If an array contains duplicate entries,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-setEquals" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$setEquals
</code>
</span>
</a>
ignores the duplicate entries.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-setEquals" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$setEquals
</code>
</span>
</a>
ignores the order of
the elements.
</p>
<p class="leafygreen-ui-1kp3ins">
If a set contains a nested array element,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-setEquals" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$setEquals
</code>
</span>
</a>
does
<em>
not
</em>
descend
into the nested array but evaluates the array at top-level.
</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>
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">
{ $setEquals: [ [ "a", "b", "a" ], [ "b", "a" ] ] }
</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="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $setEquals: [ [ "a", "b" ], [ [ "a", "b" ] ] ] }
</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">
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
<code class="leafygreen-ui-1l06pbn">
bakeryOrders
</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.bakeryOrders.insertMany( [\n { _id: 0, cakes: [\"chocolate\", \"vanilla\"], cupcakes: [\"chocolate\", \"vanilla\"] },\n { _id: 1, cakes: [\"chocolate\", \"vanilla\"], cupcakes: [\"vanilla\", \"chocolate\"] },\n { _id: 2, cakes: [\"chocolate\", \"chocolate\"], cupcakes: [\"chocolate\"] },\n { _id: 3, cakes: [\"vanilla\"], cupcakes: [\"chocolate\"] },\n { _id: 4, cakes: [\"vanilla\"], cupcakes: [] }\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">bakeryOrders</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">cakes</span>: <!-- -->[<span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"vanilla"</span>]<!-- -->, <span class="lg-highlight-attr">cupcakes</span>: <!-- -->[<span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"vanilla"</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">cakes</span>: <!-- -->[<span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"vanilla"</span>]<!-- -->, <span class="lg-highlight-attr">cupcakes</span>: <!-- -->[<span class="lg-highlight-string">"vanilla"</span>, <span class="lg-highlight-string">"chocolate"</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">cakes</span>: <!-- -->[<span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"chocolate"</span>]<!-- -->, <span class="lg-highlight-attr">cupcakes</span>: <!-- -->[<span class="lg-highlight-string">"chocolate"</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">cakes</span>: <!-- -->[<span class="lg-highlight-string">"vanilla"</span>]<!-- -->, <span class="lg-highlight-attr">cupcakes</span>: <!-- -->[<span class="lg-highlight-string">"chocolate"</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">cakes</span>: <!-- -->[<span class="lg-highlight-string">"vanilla"</span>]<!-- -->, <span class="lg-highlight-attr">cupcakes</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
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-setEquals" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$setEquals
</code>
</span>
</a>
operator to
determine if the
<code class="leafygreen-ui-1l06pbn">
cakes
</code>
array and the
<code class="leafygreen-ui-1l06pbn">
cupcakes
</code>
array in each order
contain the same flavors:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bakeryOrders.aggregate(\n [\n {\n $project: {\n _id: 0,\n cakes: 1,\n cupcakes: 1,\n sameFlavors: { $setEquals: [ \"$cakes\", \"$cupcakes\" ] }\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">bakeryOrders</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">_id</span>: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cakes</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cupcakes</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sameFlavors</span>: <!-- -->{ <span class="lg-highlight-attr">$setEquals</span>: <!-- -->[ <span class="lg-highlight-string">"$cakes"</span>, <span class="lg-highlight-string">"$cupcakes"</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-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
$project
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
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 specifies which fields are included
in the output documents. In this example, 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>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Excludes the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field from the output.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Includes the
<code class="leafygreen-ui-1l06pbn">
cakes
</code>
and
<code class="leafygreen-ui-1l06pbn">
cupcakes
</code>
fields in the output.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Outputs the result of the
<code class="leafygreen-ui-1l06pbn">
$setEquals
</code>
operator in a new field
called
<code class="leafygreen-ui-1l06pbn">
sameFlavors
</code>
.
</p>
</li>
</ul>
</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":"{\n cakes: [ \"chocolate\", \"vanilla\" ],\n cupcakes: [ \"chocolate\", \"vanilla\" ],\n sameFlavors: true\n},\n{\n cakes: [ \"chocolate\", \"vanilla\" ],\n cupcakes: [ \"vanilla\", \"chocolate\" ],\n sameFlavors: true\n},\n{\n cakes: [ \"chocolate\", \"chocolate\" ],\n cupcakes: [ \"chocolate\" ],\n sameFlavors: true\n},\n{\n cakes: [ \"vanilla\" ],\n cupcakes: [ \"chocolate\" ],\n sameFlavors: false\n},\n{\n cakes: [ \"vanilla\" ],\n cupcakes: [],\n sameFlavors: 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">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cakes</span>: <!-- -->[ <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"vanilla"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cupcakes</span>: <!-- -->[ <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"vanilla"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sameFlavors</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><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cakes</span>: <!-- -->[ <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"vanilla"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cupcakes</span>: <!-- -->[ <span class="lg-highlight-string">"vanilla"</span>, <span class="lg-highlight-string">"chocolate"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sameFlavors</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><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cakes</span>: <!-- -->[ <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"chocolate"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cupcakes</span>: <!-- -->[ <span class="lg-highlight-string">"chocolate"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sameFlavors</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><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cakes</span>: <!-- -->[ <span class="lg-highlight-string">"vanilla"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cupcakes</span>: <!-- -->[ <span class="lg-highlight-string">"chocolate"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sameFlavors</span>: <span class="lg-highlight-literal">false</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">cakes</span>: <!-- -->[ <span class="lg-highlight-string">"vanilla"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cupcakes</span>: <!-- -->[<!-- -->]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sameFlavors</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>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/mergeAllChunksOnShard/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
mergeAllChunksOnShard
<a class="headerlink leafygreen-ui-1kepx47" href="#mergeallchunksonshard" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mergeallchunksonshard">
</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="#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">
mergeAllChunksOnShard
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.mergeAllChunksOnShard" 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.mergeAllChunksOnShard">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
mergeAllChunksOnShard
</code>
finds and merges all
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-mergeability" target="_self">
<span>
mergeable
chunks
</span>
</a>
that a shard owns for a given 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 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.adminCommand(\n {\n mergeAllChunksOnShard: <name>,\n shard: <name>,\n maxNumberOfChunksToMerge: <maximum> /* optional */\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">mergeAllChunksOnShard</span>: <!-- --><name of the collection>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> shard: <name of the shard>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> maxNumberOfChunksToMerge: <maximum number of chunks to merge> /* optional */</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: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>
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">
mergeAllChunksOnShard
</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.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
shard
</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 shard.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
maxNumberOfChunksToMerge
</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>
Maximum number of chunks to merge.
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-mergeability">
</span>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
mergeAllChunksOnShard
</code>
finds and merges all mergeable chunks for a
collection on the same shard. Two or more contiguous chunks in the same
collection are
<strong>
mergeable
</strong>
when they meet all of these conditions:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
They are owned by the same shard.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
They are not
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-data-partitioning/#std-label-jumbo-chunk">
jumbo
</a>
chunks.
<code class="leafygreen-ui-1l06pbn">
jumbo
</code>
chunks are
not mergeable because they cannot participate in migrations.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Their history can be purged safely, without breaking transactions and
snapshot reads:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The last migration involving the chunk happened at least as many
seconds ago as the value of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.minSnapshotHistoryWindowInSeconds">
<code class="leafygreen-ui-1l06pbn">
minSnapshotHistoryWindowInSeconds
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The last migration involving the chunk happened at least as many
seconds ago as the value of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.transactionLifetimeLimitSeconds">
<code class="leafygreen-ui-1l06pbn">
transactionLifetimeLimitSeconds
</code>
.
</a>
</p>
</li>
</ul>
</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">
This example assumes that history is empty for all chunks and all chunks
are non-jumbo. Since both conditions are true, all contiguous intervals
on the same shard are
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-mergeability" target="_self">
<span>
mergeable
<!-- -->
.
</span>
</a>
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Setup
<a class="headerlink leafygreen-ui-1kepx47" href="#setup" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="setup">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
These chunks belong to a collection named
<code class="leafygreen-ui-1l06pbn">
coll
</code>
with shard key
<code class="leafygreen-ui-1l06pbn">
x
</code>
.
There are nine chunks in total.
</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:25%"/>
<col style="width:25%"/>
</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>
Chunk ID
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Min
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Max
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Shard
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 0
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 10
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
B
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 10
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 20
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
C
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 20
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 30
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
D
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 30
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 40
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
E
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 40
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 50
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard1
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
F
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 50
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 60
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard1
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
G
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 60
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 70
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
H
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 70
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 80
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
I
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 80
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 90
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard1
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" 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>
</h3>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"This command merges the contiguous sequences of chunks:A-B-C-DG-H","name":"Merge All Mergeable Chunks on Shard0"},{"@type":"HowToStep","text":"This command merges the contiguous sequences of chunks E-F.","name":"Merge All Mergeable Chunks on Shard1"}],"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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Merge All Mergeable Chunks on Shard0
<a class="headerlink leafygreen-ui-1kepx47" href="#merge-all-mergeable-chunks-on-shard0" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="merge-all-mergeable-chunks-on-shard0">
</div>
</a>
</h4>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { mergeAllChunksOnShard: \"db.coll\", shard: \"Shard0\" } )","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">mergeAllChunksOnShard</span>: <span class="lg-highlight-string">"db.coll"</span>, <span class="lg-highlight-attr">shard</span>: <span class="lg-highlight-string">"Shard0"</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 command merges the contiguous sequences of chunks:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A-B-C-D
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
G-H
</p>
</li>
</ul>
</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">
Merge All Mergeable Chunks on Shard1
<a class="headerlink leafygreen-ui-1kepx47" href="#merge-all-mergeable-chunks-on-shard1" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="merge-all-mergeable-chunks-on-shard1">
</div>
</a>
</h4>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { mergeAllChunksOnShard: \"db.coll\", shard: \"Shard1\" } )","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">mergeAllChunksOnShard</span>: <span class="lg-highlight-string">"db.coll"</span>, <span class="lg-highlight-attr">shard</span>: <span class="lg-highlight-string">"Shard1"</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 command merges the contiguous sequences of chunks E-F.
</p>
</section>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Result
<a class="headerlink leafygreen-ui-1kepx47" href="#result" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="result">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
After these commands have completed, the contiguous chunks have been
merged. There are four total chunks instead of the original nine.
</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:25%"/>
<col style="width:25%"/>
</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>
Chunk ID
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Min
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Max
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Shard
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A-B-C-D
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 0
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 40
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
E-F
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 40
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 60
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard1
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
G-H
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 60
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 80
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard0
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
I
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 80
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
x: 90
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Shard1
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/db.grantRolesToUser/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.grantRolesToUser()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.grantrolestouser--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.grantrolestouser--">
</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.grantRolesToUser(username, roles, writeConcern)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.grantRolesToUser" 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.grantRolesToUser">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Grants additional roles to a user.
</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/grantRolesToUser/#mongodb-dbcommand-dbcmd.grantRolesToUser">
<code class="leafygreen-ui-1l06pbn">
grantRolesToUser
</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.grantRolesToUser" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.grantRolesToUser()
</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.grantRolesToUser( \"<username>\", [ <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">grantRolesToUser</span>( <span class="lg-highlight-string">"<username>"</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.grantRolesToUser" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.grantRolesToUser()
</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">
user
</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 to whom to grant 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>
An array of additional roles to grant to the user.
</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.grantRolesToUser" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.grantRolesToUser()
</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>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.grantRolesToUser" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.grantRolesToUser()
</code>
</span>
</a>
method wraps the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/grantRolesToUser/#mongodb-dbcommand-dbcmd.grantRolesToUser">
<code class="leafygreen-ui-1l06pbn">
grantRolesToUser
</code>
</a>
command.
</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">
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.grantRolesToUser" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.grantRolesToUser()
</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-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 a database to grant 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">
Given a user
<code class="leafygreen-ui-1l06pbn">
accountUser01
</code>
in the
<code class="leafygreen-ui-1l06pbn">
products
</code>
database with the following
roles:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\"roles\" : [\n { \"role\" : \"assetsReader\",\n \"db\" : \"assets\"\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-string">"roles"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"role"</span> <!-- -->: <span class="lg-highlight-string">"assetsReader"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"db"</span> <!-- -->: <span class="lg-highlight-string">"assets"</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
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.grantRolesToUser" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.grantRolesToUser()
</code>
</span>
</a>
operation gives
<code class="leafygreen-ui-1l06pbn">
accountUser01
</code>
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-readWrite">
<code class="leafygreen-ui-1l06pbn">
readWrite
</code>
</a>
role on the
<code class="leafygreen-ui-1l06pbn">
products
</code>
database and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-read">
<code class="leafygreen-ui-1l06pbn">
read
</code>
</a>
role on the
<code class="leafygreen-ui-1l06pbn">
stock
</code>
database.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use products\ndb.grantRolesToUser(\n \"accountUser01\",\n [ \"readWrite\" , { role: \"read\", db: \"stock\" } ],\n { w: \"majority\" , wtimeout: 4000 }\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">grantRolesToUser</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"accountUser01"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[ <span class="lg-highlight-string">"readWrite"</span> <!-- -->, <!-- -->{ <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">"stock"</span> } ]<!-- -->,</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">4000</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
<code class="leafygreen-ui-1l06pbn">
accountUser01
</code>
in the
<code class="leafygreen-ui-1l06pbn">
products
</code>
database now has the following
roles:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\"roles\" : [\n { \"role\" : \"assetsReader\",\n \"db\" : \"assets\"\n },\n { \"role\" : \"read\",\n \"db\" : \"stock\"\n },\n { \"role\" : \"readWrite\",\n \"db\" : \"products\"\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-string">"roles"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"role"</span> <!-- -->: <span class="lg-highlight-string">"assetsReader"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"db"</span> <!-- -->: <span class="lg-highlight-string">"assets"</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">"read"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"db"</span> <!-- -->: <span class="lg-highlight-string">"stock"</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">"readWrite"</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"> }</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/sql-comparison/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
SQL to MongoDB Mapping Chart
<a class="headerlink leafygreen-ui-1kepx47" href="#sql-to-mongodb-mapping-chart" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="sql-to-mongodb-mapping-chart">
</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="#terminology-and-concepts" target="_self">
<span>
Terminology and Concepts
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#executables" target="_self">
<span>
Executables
</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="#further-reading" target="_self">
<span>
Further Reading
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
In addition to the charts that follow, you might want to consider the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/faq/">
Frequently Asked Questions
</a>
section for a selection of common questions about MongoDB.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Terminology and Concepts
<a class="headerlink leafygreen-ui-1kepx47" href="#terminology-and-concepts" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="terminology-and-concepts">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following table presents the various SQL terminology and concepts
and the corresponding MongoDB terminology and concepts.
</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>
SQL Terms/Concepts
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB Terms/Concepts
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
database
</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-database">
database
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
table
</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-collection">
collection
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
row
</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-document">
document
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-BSON">
BSON
</a>
document
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
column
</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-field">
field
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-index">
index
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
table joins
</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/lookup/#mongodb-pipeline-pipe.-lookup">
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</a>
, embedded documents
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
primary key
</p>
<p class="leafygreen-ui-1kp3ins">
Specify any unique column or column combination as primary key.
</p>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary-key">
primary key
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
In MongoDB, the primary key is automatically set to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-_id">
_id
</a>
field.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
aggregation (e.g. group by)
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
aggregation pipeline
</p>
<p class="leafygreen-ui-1kp3ins">
See the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/sql-aggregation-comparison/">
SQL to Aggregation Mapping Chart
<!-- -->
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
SELECT INTO NEW_TABLE
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/out/#mongodb-pipeline-pipe.-out">
<code class="leafygreen-ui-1l06pbn">
$out
</code>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
See the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/sql-aggregation-comparison/">
SQL to Aggregation Mapping Chart
<!-- -->
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
MERGE INTO TABLE
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
See the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/sql-aggregation-comparison/">
SQL to Aggregation Mapping Chart
<!-- -->
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
UNION ALL
</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/unionWith/#mongodb-pipeline-pipe.-unionWith">
<code class="leafygreen-ui-1l06pbn">
$unionWith
</code>
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
transactions
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
transactions
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
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
instead of multi-document transactions. That is, for many
scenarios, modeling your data appropriately will minimize the
need for multi-document transactions.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Executables
<a class="headerlink leafygreen-ui-1kepx47" href="#executables" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="executables">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following table presents some database executables and the
corresponding MongoDB executables. This table is
<em>
not
</em>
meant to be
exhaustive.
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:30%"/>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:20%"/>
<col style="width:20%"/>
<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>
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MySQL
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Oracle
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Informix
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
DB2
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Database Server
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
mysqld
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
oracle
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
IDS
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
DB2 Server
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Database Client
</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/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
mysql
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
sqlplus
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
DB-Access
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
DB2 Client
</code>
</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 table presents the various SQL statements and the
corresponding MongoDB statements. The examples in the table assume the
following conditions:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The SQL examples assume a table named
<code class="leafygreen-ui-1l06pbn">
people
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The MongoDB examples assume a collection named
<code class="leafygreen-ui-1l06pbn">
people
</code>
that contain
documents of the following prototype:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n _id: ObjectId(\"509a8fb2f3f4948bd2f983a0\"),\n user_id: \"abc123\",\n age: 55,\n status: 'A'\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">"509a8fb2f3f4948bd2f983a0"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">user_id</span>: <span class="lg-highlight-string">"abc123"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">55</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">'A'</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>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Create and Alter
<a class="headerlink leafygreen-ui-1kepx47" href="#create-and-alter" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-and-alter">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following table presents the various SQL statements related to
table-level actions and the corresponding MongoDB statements.
</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>
SQL Schema Statements
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB Schema Statements
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<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":"CREATE TABLE people (\n id MEDIUMINT NOT NULL\n AUTO_INCREMENT,\n user_id Varchar(30),\n age Number,\n status char(1),\n PRIMARY KEY (id)\n)","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">CREATE</span> <span class="lg-highlight-keyword">TABLE</span> people (</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> id MEDIUMINT <span class="lg-highlight-keyword">NOT</span> <span class="lg-highlight-keyword">NULL</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> AUTO_INCREMENT,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> user_id <span class="lg-highlight-type">Varchar</span>(<span class="lg-highlight-number">30</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> age Number,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> status <span class="lg-highlight-type">char</span>(<span class="lg-highlight-number">1</span>),</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">PRIMARY</span> KEY<!-- --> (id)</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>
<p class="leafygreen-ui-1kp3ins">
Implicitly created on first
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.insertOne/#mongodb-method-db.collection.insertOne">
<code class="leafygreen-ui-1l06pbn">
insertOne()
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.insertMany/#mongodb-method-db.collection.insertMany">
<code class="leafygreen-ui-1l06pbn">
insertMany()
</code>
</a>
operation. The primary key
<code class="leafygreen-ui-1l06pbn">
_id
</code>
is automatically added if
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field is not specified.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.people.insertOne( {\n user_id: \"abc123\",\n age: 55,\n status: \"A\"\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="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">people</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">user_id</span>: <span class="lg-highlight-string">"abc123"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">55</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> } )</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
However, you can also explicitly create a collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.createCollection(\"people\")","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">createCollection</span>(<span class="lg-highlight-string">"people"</span>)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"ALTER TABLE people\nADD join_date DATETIME","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">ALTER</span> <span class="lg-highlight-keyword">TABLE</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">ADD</span> join_date DATETIME</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Collections do not describe or enforce the structure of its
documents; i.e. there is no structural alteration at the
collection level.
</p>
<p class="leafygreen-ui-1kp3ins">
However, at the document level,
<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>
operations can add fields to existing documents using 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>
operator.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.people.updateMany(\n { },\n { $set: { join_date: new Date() } }\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">db.<span class="lg-highlight-property">people</span>.<span class="lg-highlight-title lg-highlight-function">updateMany</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">$set</span>: <!-- -->{ <span class="lg-highlight-attr">join_date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<!-- -->) } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"ALTER TABLE people\nDROP COLUMN join_date","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">ALTER</span> <span class="lg-highlight-keyword">TABLE</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">DROP</span> <span class="lg-highlight-keyword">COLUMN</span> join_date</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Collections do not describe or enforce the structure of its
documents; i.e. there is no structural alteration at the collection
level.
</p>
<p class="leafygreen-ui-1kp3ins">
However, at the document level,
<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>
operations can remove fields from documents using 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.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.people.updateMany(\n { },\n { $unset: { \"join_date\": \"\" } }\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">db.<span class="lg-highlight-property">people</span>.<span class="lg-highlight-title lg-highlight-function">updateMany</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">$unset</span>: <!-- -->{ <span class="lg-highlight-string">"join_date"</span>: <span class="lg-highlight-string">""</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"CREATE INDEX idx_user_id_asc\nON people(user_id)","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">CREATE</span> INDEX idx_user_id_asc</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">ON</span> people(user_id)</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":"db.people.createIndex( { user_id: 1 } )","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">people</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <span class="lg-highlight-attr">user_id</span>: <span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"CREATE INDEX\n idx_user_id_asc_age_desc\nON people(user_id, age 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">CREATE</span> INDEX</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> idx_user_id_asc_age_desc</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">ON</span> people(user_id, age <span class="lg-highlight-keyword">DESC</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":"db.people.createIndex( { user_id: 1, age: -1 } )","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">people</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <span class="lg-highlight-attr">user_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">age</span>: <!-- -->-<span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"DROP TABLE people","programmingLanguage":"SQL"}
</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 sql leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">DROP</span> <span class="lg-highlight-keyword">TABLE</span> people</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":"db.people.drop()","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">people</span>.<span class="lg-highlight-title lg-highlight-function">drop</span>(<!-- -->)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on the methods and operators used, 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/reference/method/db.collection.insertOne/#mongodb-method-db.collection.insertOne">
<code class="leafygreen-ui-1l06pbn">
db.collection.insertOne()
</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.insertMany/#mongodb-method-db.collection.insertMany">
<code class="leafygreen-ui-1l06pbn">
db.collection.insertMany()
</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.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.createIndex/#mongodb-method-db.collection.createIndex">
<code class="leafygreen-ui-1l06pbn">
db.collection.createIndex()
</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.drop/#mongodb-method-db.collection.drop">
<code class="leafygreen-ui-1l06pbn">
db.collection.drop()
</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>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/unset/#mongodb-update-up.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
</p>
</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/core/databases-and-collections/#std-label-databases-and-collections">
Databases and Collections in MongoDB
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/document/#std-label-bson-document-format">
Documents
</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/data-modeling/concepts/#std-label-data-modeling-concepts">
Data Modeling Concepts
<!-- -->
.
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Insert
<a class="headerlink leafygreen-ui-1kepx47" href="#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="insert">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following table presents the various SQL statements related to
inserting records into tables and the corresponding MongoDB statements.
</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>
SQL INSERT Statements
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB insertOne() Statements
</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":"INSERT INTO people(user_id,\n age,\n status)\nVALUES (\"bcd001\",\n 45,\n \"A\")","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">INSERT</span> <span class="lg-highlight-keyword">INTO</span> people(user_id,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> age,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> status)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">VALUES</span> (<!-- -->"bcd001"<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-number">45</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->"A"<!-- -->)</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":"db.people.insertOne(\n { user_id: \"bcd001\", age: 45, status: \"A\" }\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">db.<span class="lg-highlight-property">people</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>(</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">user_id</span>: <span class="lg-highlight-string">"bcd001"</span>, <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">45</span>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</span> }</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>
<p class="leafygreen-ui-1kp3ins">
For more information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.insertOne/#mongodb-method-db.collection.insertOne">
<code class="leafygreen-ui-1l06pbn">
db.collection.insertOne()
</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/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/reference/method/db.collection.insertMany/#mongodb-method-db.collection.insertMany">
<code class="leafygreen-ui-1l06pbn">
db.collection.insertMany()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/databases-and-collections/#std-label-databases-and-collections">
Databases and Collections in MongoDB
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/document/#std-label-bson-document-format">
Documents
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Select
<a class="headerlink leafygreen-ui-1kepx47" href="#select" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following table presents the various SQL statements related to
reading records from tables and the corresponding MongoDB statements.
</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/db.collection.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</a>
method always includes the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field in the returned documents unless specifically excluded through
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/project-fields-from-query-results/#std-label-projection">
projection
</a>
. Some of the SQL queries below may include an
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field to reflect this, even if the field is not included in the
corresponding
<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>
query.
</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>
SQL SELECT Statements
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB find() Statements
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT *\nFROM people","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> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</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":"db.people.find()","programmingLanguage":"JavaScript"}
</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 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">find</span>(<!-- -->)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT id,\n user_id,\n status\nFROM people","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> id,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> user_id,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> status</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</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":"db.people.find(\n { },\n { user_id: 1, status: 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">db.<span class="lg-highlight-property">people</span>.<span class="lg-highlight-title lg-highlight-function">find</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">user_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">status</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>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT user_id, status\nFROM people","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> user_id, status</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</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":"db.people.find(\n { },\n { user_id: 1, status: 1, _id: 0 }\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">db.<span class="lg-highlight-property">people</span>.<span class="lg-highlight-title lg-highlight-function">find</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">user_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-number">1</span>, <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></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT *\nFROM people\nWHERE status = \"A\"","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> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> status <span class="lg-highlight-operator">=</span> <!-- -->"A"</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":"db.people.find(\n { status: \"A\" }\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">db.<span class="lg-highlight-property">people</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT user_id, status\nFROM people\nWHERE status = \"A\"","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> user_id, status</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> status <span class="lg-highlight-operator">=</span> <!-- -->"A"</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":"db.people.find(\n { status: \"A\" },\n { user_id: 1, status: 1, _id: 0 }\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">db.<span class="lg-highlight-property">people</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">status</span>: <span class="lg-highlight-string">"A"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">user_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-number">1</span>, <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></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT *\nFROM people\nWHERE status != \"A\"","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> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> status <span class="lg-highlight-operator">!=</span> <!-- -->"A"</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":"db.people.find(\n { status: { $ne: \"A\" } }\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">db.<span class="lg-highlight-property">people</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">status</span>: <!-- -->{ <span class="lg-highlight-attr">$ne</span>: <span class="lg-highlight-string">"A"</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT *\nFROM people\nWHERE status = \"A\"\nAND age = 50","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> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> status <span class="lg-highlight-operator">=</span> <!-- -->"A"</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">AND</span> age <span class="lg-highlight-operator">=</span> <span class="lg-highlight-number">50</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":"db.people.find(\n { status: \"A\",\n age: 50 }\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">db.<span class="lg-highlight-property">people</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">status</span>: <span class="lg-highlight-string">"A"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">50</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT *\nFROM people\nWHERE status = \"A\"\nOR age = 50","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> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> status <span class="lg-highlight-operator">=</span> <!-- -->"A"</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">OR</span> age <span class="lg-highlight-operator">=</span> <span class="lg-highlight-number">50</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":"db.people.find(\n { $or: [ { status: \"A\" } , { age: 50 } ] }\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">db.<span class="lg-highlight-property">people</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">$or</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</span> } <!-- -->, <!-- -->{ <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">50</span> } ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT *\nFROM people\nWHERE age > 25","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> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> age <span class="lg-highlight-operator">></span> <span class="lg-highlight-number">25</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":"db.people.find(\n { age: { $gt: 25 } }\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">db.<span class="lg-highlight-property">people</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">age</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">25</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT *\nFROM people\nWHERE age < 25","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> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> age <span class="lg-highlight-operator"><</span> <span class="lg-highlight-number">25</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":"db.people.find(\n { age: { $lt: 25 } }\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">db.<span class="lg-highlight-property">people</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">age</span>: <!-- -->{ <span class="lg-highlight-attr">$lt</span>: <span class="lg-highlight-number">25</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT *\nFROM people\nWHERE age > 25\nAND age <= 50","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> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> age <span class="lg-highlight-operator">></span> <span class="lg-highlight-number">25</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">AND</span> age <span class="lg-highlight-operator"><=</span> <span class="lg-highlight-number">50</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":"db.people.find(\n { age: { $gt: 25, $lte: 50 } }\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">db.<span class="lg-highlight-property">people</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">age</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">25</span>, <span class="lg-highlight-attr">$lte</span>: <span class="lg-highlight-number">50</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT *\nFROM people\nWHERE user_id like \"%bc%\"","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> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> user_id <span class="lg-highlight-keyword">like</span> <!-- -->"%bc%"</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":"db.people.find( { user_id: /bc/ } )","programmingLanguage":"JavaScript"}
</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 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">find</span>( <!-- -->{ <span class="lg-highlight-attr">user_id</span>: <span class="lg-highlight-regexp">/bc/</span> <!-- -->} )</td></tr></tbody></table></code></pre>
</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":"db.people.find( { user_id: { $regex: /bc/ } } )","programmingLanguage":"JavaScript"}
</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 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">find</span>( <!-- -->{ <span class="lg-highlight-attr">user_id</span>: <!-- -->{ <span class="lg-highlight-attr">$regex</span>: <span class="lg-highlight-regexp">/bc/</span> <!-- -->} } )</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT *\nFROM people\nWHERE user_id like \"bc%\"","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> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> user_id <span class="lg-highlight-keyword">like</span> <!-- -->"bc%"</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":"db.people.find( { user_id: /^bc/ } )","programmingLanguage":"JavaScript"}
</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 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">find</span>( <!-- -->{ <span class="lg-highlight-attr">user_id</span>: <span class="lg-highlight-regexp">/^bc/</span> <!-- -->} )</td></tr></tbody></table></code></pre>
</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":"db.people.find( { user_id: { $regex: /^bc/ } } )","programmingLanguage":"JavaScript"}
</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 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">find</span>( <!-- -->{ <span class="lg-highlight-attr">user_id</span>: <!-- -->{ <span class="lg-highlight-attr">$regex</span>: <span class="lg-highlight-regexp">/^bc/</span> <!-- -->} } )</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT *\nFROM people\nWHERE status = \"A\"\nORDER BY user_id ASC","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> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> status <span class="lg-highlight-operator">=</span> <!-- -->"A"</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">ORDER</span> <span class="lg-highlight-keyword">BY</span> user_id <span class="lg-highlight-keyword">ASC</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":"db.people.find( { status: \"A\" } ).sort( { user_id: 1 } )","programmingLanguage":"JavaScript"}
</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 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">find</span>( <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</span> } ).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">user_id</span>: <span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT *\nFROM people\nWHERE status = \"A\"\nORDER BY user_id 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> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> status <span class="lg-highlight-operator">=</span> <!-- -->"A"</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">ORDER</span> <span class="lg-highlight-keyword">BY</span> user_id <span class="lg-highlight-keyword">DESC</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":"db.people.find( { status: \"A\" } ).sort( { user_id: -1 } )","programmingLanguage":"JavaScript"}
</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 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">find</span>( <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</span> } ).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">user_id</span>: <!-- -->-<span class="lg-highlight-number">1</span> } )</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT COUNT(*)\nFROM people","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> <span class="lg-highlight-built_in">COUNT</span>(<span class="lg-highlight-operator">*</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</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":"db.people.count()","programmingLanguage":"JavaScript"}
</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 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">count</span>(<!-- -->)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<em>
or
</em>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.people.find().count()","programmingLanguage":"JavaScript"}
</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 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">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">count</span>(<!-- -->)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT COUNT(user_id)\nFROM people","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> <span class="lg-highlight-built_in">COUNT</span>(<!-- -->user_id)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</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":"db.people.count( { user_id: { $exists: true } } )","programmingLanguage":"JavaScript"}
</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 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">count</span>( <!-- -->{ <span class="lg-highlight-attr">user_id</span>: <!-- -->{ <span class="lg-highlight-attr">$exists</span>: <span class="lg-highlight-literal">true</span> } } )</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<em>
or
</em>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.people.find( { user_id: { $exists: true } } ).count()","programmingLanguage":"JavaScript"}
</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 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">find</span>( <!-- -->{ <span class="lg-highlight-attr">user_id</span>: <!-- -->{ <span class="lg-highlight-attr">$exists</span>: <span class="lg-highlight-literal">true</span> } } ).<span class="lg-highlight-title lg-highlight-function">count</span>(<!-- -->)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT COUNT(*)\nFROM people\nWHERE age > 30","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> <span class="lg-highlight-built_in">COUNT</span>(<span class="lg-highlight-operator">*</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> age <span class="lg-highlight-operator">></span> <span class="lg-highlight-number">30</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":"db.people.count( { age: { $gt: 30 } } )","programmingLanguage":"JavaScript"}
</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 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">count</span>( <!-- -->{ <span class="lg-highlight-attr">age</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">30</span> } } )</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<em>
or
</em>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.people.find( { age: { $gt: 30 } } ).count()","programmingLanguage":"JavaScript"}
</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 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">find</span>( <!-- -->{ <span class="lg-highlight-attr">age</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">30</span> } } ).<span class="lg-highlight-title lg-highlight-function">count</span>(<!-- -->)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT DISTINCT(status)\nFROM people","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> <span class="lg-highlight-keyword">DISTINCT</span>(status)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</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":"db.people.aggregate( [ { $group : { _id : \"$status\" } } ] )","programmingLanguage":"JavaScript"}
</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 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>( <!-- -->[ <!-- -->{ <!-- -->$group<!-- --> <!-- -->: <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-string">"$status"</span> } } ] )</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
or, for distinct value sets that do not exceed the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/limits/#std-label-limit-bson-document-size">
BSON size limit
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.people.distinct( \"status\" )","programmingLanguage":"JavaScript"}
</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 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">distinct</span>( <span class="lg-highlight-string">"status"</span> )</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT *\nFROM people\nLIMIT 1","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> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx">LIMIT <span class="lg-highlight-number">1</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":"db.people.findOne()","programmingLanguage":"JavaScript"}
</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 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">findOne</span>(<!-- -->)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<em>
or
</em>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.people.find().limit(1)","programmingLanguage":"JavaScript"}
</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 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">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">limit</span>(<span class="lg-highlight-number">1</span>)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"SELECT *\nFROM people\nLIMIT 5\nSKIP 10","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> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx">LIMIT <span class="lg-highlight-number">5</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">SKIP</span> <span class="lg-highlight-number">10</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":"db.people.find().limit(5).skip(10)","programmingLanguage":"JavaScript"}
</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 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">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">limit</span>(<span class="lg-highlight-number">5</span>).<span class="lg-highlight-title lg-highlight-function">skip</span>(<span class="lg-highlight-number">10</span>)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"EXPLAIN SELECT *\nFROM people\nWHERE status = \"A\"","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">EXPLAIN <span class="lg-highlight-keyword">SELECT</span> <span class="lg-highlight-operator">*</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> status <span class="lg-highlight-operator">=</span> <!-- -->"A"</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":"db.people.find( { status: \"A\" } ).explain()","programmingLanguage":"JavaScript"}
</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 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">find</span>( <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</span> } ).<span class="lg-highlight-title lg-highlight-function">explain</span>(<!-- -->)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on the methods and operators used, 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/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.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.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/cursor.limit/#mongodb-method-cursor.limit">
<code class="leafygreen-ui-1l06pbn">
limit()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.skip/#mongodb-method-cursor.skip">
<code class="leafygreen-ui-1l06pbn">
skip()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.explain/#mongodb-method-cursor.explain">
<code class="leafygreen-ui-1l06pbn">
explain()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.sort/#mongodb-method-cursor.sort">
<code class="leafygreen-ui-1l06pbn">
sort()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.count/#mongodb-method-cursor.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/operator/query/ne/#mongodb-query-op.-ne">
<code class="leafygreen-ui-1l06pbn">
$ne
</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/and/#mongodb-query-op.-and">
<code class="leafygreen-ui-1l06pbn">
$and
</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/or/#mongodb-query-op.-or">
<code class="leafygreen-ui-1l06pbn">
$or
</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/gt/#mongodb-query-op.-gt">
<code class="leafygreen-ui-1l06pbn">
$gt
</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/lt/#mongodb-query-op.-lt">
<code class="leafygreen-ui-1l06pbn">
$lt
</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/exists/#mongodb-query-op.-exists">
<code class="leafygreen-ui-1l06pbn">
$exists
</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/lte/#mongodb-query-op.-lte">
<code class="leafygreen-ui-1l06pbn">
$lte
</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/regex/#mongodb-query-op.-regex">
<code class="leafygreen-ui-1l06pbn">
$regex
</code>
</a>
</p>
</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/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/reference/operator/query/">
Query and Projection Operators
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/">
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
Methods
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Update Records
<a class="headerlink leafygreen-ui-1kepx47" href="#update-records" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-records">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following table presents the various SQL statements related to
updating existing records in tables and the corresponding MongoDB
statements.
</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>
SQL Update Statements
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB updateMany() Statements
</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":"UPDATE people\nSET status = \"C\"\nWHERE age > 25","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">UPDATE</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">SET</span> status <span class="lg-highlight-operator">=</span> <!-- -->"C"</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> age <span class="lg-highlight-operator">></span> <span class="lg-highlight-number">25</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":"db.people.updateMany(\n { age: { $gt: 25 } },\n { $set: { status: \"C\" } }\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">db.<span class="lg-highlight-property">people</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">age</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">25</span> } }<!-- -->,</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">"C"</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</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":"UPDATE people\nSET age = age + 3\nWHERE status = \"A\"","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">UPDATE</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">SET</span> age <span class="lg-highlight-operator">=</span> age <span class="lg-highlight-operator">+</span> <span class="lg-highlight-number">3</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> status <span class="lg-highlight-operator">=</span> <!-- -->"A"</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":"db.people.updateMany(\n { status: \"A\" } ,\n { $inc: { age: 3 } }\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">db.<span class="lg-highlight-property">people</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">status</span>: <span class="lg-highlight-string">"A"</span> } <!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$inc</span>: <!-- -->{ <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">3</span> } }</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>
<p class="leafygreen-ui-1kp3ins">
For more information on the method and operators used in the examples, 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/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/operator/query/gt/#mongodb-query-op.-gt">
<code class="leafygreen-ui-1l06pbn">
$gt
</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>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/inc/#mongodb-update-up.-inc">
<code class="leafygreen-ui-1l06pbn">
$inc
</code>
</a>
</p>
</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/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/reference/operator/update/">
Update Operators
</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.updateOne/#mongodb-method-db.collection.updateOne">
<code class="leafygreen-ui-1l06pbn">
db.collection.updateOne()
</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.replaceOne/#mongodb-method-db.collection.replaceOne">
<code class="leafygreen-ui-1l06pbn">
db.collection.replaceOne()
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Delete Records
<a class="headerlink leafygreen-ui-1kepx47" href="#delete-records" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="delete-records">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following table presents the various SQL statements related to
deleting records from tables and the corresponding MongoDB statements.
</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>
SQL Delete Statements
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
MongoDB deleteMany() Statements
</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":"DELETE FROM people\nWHERE status = \"D\"","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">DELETE</span> <span class="lg-highlight-keyword">FROM</span> people</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">WHERE</span> status <span class="lg-highlight-operator">=</span> <!-- -->"D"</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":"db.people.deleteMany( { status: \"D\" } )","programmingLanguage":"JavaScript"}
</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 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">deleteMany</span>( <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"D"</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":"DELETE FROM people","programmingLanguage":"SQL"}
</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 sql leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">DELETE</span> <span class="lg-highlight-keyword">FROM</span> people</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":"db.people.deleteMany({})","programmingLanguage":"JavaScript"}
</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 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">deleteMany</span>(<!-- -->{<!-- -->})</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information, see
<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>
</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/tutorial/remove-documents/">
Delete Documents
</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.deleteOne/#mongodb-method-db.collection.deleteOne">
<code class="leafygreen-ui-1l06pbn">
db.collection.deleteOne()
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Further Reading
<a class="headerlink leafygreen-ui-1kepx47" href="#further-reading" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="further-reading">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
If you are considering migrating your SQL application to MongoDB,
download the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/modernize?tck=docs_server" target="_self">
<span>
MongoDB Application Modernization Guide
</span>
</a>
.
</p>
<p class="leafygreen-ui-1kp3ins">
The download includes the following resources:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Presentation on the methodology of data modeling with MongoDB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
White paper covering best practices and considerations for
migrating to MongoDB from an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-RDBMS">
RDBMS
</a>
data model
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Reference MongoDB schema with its RDBMS equivalent
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Application Modernization scorecard
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/PlanCache.help/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
PlanCache.help()
<a class="headerlink leafygreen-ui-1kepx47" href="#plancache.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="plancache.help--">
</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">
PlanCache.help()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-PlanCache.help" 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.help">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Displays the methods available to view and modify a collection's
query plan cache.
</p>
<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().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">getPlanCache</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>
</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/db.collection.getPlanCache/#mongodb-method-db.collection.getPlanCache">
<code class="leafygreen-ui-1l06pbn">
db.collection.getPlanCache()
</code>
</a>
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/build-indexes-on-replica-sets/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Rolling Index Builds on Replica Sets
<a class="headerlink leafygreen-ui-1kepx47" href="#rolling-index-builds-on-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="rolling-index-builds-on-replica-sets">
</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="#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>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
Index builds can impact replica set performance. By default,
MongoDB builds indexes simultaneously on all data-bearing
replica set members. For workloads which cannot tolerate performance
decrease due to index builds, consider using the following
procedure to build indexes in a rolling fashion.
</p>
<p class="leafygreen-ui-1kp3ins">
Rolling index builds take at most one replica set member out at a time,
starting with the secondary members, and builds the index on that member
as a standalone. Rolling index builds require at least one replica set
election.
</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 information about creating indexes in Atlas, refer to the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-ui/indexes/" target="_self">
<span>
index management
</span>
</a>
page in the Atlas
documentation.
</p>
</div>
</div>
<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">
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>
</h3>
<p class="leafygreen-ui-1kp3ins">
To create
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-unique/#std-label-index-type-unique">
unique indexes
</a>
using the following
procedure, you must stop all writes to the collection during this
procedure.
</p>
<p class="leafygreen-ui-1kp3ins">
If you cannot stop all writes to the collection during this procedure,
do not use the procedure on this page. Instead, build your unique index
on the collection by issuing
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.createIndex/#mongodb-method-db.collection.createIndex">
<code class="leafygreen-ui-1l06pbn">
db.collection.createIndex()
</code>
</a>
on
the primary for a replica set.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Oplog Size
<a class="headerlink leafygreen-ui-1kepx47" href="#oplog-size" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="oplog-size">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Ensure that your
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-oplog">
oplog
</a>
is large enough to permit the indexing
or re-indexing operation to complete without falling too far behind to
catch up. See the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-oplog/#std-label-replica-set-oplog-sizing">
oplog sizing
</a>
documentation for additional information.
</p>
</section>
</section>
<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>
<dl>
<dt>
For building unique indexes
</dt>
<dd class="css-1ufxuu7">
<p class="leafygreen-ui-1kp3ins">
To create
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-unique/#std-label-index-type-unique">
unique indexes
</a>
using the
following procedure, you must stop all writes to the collection
during the index build. Otherwise, you may end up with inconsistent
data across the replica set members.
</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">
If you cannot stop all writes to the collection, do not use the
following procedure to create unique indexes.
</p>
</div>
</div>
</dd>
</dl>
</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>
<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 to build indexes in a rolling fashion
applies to replica set deployments, and not sharded clusters. For
the procedure for sharded clusters, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/build-indexes-on-sharded-clusters/">
Rolling Index Builds on Sharded Clusters
</a>
instead.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-tutorial-index-on-replica-sets-stop-one-member">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
A. Stop One Secondary and Restart as a Standalone
<a class="headerlink leafygreen-ui-1kepx47" href="#a.-stop-one-secondary-and-restart-as-a-standalone" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.-stop-one-secondary-and-restart-as-a-standalone">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Stop 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 associated with a secondary.
Restart after making the following configuration updates:
</p>
<div aria-hidden="true">
</div>
<div class="leafygreen-ui-1b75luy" data-lgid="lg-tabs">
<div class="leafygreen-ui-jp7pox" id="tabs-10">
<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="_scg5jrp56" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_ueb36dmzb" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Configuration File
</div>
</button>
<button aria-controls="_b3la9yj53" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_ro4bewg7q" 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="_08qp851hz" class="" id="_6t1u8dx6j" 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.
<a class="footnote-reference header-buffer" href="#footnote-different-port" id="ref-different-port-id1">
[
<!-- -->
1
<!-- -->
]
</a>
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>
</ul>
<p class="leafygreen-ui-1kp3ins">
For example, the updated configuration file for a replica
set member 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: 27217\n# port: 27017\n#replication:\n# replSetName: myRepl\nsetParameter:\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">27217</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment"># port: 27017</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: myRepl</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">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>
<p class="leafygreen-ui-1kp3ins">
Other settings (e.g.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-storage.dbPath">
<code class="leafygreen-ui-1l06pbn">
storage.dbPath
</code>
</a>
, etc.) remain the same.
</p>
<p class="leafygreen-ui-1kp3ins">
And restart:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --config <path>","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 --config <path/To/ConfigFile></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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="_fjt7xsyef" class="leafygreen-ui-38lglc" id="_tjylnhfy2" 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">
--replSet
</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.
<a class="footnote-reference header-buffer" href="#footnote-different-port" id="ref-different-port-id2">
[
<!-- -->
1
<!-- -->
]
</a>
</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>
</ul>
<p class="leafygreen-ui-1kp3ins">
For example, if your replica set member
<em>
normally
</em>
runs
with on the default port of
<code class="leafygreen-ui-1l06pbn">
27017
</code>
and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--replSet">
<code class="leafygreen-ui-1l06pbn">
--replSet
</code>
</a>
option, you would
specify a different port, omit the
<code class="leafygreen-ui-1l06pbn">
--replSet
</code>
option,
and set
<code class="leafygreen-ui-1l06pbn">
disableLogicalSessionCacheRefresh
</code>
parameter
to true:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --port 27217 --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 27217 --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">
Other settings (e.g.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--dbpath">
<code class="leafygreen-ui-1l06pbn">
--dbpath
</code>
</a>
, etc.) remain the same.
</p>
</div>
</div>
</div>
</div>
</div>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-different-port" 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-different-port-id1">
1
</a>
,
<a href="#ref-different-port-id2">
2
</a>
)
</em>
<!-- -->
By running the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
on a different
port, you ensure that the other members of the replica set and all
clients will not contact the member while you are building the
index.
</td>
</tr>
</tbody>
</table>
<span class="leafygreen-ui-1qnjd6d" id="std-label-tutorial-index-on-replica-sets-build-index">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
B. Build the Index
<a class="headerlink leafygreen-ui-1kepx47" href="#b.-build-the-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="b.-build-the-index">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Connect directly 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 running as a
standalone on the new port and create the new index for this
instance.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, 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 instance, and
use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.createIndex/#mongodb-method-db.collection.createIndex">
<code class="leafygreen-ui-1l06pbn">
createIndex()
</code>
</a>
to create an ascending
index on the
<code class="leafygreen-ui-1l06pbn">
username
</code>
field of the
<code class="leafygreen-ui-1l06pbn">
records
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.records.createIndex( { username: 1 } )","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">db.records.createIndex( { username: 1 } )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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-tutorial-index-on-replica-sets-restart-mongod">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
C. Restart the Program
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
as a Replica Set Member
<a class="headerlink leafygreen-ui-1kepx47" href="#c.-restart-the-program-mongod-as-a-replica-set-member" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="c.-restart-the-program-mongod-as-a-replica-set-member">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When the index build completes, shutdown 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. Undo the configuration changes made when starting as a
standalone to return the its original configuration and restart as
a member of the replica set.
</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">
Be sure to remove the
<code class="leafygreen-ui-1l06pbn">
disableLogicalSessionCacheRefresh
</code>
parameter.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For example, to restart your replica set member:
</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 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="_lhheg1cyg" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_ziu6qlbk5" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Configuration File
</div>
</button>
<button aria-controls="_ejly0dyon" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_mwmx3rcr1" 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="_cd59g1p0r" class="" id="_q22rzt270" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
If you are using a configuration file:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Revert to the original port number.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Uncomment 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>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Remove parameter
<code class="leafygreen-ui-1l06pbn">
disableLogicalSessionCacheRefresh
</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>
<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":"net:\n bindIp: localhost,<hostname(s)|ip>\n port: 27017\nreplication:\n replSetName: myRepl","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">27017</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">replication:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">replSetName:</span> <span class="lg-highlight-string">myRepl</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">
Other settings (e.g.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-storage.dbPath">
<code class="leafygreen-ui-1l06pbn">
storage.dbPath
</code>
</a>
, etc.) remain the same.
</p>
<p class="leafygreen-ui-1kp3ins">
And restart:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --config <path>","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 --config <path/To/ConfigFile></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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="_tl9a9yl2d" class="leafygreen-ui-38lglc" id="_kdgpu1jom" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
If you are using command-line options,
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Revert to the original port number
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Include the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--replSet">
<code class="leafygreen-ui-1l06pbn">
--replSet
</code>
</a>
option.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Remove parameter
<code class="leafygreen-ui-1l06pbn">
disableLogicalSessionCacheRefresh
</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":"mongod --port 27017 --replSet myRepl","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 27017 --replSet myRepl</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Other settings (e.g.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--dbpath">
<code class="leafygreen-ui-1l06pbn">
--dbpath
</code>
</a>
, etc.) remain the same.
</p>
</div>
</div>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Allow replication to catch up on this member.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
D. Repeat the Procedure for the Remaining Secondaries
<a class="headerlink leafygreen-ui-1kepx47" href="#d.-repeat-the-procedure-for-the-remaining-secondaries" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="d.-repeat-the-procedure-for-the-remaining-secondaries">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Once the member catches up with the other members of the set, repeat
the procedure one member at a time for the remaining secondary members:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-tutorial-index-on-replica-sets-stop-one-member" target="_self">
<span>
A. Stop One Secondary and Restart as a Standalone
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-tutorial-index-on-replica-sets-build-index" target="_self">
<span>
B. Build the Index
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-tutorial-index-on-replica-sets-restart-mongod" target="_self">
<span>
C. Restart the Program
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
as a Replica Set Member
</span>
</a>
</p>
</li>
</ol>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
E. Build the Index on the Primary
<a class="headerlink leafygreen-ui-1kepx47" href="#e.-build-the-index-on-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="e.-build-the-index-on-the-primary">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When all the secondaries have the new index, step down the primary,
restart it as a standalone using the procedure described above,
and build the index on the former primary:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use the
<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>
method 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 step down the primary. Upon successful stepdown, the current primary
becomes a secondary and the replica set members elect a new primary.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-tutorial-index-on-replica-sets-stop-one-member" target="_self">
<span>
A. Stop One Secondary and Restart as a Standalone
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-tutorial-index-on-replica-sets-build-index" target="_self">
<span>
B. Build the Index
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-tutorial-index-on-replica-sets-restart-mongod" target="_self">
<span>
C. Restart the Program
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
as a Replica Set Member
</span>
</a>
</p>
</li>
</ol>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/queryable-encryption/features/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Features
<a class="headerlink leafygreen-ui-1kepx47" href="#features" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="features">
</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="#queryable-encryption" target="_self">
<span>
Queryable Encryption
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#how-queryable-encryption-works" target="_self">
<span>
How Queryable Encryption Works
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#other-security-mechanisms" target="_self">
<span>
Other Security Mechanisms
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#role-based-access-control" target="_self">
<span>
Role-Based Access Control
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#encryption-at-rest" target="_self">
<span>
Encryption at Rest
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#transport-encryption--tls-ssl-" target="_self">
<span>
Transport Encryption (TLS/SSL)
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#comparison-of-features" target="_self">
<span>
Comparison of Features
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#scenario" target="_self">
<span>
Scenario
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#solution" target="_self">
<span>
Solution
</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">
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">
On this page, you can learn about the security benefits of Queryable Encryption,
how it works, and how it compares to other security mechanisms supported
by MongoDB. You can also view a fictional scenario that demonstrates the
value of Queryable Encryption in securing your data.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Queryable Encryption
<a class="headerlink leafygreen-ui-1kepx47" href="#queryable-encryption" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Queryable Encryption enables a client application to
encrypt data before transporting it over the network using fully
randomized encryption, while maintaining queryability.
Sensitive data is transparently encrypted and decrypted by the client
and only communicated to and from the server in encrypted form.
</p>
<p class="leafygreen-ui-1kp3ins">
Unlike
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/csfle/#std-label-manual-csfle-feature">
Client-Side Field Level Encryption
</a>
that can use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/csfle/fundamentals/encryption-algorithms/#std-label-csfle-deterministic-encryption">
Deterministic Encryption
</a>
,
Queryable Encryption uses fast, searchable encryption schemes based on structured encryption.
These schemes produce different encrypted output values even when given
the same cleartext input.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
How Queryable Encryption Works
<a class="headerlink leafygreen-ui-1kepx47" href="#how-queryable-encryption-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-queryable-encryption-works">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The diagram below shows the process and architecture of how Queryable Encryption is
used in a customer environment.
</p>
<div class="gatsby-image-wrapper gatsby-image-wrapper-constrained leafygreen-ui-12xaq6r" data-gatsby-image-wrapper="">
<div style="max-width:2704px;display:block">
<img alt="" aria-hidden="true" role="presentation" src="data:image/svg+xml;charset=utf-8,%3Csvg%20height='1300'%20width='2704'%20xmlns='http://www.w3.org/2000/svg'%20version='1.1'%3E%3C/svg%3E" style="max-width:100%;display:block;position:static"/>
</div>
<div aria-hidden="true" data-placeholder-image="" style="opacity:1;transition:opacity 500ms linear">
</div>
<img alt="How Queryable Encryption works" data-main-image="" decoding="async" height="1300" loading="lazy" sizes="(min-width: 2704px) 2704px, 100vw" src="/docs/manual/static/a84fca4060504fdb937ab811f896a726/ae069/QE-how-it-works.webp" srcset="/docs/manual/static/a84fca4060504fdb937ab811f896a726/5c32f/QE-how-it-works.webp 676w,/docs/manual/static/a84fca4060504fdb937ab811f896a726/c7a70/QE-how-it-works.webp 1352w,/docs/manual/static/a84fca4060504fdb937ab811f896a726/ae069/QE-how-it-works.webp 2704w" style="--border-color:#C1C7C6;opacity:0" width="2704"/>
<noscript>
<img alt="How Queryable Encryption works" data-gatsby-image-ssr="" data-main-image="" decoding="async" height="1300" loading="lazy" sizes="(min-width: 2704px) 2704px, 100vw" src="/docs/manual/static/a84fca4060504fdb937ab811f896a726/ae069/QE-how-it-works.webp" srcset="/docs/manual/static/a84fca4060504fdb937ab811f896a726/5c32f/QE-how-it-works.webp 676w,/docs/manual/static/a84fca4060504fdb937ab811f896a726/c7a70/QE-how-it-works.webp 1352w,/docs/manual/static/a84fca4060504fdb937ab811f896a726/ae069/QE-how-it-works.webp 2704w" style="--border-color:#C1C7C6;opacity:0" width="2704"/>
</noscript>
<script type="module">
const t="undefined"!=typeof HTMLImageElement&&"loading"in HTMLImageElement.prototype;if(t){const t=document.querySelectorAll("img[data-main-image]");for(let e of t){e.dataset.src&&(e.setAttribute("src",e.dataset.src),e.removeAttribute("data-src")),e.dataset.srcset&&(e.setAttribute("srcset",e.dataset.srcset),e.removeAttribute("data-srcset"));const t=e.parentNode.querySelectorAll("source[data-srcset]");for(let e of t)e.setAttribute("srcset",e.dataset.srcset),e.removeAttribute("data-srcset");e.complete&&(e.style.opacity=1,e.parentNode.parentNode.querySelector("[data-placeholder-image]").style.opacity=0)}}
</script>
</div>
<p class="leafygreen-ui-1kp3ins">
In this diagram, the user is able to query on fully randomly encrypted
data such as SSN number.
</p>
<p class="leafygreen-ui-1kp3ins">
The process and mechanisms that make this possible within Queryable Encryption are as follows:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
When the application submits the query, MongoDB drivers first analyze
the query.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The driver recognizes the query is against an encrypted field and
requests the encryption keys from the customer-provisioned key
provider such as:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
AWS Key Management Service (AWS KMS)
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Google Cloud KMS
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Azure Key Vault
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Any
<span class="leafygreen-ui-vm4wms" tabindex="0">
KMIP
</span>
-compliant key provider
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The driver submits the query to the MongoDB server with the encrypted
fields rendered as ciphertext.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Queryable Encryption implements a fast, searchable scheme that allows
the server to process queries on fully encrypted data, without knowing
anything about the data. The data and the query itself remain encrypted
at all times on the server.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The MongoDB server returns the encrypted results of the query to the
driver.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The query results are decrypted with the keys held by the driver and
returned to the client and shown as plaintext.
</p>
</li>
</ol>
<p class="leafygreen-ui-1kp3ins">
Queryable Encryption functions with the help of the following data structures. It is critical
that these are not modified or deleted, or query results will be incorrect.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Queryable Encryption adds a
<code class="leafygreen-ui-1l06pbn">
__safeContent__
</code>
field to documents in any collection where there's a Queryable Encryption
encrypted field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Queryable Encryption creates two internal metadata collections in the same database as the collection where there's a
Queryable Encryption encrypted field. These are named as follows:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
enxcol_.<collectionName>.esc
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
enxcol_.<collectionName>.ecoc
</code>
</p>
</li>
</ul>
</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">
Do not modify these data structures or query results will be incorrect
and security could be impacted.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Queryable Encryption keeps encrypted fields secure in the following scenarios:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Direct access to encrypted fields by a database superuser
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Access to encrypted fields by reading the server's memory
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Capture of encrypted fields over an insecure network
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Access to on-disk encrypted fields by reading database or backup files
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Frequency analysis attacks by identifying patterns in documents with encrypted fields
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
While all clients have access to the non-sensitive data fields,
only appropriately-configured Queryable Encryption clients are able to run
read and write queries using the encrypted data fields.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Remote Key Management System
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
When you use Queryable Encryption in production, you must use a remote
Key Management System (KMS) to store your encryption key.
</p>
<p class="leafygreen-ui-1kp3ins">
To view a step-by-step guide demonstrating how to use a remote KMS with
Queryable Encryption, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/tutorials/#std-label-qe-tutorial-automatic-encryption">
Tutorials
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To view a list of all supported KMS providers, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/kms-providers/#std-label-qe-fundamentals-kms-providers">
KMS Providers
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To learn more about why you should use a remote KMS, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/fundamentals/kms-providers/#std-label-qe-reasons-to-use-remote-kms">
Reasons to Use a Remote Key Management System
<!-- -->
.
</a>
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-qe-feature-comparison">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Other Security Mechanisms
<a class="headerlink leafygreen-ui-1kepx47" href="#other-security-mechanisms" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="other-security-mechanisms">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This section describes the following security mechanisms supported by
MongoDB and explains their use cases and limitations:
</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="#std-label-qe-features-role-based-access-control" target="_self">
<span>
Role-Based Access Control
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-qe-features-encryption-at-rest" target="_self">
<span>
Encryption at Rest
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-qe-features-transport-encryption" target="_self">
<span>
Transport Encryption (TLS/SSL)
</span>
</a>
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-qe-features-role-based-access-control">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Role-Based Access Control
<a class="headerlink leafygreen-ui-1kepx47" href="#role-based-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="role-based-access-control">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Role-Based Access Control is a security mechanism that
allows administrators to grant and restrict collection-level permissions
for users. With the appropriate role definition and assignment, this
solution prevents accidental disclosure of data and access.
</p>
<p class="leafygreen-ui-1kp3ins">
Role-Based Access control cannot protect against the following scenarios:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Capture of the data over an insecure network
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Access to on-disk data by reading database or backup files
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Access to data by reading the server's memory
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Direct access to data by a database superuser
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
To learn more, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/manual/core/authorization/" target="_self">
<span>
Role-Based Access Control
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-qe-features-encryption-at-rest">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Encryption at Rest
<a class="headerlink leafygreen-ui-1kepx47" href="#encryption-at-rest" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="encryption-at-rest">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Encryption at Rest is a mechanism that encrypts database files on disk.
This mechanism prevents a person who lacks database credentials, but has
access to the computer hosting your database, from viewing your data.
</p>
<p class="leafygreen-ui-1kp3ins">
This mechanism does not protect your data against the following scenarios:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Capture of the data over an insecure network
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Access to data by reading the server's memory
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Direct access to data by a database superuser
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
To learn more, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/manual/core/security-encryption-at-rest/" target="_self">
<span>
Encryption at Rest
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-qe-features-transport-encryption">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Transport Encryption (TLS/SSL)
<a class="headerlink leafygreen-ui-1kepx47" href="#transport-encryption--tls-ssl-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="transport-encryption--tls-ssl-">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Transport Encryption using TLS/SSL encrypts your data over the network.
TLS/SSL protects your data as it travels over an insecure network, but
cannot protect your data from a privileged user or as it sits on disk.
</p>
<p class="leafygreen-ui-1kp3ins">
To learn more, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/manual/core/security-transport-encryption/" target="_self">
<span>
Transport Encryption using TLS/SSL
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-encryption-features-comparison">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Comparison of Features
<a class="headerlink leafygreen-ui-1kepx47" href="#comparison-of-features" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-features">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To learn more about Client-Side Field Level Encryption, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/csfle/features/#std-label-csfle-features">
Client-Side Field Level Encryption Features
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The following table describes potential security threats and how
MongoDB encryption features address them. Use these mechanisms together:
Role-Based Access Control, Encryption at Rest, Transport Encryption, and
In-Use Encryption. Note that you can't use both Client-Side Field Level Encryption and Queryable Encryption in the
same collection.
</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 is a high-level summary meant for general comparison. For detailed
information, see the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://cdn.bfldr.com/2URK6TO/as/64kp46t53v34xw37gkngbrg/An_Overview_of_Queryable_Encryption" rel="noopener noreferrer">
<span>
Overview of Queryable 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>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://cdn.bfldr.com/2URK6TO/as/jkwp857q2zr8fj5vqs24f5/Design__Analysis_Stateless_Document_Database_Encryption_Scheme" rel="noopener noreferrer">
<span>
Design and Analysis of a Stateless
Document Database Encryption Scheme
</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>
whitepapers.
</p>
</div>
</div>
<div class="header-buffer" id="ref-db-exfil-id1">
</div>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:16%"/>
<col style="width:16%"/>
<col style="width:25%"/>
<col style="width:25%"/>
</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>
Threat
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
TLS/SSL Transport Encryption
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Encryption at Rest (EaR)
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Queryable Encryption (Equality) + TLS/SSL + EaR
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
CSFLE + TLS/SSL + EaR
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Network Snooping (attacker has access to network traffic)
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reveals
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-operation-metadata">
operation metadata
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reveals operation metadata
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reveals operation metadata
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reveals operation metadata
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Database Recoveries from Disk (attacker has physical disk access)
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reveals database
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reveals
<em>
size of database
</em>
and operation metadata
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reveals
<em>
size of database
</em>
and operation metadata
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reveals
<em>
size of database
</em>
and operation metadata
</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/glossary/#std-term-database-exfiltration">
Database exfiltration
</a>
from Disk and
Memory (attacker has physical disk access and multiple database
snapshots)
<a class="footnote-reference header-buffer" href="#footnote-db-exfil" id="ref-db-exfil-id1">
[
<!-- -->
1
<!-- -->
]
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reveals database
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reveals database
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reveals
<em>
size of database
</em>
and operation metadata
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reveals
<em>
frequency of values
</em>
and operation metadata
</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/glossary/#std-term-Advanced-Persistent-Threat">
Advanced Persistent Threat
</a>
(attacker has long-term, continuous
access to network, disk, and memory while remaining undetected)
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reveals database
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Reveals database
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Queryable Encryption is not designed to protect against an ATP. See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://cdn.bfldr.com/2URK6TO/as/64kp46t53v34xw37gkngbrg/An_Overview_of_Queryable_Encryption" rel="noopener noreferrer">
<span>
whitepaper
</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 details.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
CSFLE is not designed to protect against an ATP. See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://cdn.bfldr.com/2URK6TO/as/64kp46t53v34xw37gkngbrg/An_Overview_of_Queryable_Encryption" rel="noopener noreferrer">
<span>
whitepaper
</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 details.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-db-exfil" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<a href="#ref-db-exfil-id1">
1
</a>
]
</td>
<td class="leafygreen-ui-17r2xs0">
<!-- -->
This assumes exfiltration occurs between completed operations.
See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://cdn.bfldr.com/2URK6TO/as/64kp46t53v34xw37gkngbrg/An_Overview_of_Queryable_Encryption" rel="noopener noreferrer">
<span>
whitepaper
</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 details.
</td>
</tr>
</tbody>
</table>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Queryable Encryption defends against data exfiltration, not against adversaries with
persistent access to an environment, or those who can retrieve both
database snapshots and accompanying query transcripts/logs.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
When using Queryable Encryption, equality and range queries offer similar security against
attackers with database snapshots. However, an attacker with access to both
database snapshots and query information is beyond the scope of Queryable Encryption's
security guarantees. This is
<strong>
especially
</strong>
true for range queries, even if
only a small number of query transcripts or logs are retrieved. See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://cdn.bfldr.com/2URK6TO/as/64kp46t53v34xw37gkngbrg/An_Overview_of_Queryable_Encryption" rel="noopener noreferrer">
<span>
6.1:
Range Queries in the Persistent Model
</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>
in the overview whitepaper for details.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Scenario
<a class="headerlink leafygreen-ui-1kepx47" href="#scenario" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="scenario">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following fictional scenario demonstrates the value of Queryable Encryption
in securing your application's data, and how
Queryable Encryption interacts with the other security mechanism discussed
in this guide.
</p>
<p class="leafygreen-ui-1kp3ins">
In this scenario, we secure sensitive data on a medical care management
system that stores patients' personal information, billing
information, and medical records for a fictional company,
<em>
MedcoMD
</em>
.
None of the patient data is public, and specific data such as their
social security number (SSN, a US government-issued ID number), patient ID number,
billing information, and medication information are particularly sensitive and
subject to privacy compliance. It is important for the company and the patient
that the data is kept private and secure.
</p>
<p class="leafygreen-ui-1kp3ins">
MedcoMD needs this system to satisfy the following use cases:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Doctors use the system to access patients' medical records, billing
information, and update medications.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Receptionists use the system to verify patients' identities
using their contact information.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Receptionists can view a patient's billing information, but not
their patient ID number.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Receptionists cannot access a patient's medical records.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
MedcoMD is also concerned with the disclosure of sensitive data through any
of the following methods:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Accidental disclosure of data on a receptionist's publicly-viewable
screen.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Direct access to the database by a superuser such as a database
administrator.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Capture of data over an insecure network.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Access to data by reading the database server's memory.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Access to data by reading database or backup files.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
What can MedcoMD do to balance the functionality and access restrictions
of their medical care management system?
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Solution
<a class="headerlink leafygreen-ui-1kepx47" href="#solution" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="solution">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MedcoMD uses the following security mechanisms to satisfy their use cases
and protect against the disclosure of sensitive medical data:
</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="#std-label-qe-features-transport-encryption" target="_self">
<span>
Transport Encryption (TLS/SSL)
</span>
</a>
to secure data as it travels over the network.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-qe-features-encryption-at-rest" target="_self">
<span>
Encryption at Rest
</span>
</a>
to protect against disclosure of data by reading database
or backup files.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-qe-features-role-based-access-control" target="_self">
<span>
Role-Based Access Control
</span>
</a>
to limit the access of database users to the collections necessary for them
to perform their tasks.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Encrypting sensitive fields with Queryable Encryption to satisfy the following
use cases and constraints:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Prevent reading data from server memory as the
Queryable Encryption encrypted data is never on the database server in
an unencrypted form.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Allow receptionists to verify patients' identities and
prevent accidental disclosure of sensitive data on a receptionist's
publicly viewable screen by providing receptionists with a client
that is not Queryable Encryption enabled.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Allow doctors to view sensitive data privately in their offices
by providing doctors with a Queryable Encryption enabled client.
</p>
</li>
</ul>
</li>
</ul>
</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>
<p class="leafygreen-ui-1kp3ins">
To view a list of security measures you should implement to protect
your MongoDB deployment, see the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/manual/administration/security-checklist/" target="_self">
<span>
Security Checklist
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To start using Queryable Encryption, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/quick-start/#std-label-qe-quick-start">
Quick Start
<!-- -->
.
</a>
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/administration/replica-set-deployment/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Self-Managed Replica Set Deployment Tutorials
<a class="headerlink leafygreen-ui-1kepx47" href="#self-managed-replica-set-deployment-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="self-managed-replica-set-deployment-tutorials">
</div>
</a>
</h1>
<p class="leafygreen-ui-1kp3ins">
The following tutorials provide information on deploying replica sets.
</p>
<dl>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/deploy-replica-set/#std-label-server-replica-set-deploy">
Deploy a Self-Managed Replica Set
</a>
</dt>
<dd class="css-1ufxuu7">
Configure a three-member replica set for production systems.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/deploy-replica-set-for-testing/#std-label-server-replica-set-deploy-test">
Deploy a Self-Managed Replica Set for Testing and Development
</a>
</dt>
<dd class="css-1ufxuu7">
Configure a three-member replica set for either development or
testing systems.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/deploy-geographically-distributed-replica-set/#std-label-server-replica-set-deploy-geo">
Deploy a Geographically Redundant Self-Managed Replica Set
</a>
</dt>
<dd class="css-1ufxuu7">
Create a geographically redundant replica set to protect against
location-centered availability limitations (e.g. network and power
interruptions).
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/add-replica-set-arbiter/#std-label-server-replica-set-deploy-arbiter">
Add an Arbiter to a Self-Managed Replica Set
</a>
</dt>
<dd class="css-1ufxuu7">
Add an arbiter to give a replica set an odd number of voting
members to prevent election ties.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/convert-standalone-to-replica-set/#std-label-server-replica-set-deploy-convert">
Convert a Standalone Self-Managed mongod to a Replica Set
</a>
</dt>
<dd class="css-1ufxuu7">
Convert an existing standalone
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
instance into a
three-member replica set.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/expand-replica-set/#std-label-server-replica-set-deploy-expand">
Add Members to a Self-Managed Replica Set
</a>
</dt>
<dd class="css-1ufxuu7">
Add a new member to an existing replica set.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/remove-replica-set-member/#std-label-server-replica-set-remove-member">
Remove Members from a Self-Managed Replica Set
</a>
</dt>
<dd class="css-1ufxuu7">
Remove a member from a replica set.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/replace-replica-set-member/#std-label-server-replica-set-replace-member">
Replace a Self-Managed Replica Set Member
</a>
</dt>
<dd class="css-1ufxuu7">
Update the replica set configuration when the hostname of a
member's corresponding
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
instance has changed.
</dd>
</dl>
</section>
|
https://www.mongodb.com/docs/manual/data-modeling/design-antipatterns/bloated-documents/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Bloated Documents
<a class="headerlink leafygreen-ui-1kepx47" href="#bloated-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="bloated-documents">
</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="#example" target="_self">
<span>
Example
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#join-collections-with--lookup" target="_self">
<span>
Join Collections with $lookup
</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">
Storing data fields that are related to each other but not accessed
together can create bloated documents that lead to excessive RAM and
bandwidth usage. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-working-set">
working set
</a>
, consisting of frequently accessed
data and indexes, is stored in the RAM allotment. When the working
set fits in RAM, MongoDB can query from memory instead of from disk,
which improves performance. However, if documents are too large, the
working set might not fit into RAM, causing performance to degrade as
MongoDB has to access data from disk.
</p>
<p class="leafygreen-ui-1kp3ins">
To prevent bloated documents, restructure your schema with smaller documents
and use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/data-modeling/concepts/embedding-vs-references/#std-label-data-modeling-referencing">
document references
</a>
to separate
fields that aren't returned together. This approach reduces the working set
size and improves performance.
</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">
Consider the following schema that contains book information used on a
bookstore website's main page. The main page only displays the book title,
author, and front cover image. You must click on the book to see additional
details.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n title: \"Tale of Two Cities\",\n author: \"Charles Dickens\",\n genre: \"Historical Fiction\",\n cover_image: \"<url>\",\n year: 1859,\n pages: 448,\n price: 15.99,\n description: \"A historical novel set during the French Revolution.\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">title</span>: <span class="lg-highlight-string">"Tale of Two Cities"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">author</span>: <span class="lg-highlight-string">"Charles Dickens"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">genre</span>: <span class="lg-highlight-string">"Historical Fiction"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cover_image</span>: <span class="lg-highlight-string">"<url>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">year</span>: <span class="lg-highlight-number">1859</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pages</span>: <span class="lg-highlight-number">448</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">15.99</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"A historical novel set during the French Revolution.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">}</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
In the current schema, to display the information for the website's
main page, all of the book information must be queried. To reduce document
size and streamline queries, you can split the large document into two
smaller collections.
</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">
In the following example, the book information is split into two
collections:
<code class="leafygreen-ui-1l06pbn">
mainBookInfo
</code>
and
<code class="leafygreen-ui-1l06pbn">
additionalBookDetails
</code>
.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
mainBookInfo
</code>
collection contains the information displayed on
the website's main page.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
additionalBookDetails
</code>
collection contains extra details revealed
after a user clicks on the book.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
mainBookInfo
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.mainBookInfo.insertOne(\n {\n _id: 1234,\n title: \"Tale of Two Cities\",\n author: \"Charles Dickens\",\n genre: \"Historical Fiction\",\n cover_image: \"<url>\"\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">mainBookInfo</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-attr">_id</span>: <span class="lg-highlight-number">1234</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">title</span>: <span class="lg-highlight-string">"Tale of Two Cities"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">author</span>: <span class="lg-highlight-string">"Charles Dickens"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">genre</span>: <span class="lg-highlight-string">"Historical Fiction"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cover_image</span>: <span class="lg-highlight-string">"<url>"</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">
additionalBookDetails
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.additionalBookDetails.insertOne(\n {\n title: \"Tale of Two Cities\",\n bookId: 1234,\n year: 1859,\n pages: 448,\n price: 15.99,\n description: \"A historical novel set during the French Revolution.\"\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">additionalBookDetails</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-attr">title</span>: <span class="lg-highlight-string">"Tale of Two Cities"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bookId</span>: <span class="lg-highlight-number">1234</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">year</span>: <span class="lg-highlight-number">1859</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pages</span>: <span class="lg-highlight-number">448</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">15.99</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">"A historical novel set during the French Revolution."</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 two collections are linked by the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field in the
<code class="leafygreen-ui-1l06pbn">
mainBookInfo
</code>
collection and the
<code class="leafygreen-ui-1l06pbn">
bookId
</code>
field in the
<code class="leafygreen-ui-1l06pbn">
additionalBookDetails
</code>
collection. On the home page, only the
<code class="leafygreen-ui-1l06pbn">
mainBookInfo
</code>
collection is
used to provide the necessary information. When a user selects a book to
learn more about, the website queries the
<code class="leafygreen-ui-1l06pbn">
additionalBookDetails
</code>
collection using the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field to match with the
<code class="leafygreen-ui-1l06pbn">
bookId
</code>
field.
</p>
<p class="leafygreen-ui-1kp3ins">
By splitting the information into two collections, you ensure that your
documents do not grow too large and exceed RAM allotment.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Join Collections with $lookup
<a class="headerlink leafygreen-ui-1kepx47" href="#join-collections-with--lookup" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="join-collections-with--lookup">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To join the data from the
<code class="leafygreen-ui-1l06pbn">
mainBookInfo
</code>
collection and the
<code class="leafygreen-ui-1l06pbn">
additionalBookDetails
</code>
collection, the application needs to perform a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup">
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</a>
operation.
</p>
<p class="leafygreen-ui-1kp3ins">
The following aggregation operation joins the
<code class="leafygreen-ui-1l06pbn">
mainBookInfo
</code>
and
<code class="leafygreen-ui-1l06pbn">
additionalBookDetails
</code>
collection from the previous example.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.mainBookInfo.aggregate( [\n {\n $lookup: {\n from: \"additionalBookDetails\",\n localField: \"_id\",\n foreignField: \"bookId\",\n as: \"details\"\n }\n },\n {\n $replaceRoot: {\n newRoot: { $mergeObjects: [ { $arrayElemAt: [ \"$details\", 0 ] }, \"$$ROOT\" ] }\n }\n },\n {\n $project: { details: 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-property">mainBookInfo</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">$lookup</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">from</span>: <span class="lg-highlight-string">"additionalBookDetails"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">localField</span>: <span class="lg-highlight-string">"_id"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">foreignField</span>: <span class="lg-highlight-string">"bookId"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">as</span>: <span class="lg-highlight-string">"details"</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">$replaceRoot</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">newRoot</span>: <!-- -->{ <span class="lg-highlight-attr">$mergeObjects</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$arrayElemAt</span>: <!-- -->[ <span class="lg-highlight-string">"$details"</span>, <span class="lg-highlight-number">0</span> ] }<!-- -->, <span class="lg-highlight-string">"$$ROOT"</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">$project</span>: <!-- -->{ <span class="lg-highlight-attr">details</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 operation returns the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: ObjectId('666b1235eda086b5e22dbcf1'),\n title: 'Tale of Two Cities',\n author: 'Charles Dickens',\n genre: 'Historical Fiction',\n cover_image: '<url>',\n bookId: 1234,\n year: 1859,\n pages: 448,\n price: 15.99,\n description: 'A historical novel set during the French Revolution.'\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-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">'666b1235eda086b5e22dbcf1'</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">title</span>: <span class="lg-highlight-string">'Tale of Two Cities'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">author</span>: <span class="lg-highlight-string">'Charles Dickens'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">genre</span>: <span class="lg-highlight-string">'Historical Fiction'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cover_image</span>: <span class="lg-highlight-string">'<url>'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">bookId</span>: <span class="lg-highlight-number">1234</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">year</span>: <span class="lg-highlight-number">1859</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pages</span>: <span class="lg-highlight-number">448</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">15.99</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">description</span>: <span class="lg-highlight-string">'A historical novel set during the French Revolution.'</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 this example, the
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
operation joins the
<code class="leafygreen-ui-1l06pbn">
mainBookInfo
</code>
collection with the
<code class="leafygreen-ui-1l06pbn">
additionalBookDetails
</code>
collection using the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
and
<code class="leafygreen-ui-1l06pbn">
bookId
</code>
fields. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/mergeObjects/#mongodb-expression-exp.-mergeObjects">
<code class="leafygreen-ui-1l06pbn">
$mergeObjects
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/replaceRoot/#mongodb-pipeline-pipe.-replaceRoot">
<code class="leafygreen-ui-1l06pbn">
$replaceRoot
</code>
</a>
operations merge the joined documents from
the
<code class="leafygreen-ui-1l06pbn">
mainBookInfo
</code>
and
<code class="leafygreen-ui-1l06pbn">
additionalBookDetails
</code>
collections.
</p>
</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/data-modeling/design-antipatterns/#std-label-schema-design-antipatterns">
Schema Design Anti-Patterns
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/data-modeling/concepts/embedding-vs-references/#std-label-embedding-vs-references">
Embedded Data Versus References
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/db.dropAllUsers/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.dropAllUsers()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.dropallusers--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.dropallusers--">
</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.dropAllUsers(writeConcern)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.dropAllUsers" 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.dropAllUsers">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Removes all users from the current database.
</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/dropAllUsersFromDatabase/#mongodb-dbcommand-dbcmd.dropAllUsersFromDatabase">
<code class="leafygreen-ui-1l06pbn">
dropAllUsersFromDatabase
</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>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.dropAllUsers" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.dropAllUsers()
</code>
</span>
</a>
method removes all users from the
database.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.dropAllUsers" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.dropAllUsers()
</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>
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">
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
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.dropAllUsers" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.dropAllUsers()
</code>
</span>
</a>
method wraps the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dropAllUsersFromDatabase/#mongodb-dbcommand-dbcmd.dropAllUsersFromDatabase">
<code class="leafygreen-ui-1l06pbn">
dropAllUsersFromDatabase
</code>
</a>
command.
</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">
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.dropAllUsers" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.dropAllUsers()
</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-dropUser">
<code class="leafygreen-ui-1l06pbn">
dropUser
</code>
</a>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#std-label-security-user-actions">
action
</a>
on a database to drop a user from 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 following
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.dropAllUsers" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.dropAllUsers()
</code>
</span>
</a>
operation drops every user from
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.dropAllUsers( {w: \"majority\", wtimeout: 5000} )","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">dropAllUsers</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></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
n
</code>
field in the results document shows the number of users
removed:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"n\" : 12, \"ok\" : 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">{ <span class="lg-highlight-string">"n"</span> <!-- -->: <span class="lg-highlight-number">12</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>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/dayOfMonth/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$dayOfMonth (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-dayofmonth--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="-dayofmonth--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">
$dayOfMonth
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-dayOfMonth" 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.-dayOfMonth">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns the day of the month for a date as a number between 1 and 31.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-dayOfMonth" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$dayOfMonth
</code>
</span>
</a>
expression has the following
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
operator expression syntax
<!-- -->
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $dayOfMonth: <dateexpression> }","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">$dayOfMonth</span>: <<!-- -->dateExpression<!-- -->> <!-- -->}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
An
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-date">
Date
</a>
, a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-timestamp">
Timestamp
</a>
, or an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-object-id">
ObjectID
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A document with this format:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ date: <dateexpression>, timezone: <tzexpression> }","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">date</span>: <<!-- -->dateExpression<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">timezone</span>: <<!-- -->tzExpression<!-- -->> <!-- -->}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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>
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">
date
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The date to which the operator is applied.
<code class="leafygreen-ui-1l06pbn">
<dateExpression>
</code>
must be a valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-date">
Date
</a>
, a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-timestamp">
Timestamp
</a>
,
or an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-object-id">
ObjectID
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
timezone
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. The timezone of the operation result.
<code class="leafygreen-ui-1l06pbn">
<tzExpression>
</code>
must be a valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to a string formatted as either
an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones" rel="noopener noreferrer">
<span>
Olson Timezone Identifier
</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>
or a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/wiki/List_of_UTC_time_offsets" rel="noopener noreferrer">
<span>
UTC Offset
</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>
.
If no
<code class="leafygreen-ui-1l06pbn">
timezone
</code>
is provided, the result is in UTC.
</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>
Format
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Examples
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
Olson Timezone Identifier
</code>
</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":"\"America/New_York\"\n\"Europe/London\"\n\"GMT\""}
</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">"America/New_York"</td></tr><tr class=""><td class="leafygreen-ui-7razhx">"Europe/London"</td></tr><tr class=""><td class="leafygreen-ui-7razhx">"GMT"</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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>
<code class="leafygreen-ui-1l06pbn">
UTC Offset
</code>
</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":"+/-[hh]:[mm], e.g. \"+04:45\"\n+/-[hh][mm], e.g. \"-0530\"\n+/-[hh], e.g. \"+03\""}
</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">+/-[hh]:[mm], e.g. "+04:45"</td></tr><tr class=""><td class="leafygreen-ui-7razhx">+/-[hh][mm], e.g. "-0530"</td></tr><tr class=""><td class="leafygreen-ui-7razhx">+/-[hh], e.g. "+03"</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</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>
<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>
Result
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $dayOfMonth: new Date(\"2016-01-01\") }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$dayOfMonth</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2016-01-01"</span>) }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
1
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $dayOfMonth: { date: new Date(\"Jan 7, 2003\") } }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$dayOfMonth</span>: <!-- -->{ <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"Jan 7, 2003"</span>) } }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
7
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $dayOfMonth: {\n date: new Date(\"August 14, 2011\"),\n timezone: \"America/Chicago\"\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">{ <span class="lg-highlight-attr">$dayOfMonth</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"August 14, 2011"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timezone</span>: <span class="lg-highlight-string">"America/Chicago"</span></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>
14
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $dayOfMonth: ISODate(\"1998-11-07T00:00:00Z\") }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$dayOfMonth</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"1998-11-07T00:00:00Z"</span>) }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
7
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $dayOfMonth: {\n date: ISODate(\"1998-11-07T00:00:00Z\"),\n timezone: \"-0400\"\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">{ <span class="lg-highlight-attr">$dayOfMonth</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"1998-11-07T00:00:00Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timezone</span>: <span class="lg-highlight-string">"-0400"</span></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>
6
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $dayOfMonth: \"March 28, 1976\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$dayOfMonth</span>: <span class="lg-highlight-string">"March 28, 1976"</span> }</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">
error
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $dayOfMonth: Date(\"2016-01-01\") }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$dayOfMonth</span>: <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2016-01-01"</span>) }</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">
error
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $dayOfMonth: \"2009-04-09\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$dayOfMonth</span>: <span class="lg-highlight-string">"2009-04-09"</span> }</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">
error
</code>
</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>
$dayOfMonth cannot take a string as an argument.
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
</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">
Consider a
<code class="leafygreen-ui-1l06pbn">
sales
</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":"{\n \"_id\" : 1,\n \"item\" : \"abc\",\n \"price\" : 10,\n \"quantity\" : 2,\n \"date\" : ISODate(\"2014-01-01T08:15:39.736Z\")\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">"item"</span> <!-- -->: <span class="lg-highlight-string">"abc"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">10</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"date"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2014-01-01T08:15:39.736Z"</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 uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-dayOfMonth" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$dayOfMonth
</code>
</span>
</a>
and other
date operators to break down the
<code class="leafygreen-ui-1l06pbn">
date
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sales.aggregate(\n [\n {\n $project:\n {\n year: { $year: \"$date\" },\n month: { $month: \"$date\" },\n day: { $dayOfMonth: \"$date\" },\n hour: { $hour: \"$date\" },\n minutes: { $minute: \"$date\" },\n seconds: { $second: \"$date\" },\n milliseconds: { $millisecond: \"$date\" },\n dayOfYear: { $dayOfYear: \"$date\" },\n dayOfWeek: { $dayOfWeek: \"$date\" },\n week: { $week: \"$date\" }\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">sales</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">year</span>: <!-- -->{ <span class="lg-highlight-attr">$year</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">month</span>: <!-- -->{ <span class="lg-highlight-attr">$month</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">day</span>: <!-- -->{ <span class="lg-highlight-attr">$dayOfMonth</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">hour</span>: <!-- -->{ <span class="lg-highlight-attr">$hour</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">minutes</span>: <!-- -->{ <span class="lg-highlight-attr">$minute</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">seconds</span>: <!-- -->{ <span class="lg-highlight-attr">$second</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">milliseconds</span>: <!-- -->{ <span class="lg-highlight-attr">$millisecond</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">dayOfYear</span>: <!-- -->{ <span class="lg-highlight-attr">$dayOfYear</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">dayOfWeek</span>: <!-- -->{ <span class="lg-highlight-attr">$dayOfWeek</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">week</span>: <!-- -->{ <span class="lg-highlight-attr">$week</span>: <span class="lg-highlight-string">"$date"</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 result:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : 1,\n \"year\" : 2014,\n \"month\" : 1,\n \"day\" : 1,\n \"hour\" : 8,\n \"minutes\" : 15,\n \"seconds\" : 39,\n \"milliseconds\" : 736,\n \"dayOfYear\" : 1,\n \"dayOfWeek\" : 4,\n \"week\" : 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">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"year"</span> <!-- -->: <span class="lg-highlight-number">2014</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"month"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"day"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hour"</span> <!-- -->: <span class="lg-highlight-number">8</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"minutes"</span> <!-- -->: <span class="lg-highlight-number">15</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"seconds"</span> <!-- -->: <span class="lg-highlight-number">39</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"milliseconds"</span> <!-- -->: <span class="lg-highlight-number">736</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"dayOfYear"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"dayOfWeek"</span> <!-- -->: <span class="lg-highlight-number">4</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"week"</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>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/log10/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$log10 (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-log10--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="-log10--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">
$log10
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-log10" 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.-log10">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Calculates the log base 10 of a number and returns the result as a
double.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-log10" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$log10
</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":"{ $log10: <number> }","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">$log10</span>: <<!-- -->number<!-- -->> <!-- -->}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
<number>
</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 non-negative
number. 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>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-log10" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$log10
</code>
</span>
</a>
is equivalent to
<code class="leafygreen-ui-1l06pbn">
$log: [ <number>, 10 ]
</code>
expression.
</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">
The default return type is a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-Double">
<code class="leafygreen-ui-1l06pbn">
double
</code>
</a>
. If at least
one operand is a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-Decimal128">
<code class="leafygreen-ui-1l06pbn">
decimal
</code>
</a>
, then the return
type is a decimal.
</p>
<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,
<code class="leafygreen-ui-1l06pbn">
$log10
</code>
returns
<code class="leafygreen-ui-1l06pbn">
null
</code>
. If the argument resolves to
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
,
<code class="leafygreen-ui-1l06pbn">
$log10
</code>
returns
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
.
</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">
{ $log10: 1 }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
0
</code>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $log10: 10 }
</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">
{ $log10: 100 }
</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">
{ $log10: 1000 }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
3
</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">
Create a collection named
<code class="leafygreen-ui-1l06pbn">
samples
</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.samples.insertMany(\n [\n { _id: 1, H3O: 0.0025 },\n { _id: 2, H3O: 0.001 },\n { _id: 3, H3O: 0.02 }\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">samples</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">H3O</span>: <span class="lg-highlight-number">0.0025</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">H3O</span>: <span class="lg-highlight-number">0.001</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">H3O</span>: <span class="lg-highlight-number">0.02</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 example calculates the pH value of the samples:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.samples.aggregate( [\n { $project: { pH: { $multiply: [ -1, { $log10: \"$H3O\" } ] } } }\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">samples</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">pH</span>: <!-- -->{ <span class="lg-highlight-attr">$multiply</span>: <!-- -->[ <!-- -->-<span class="lg-highlight-number">1</span>, <!-- -->{ <span class="lg-highlight-attr">$log10</span>: <span class="lg-highlight-string">"$H3O"</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 results:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"pH\" : 2.6020599913279625 }\n{ \"_id\" : 2, \"pH\" : 3 }\n{ \"_id\" : 3, \"pH\" : 1.6989700043360187 }","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">"pH"</span> <!-- -->: <span class="lg-highlight-number">2.6020599913279625</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">"pH"</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">3</span>, <span class="lg-highlight-string">"pH"</span> <!-- -->: <span class="lg-highlight-number">1.6989700043360187</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/log/#mongodb-expression-exp.-log">
<code class="leafygreen-ui-1l06pbn">
$log
</code>
</a>
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/release-notes/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Release Notes
<a class="headerlink leafygreen-ui-1kepx47" href="#release-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="release-notes">
</div>
</a>
</h1>
<p class="leafygreen-ui-1kp3ins">
Always install the latest, stable version of MongoDB. For more
information on types of MongoDB releases, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/versioning/#std-label-release-version-numbers">
MongoDB Versioning
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To see changes in MongoDB releases, see the following release notes.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Current Stable Release
<a class="headerlink leafygreen-ui-1kepx47" href="#current-stable-release" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="current-stable-release">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
(
<em>
8.0-series
</em>
)
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/release-notes/8.0/#std-label-release-notes-8.0">
Release Notes for MongoDB 8.0
</a>
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Previous Stable Releases
<a class="headerlink leafygreen-ui-1kepx47" href="#previous-stable-releases" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="previous-stable-releases">
</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/release-notes/7.0/#std-label-release-notes-7.0">
Release Notes for MongoDB 7.0 (Stable Release)
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/release-notes/6.0/#std-label-release-notes-6.0">
Release Notes for MongoDB 6.0
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/release-notes/5.0/#std-label-release-notes-5.0">
Release Notes for MongoDB 5.0
</a>
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
EOL Releases
<a class="headerlink leafygreen-ui-1kepx47" href="#eol-releases" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="eol-releases">
</div>
</a>
</h2>
<table class="hlist">
<tbody>
<tr>
<td>
<ul class="simple">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
4.4
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
4.2
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
4.0
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
3.6
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
3.4
</p>
</li>
</ul>
</td>
<td>
<ul class="simple">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
3.2
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
3.0
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
2.6
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
2.4
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
2.2
</p>
</li>
</ul>
</td>
<td>
<ul class="simple">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
2.0
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
1.8
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
1.6
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
1.4
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
1.2
</p>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<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 documentation associated with an EOL release, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/legacy/?site=docs" target="_self">
<span>
the legacy documentation
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
</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>
<p class="leafygreen-ui-1kp3ins">
See also:
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/versioning/">
MongoDB Versioning
</a>
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/db.grantRolesToRole/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.grantRolesToRole()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.grantrolestorole--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.grantrolestorole--">
</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.grantRolesToRole(rolename, roles, writeConcern)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.grantRolesToRole" 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.grantRolesToRole">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Grants roles to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-user-defined-roles/#std-label-user-defined-roles">
user-defined role
<!-- -->
.
</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/grantRolesToRole/#mongodb-dbcommand-dbcmd.grantRolesToRole">
<code class="leafygreen-ui-1l06pbn">
grantRolesToRole
</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.grantRolesToRole" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.grantRolesToRole()
</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.grantRolesToRole( \"<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">grantRolesToRole</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.grantRolesToRole" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.grantRolesToRole()
</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 to which to grant sub 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>
An array of roles from which to inherit.
</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.grantRolesToRole" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.grantRolesToRole()
</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.grantRolesToRole" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.grantRolesToRole()
</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">
A role can inherit privileges from other roles in its database. A role
created on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database can inherit privileges from roles in
any 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">
You must have 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 a database to grant 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 following
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.grantRolesToRole" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.grantRolesToRole()
</code>
</span>
</a>
operation updates the
<code class="leafygreen-ui-1l06pbn">
productsReaderWriter
</code>
role in the
<code class="leafygreen-ui-1l06pbn">
products
</code>
database to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-inheritance">
inherit
</a>
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-privileges">
privileges
</a>
of
<code class="leafygreen-ui-1l06pbn">
productsReader
</code>
role:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use products\ndb.grantRolesToRole(\n \"productsReaderWriter\",\n [ \"productsReader\" ],\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 products</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">grantRolesToRole</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"productsReaderWriter"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[ <span class="lg-highlight-string">"productsReader"</span> ]<!-- -->,</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/reference/method/sh.shardCollection/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
sh.shardCollection()
<a class="headerlink leafygreen-ui-1kepx47" href="#sh.shardcollection--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.shardcollection--">
</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>
</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.shardCollection(namespace, key, unique, options)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-sh.shardCollection" 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.shardCollection">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Shards a collection using the
<code class="leafygreen-ui-1l06pbn">
key
</code>
as a the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard-key">
shard key
</a>
.
The shard key determines how MongoDB distributes the collection's
documents among the shards.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0, sharding a collection does
<strong>
not
</strong>
require you to
first run the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.enableSharding/#mongodb-method-sh.enableSharding">
<code class="leafygreen-ui-1l06pbn">
sh.enableSharding()
</code>
</a>
method to configure the database.
</p>
</div>
</div>
<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/shardCollection/#mongodb-dbcommand-dbcmd.shardCollection">
<code class="leafygreen-ui-1l06pbn">
shardCollection
</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">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.shardCollection" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.shardCollection()
</code>
</span>
</a>
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">
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 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 value 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">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-shard-key/#std-label-sharding-shard-key">
shard key
</a>
must be
supported by an index. Unless the collection is empty, the
index must exist prior to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/shardCollection/#mongodb-dbcommand-dbcmd.shardCollection">
<code class="leafygreen-ui-1l06pbn">
shardCollection
</code>
</a>
command. If the collection is empty, MongoDB creates the
index prior to sharding the collection if the index that can
support the shard key does not already exist.
</p>
<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>
<p class="leafygreen-ui-1kp3ins">
Optional. Specify
<code class="leafygreen-ui-1l06pbn">
true
</code>
to ensure that the underlying index
enforces a unique constraint. Defaults to
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
You cannot specify
<code class="leafygreen-ui-1l06pbn">
true
</code>
when using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/hashed-sharding/#std-label-sharding-hashed">
hashed shard keys
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
For
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongo/#std-label-mongo">
Legacy mongo Shell
</a>
, you must explicitly
specify the value for
<code class="leafygreen-ui-1l06pbn">
unique
</code>
if you specify the
<code class="leafygreen-ui-1l06pbn">
options
</code>
document.
<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>
doesn't require
<code class="leafygreen-ui-1l06pbn">
unique
</code>
when you specify the
<code class="leafygreen-ui-1l06pbn">
options
</code>
document.
</p>
</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>
and
<code class="leafygreen-ui-1l06pbn">
collation
</code>
.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
options
</code>
argument supports the following options:
</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">
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>
<p class="leafygreen-ui-1kp3ins">
Optional. Specifies the minimum number of chunks to create
initially when sharding an
<em>
empty
</em>
collection with a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/hashed-sharding/#std-label-sharding-hashed-sharding">
hashed shard key
</a>
. MongoDB
then creates and balances chunks across the cluster. The
<code class="leafygreen-ui-1l06pbn">
numInitialChunks
</code>
parameter must be less than
<code class="leafygreen-ui-1l06pbn">
8192
</code>
chunks per shard.
Defaults to
<code class="leafygreen-ui-1l06pbn">
2
</code>
chunks per shard.
</p>
<p class="leafygreen-ui-1kp3ins">
If the collection is not empty or the shard key does not
contain a hashed field, the operation returns an error.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If sharding with
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-method-shard-collection-presplitHashedZones" target="_self">
<span>
presplitHashedZones: true
</span>
</a>
,
MongoDB attempts to evenly distribute the specified
number of chunks across the zones in the cluster.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If sharding with
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-method-shard-collection-presplitHashedZones" target="_self">
<span>
presplitHashedZones: false
</span>
</a>
or omitted and
no zones and zone ranges are defined for the empty
collection, MongoDB attempts to evenly distributed the
specified number of chunks across the shards in the cluster.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If sharding with
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-method-shard-collection-presplitHashedZones" target="_self">
<span>
presplitHashedZones: false
</span>
</a>
or omitted and
zones and zone ranges have been defined for the empty
collection,
<code class="leafygreen-ui-1l06pbn">
numInitialChunks
</code>
has no effect.
</p>
</li>
</ul>
</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">
shardCollection
</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``{ locale : "simple" }``, or the
<code class="leafygreen-ui-1l06pbn">
shardCollection
</code>
command fails. At least one of the indexes
whose fields support the shard key pattern must have the simple
collation.
</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-method-shard-collection-presplitHashedZones" target="_self">
<span>
presplitHashedZones
</span>
</a>
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-method-shard-collection-presplitHashedZones">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. Specify
<code class="leafygreen-ui-1l06pbn">
true
</code>
to perform initial chunk creation
and distribution for an empty or non-existing collection based
on the defined zones and zone ranges for the collection. For
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/hashed-sharding/#std-label-sharding-hashed">
hashed sharding
</a>
only.
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
shardCollection()
</code>
with
<code class="leafygreen-ui-1l06pbn">
presplitHashedZones: true
</code>
returns
an error if any of the following are true:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The shard key does not contain a hashed field
(i.e. is not a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-hashed/#std-label-index-hashed-index">
single field hashed index
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-hashed/create/#std-label-index-type-compound-hashed">
compound hashed index
</a>
).
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The collection has no defined zones or zone ranges.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The defined zone ranges do not meet the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.updateZoneKeyRange/#std-label-updateZoneKeyRange-method-init-chunk-distribution">
requirements
<!-- -->
.
</a>
</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-method-shard-collection-timeseries" target="_self">
<span>
timeseries
</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-method-shard-collection-timeseries">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. Specify this option to create a new sharded
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries-collections/#std-label-manual-timeseries-collection">
time series
collection
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To shard an existing time series collection, omit this parameter.
</p>
<p class="leafygreen-ui-1kp3ins">
When the collection specified to
<code class="leafygreen-ui-1l06pbn">
shardCollection
</code>
is a time series
collection and the
<code class="leafygreen-ui-1l06pbn">
timeseries
</code>
option is not specified, MongoDB uses
the values that define the existing time series collection to populate
the
<code class="leafygreen-ui-1l06pbn">
timeseries
</code>
field.
</p>
<p class="leafygreen-ui-1kp3ins">
For detailed syntax, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-method-sharded-time-series-collection-options" target="_self">
<span>
Time Series Options
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</dd>
</dl>
<span class="leafygreen-ui-1qnjd6d" id="std-label-method-sharded-time-series-collection-options">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Time Series Options
<a class="headerlink leafygreen-ui-1kepx47" href="#time-series-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="time-series-options">
</div>
</a>
</h3>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.1
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
To create a new
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries-collections/#std-label-manual-timeseries-collection">
time series collection
</a>
that is sharded, specify the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/shardCollection/#std-label-cmd-shard-collection-timeseries">
timeseries
</a>
option to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.shardCollection" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.shardCollection()
</code>
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/shardCollection/#std-label-cmd-shard-collection-timeseries">
timeseries
</a>
option 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">
timeField
</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">
Required. The name of the field which contains the date in each
time series document. Documents in a time series collection must
have a valid BSON date as the value for the
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
metaField
</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. The name of the field which contains metadata in
each time series document. The metadata in the specified field
should be data that is used to label a unique series of
documents. The metadata should rarely, if ever, change
The name of the specified field may not be
<code class="leafygreen-ui-1l06pbn">
_id
</code>
or the same
as the
<code class="leafygreen-ui-1l06pbn">
timeseries.timeField
</code>
. The field can be of any data type.
</p>
<p class="leafygreen-ui-1kp3ins">
Although the
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
field is optional, using metadata can improve
query optimization. For example, MongoDB automatically
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries/timeseries-secondary-index/#std-label-timeseries-add-secondary-index">
creates a compound index
</a>
on the
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
and
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
fields for new collections.
If you do not provide a value for this field, the data is bucketed solely
based on time.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
granularity
</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. Possible values are:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"seconds"
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"minutes"
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"hours"
</code>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
By default, MongoDB sets the
<code class="leafygreen-ui-1l06pbn">
granularity
</code>
to
<code class="leafygreen-ui-1l06pbn">
"seconds"
</code>
for
high-frequency ingestion.
</p>
<p class="leafygreen-ui-1kp3ins">
Manually set the
<code class="leafygreen-ui-1l06pbn">
granularity
</code>
parameter to improve performance
by optimizing how data in the time series collection is stored
internally. To select a value for
<code class="leafygreen-ui-1l06pbn">
granularity
</code>
, choose the
closest match to the time span between consecutive incoming
measurements.
</p>
<p class="leafygreen-ui-1kp3ins">
If you specify the
<code class="leafygreen-ui-1l06pbn">
timeseries.metaField
</code>
, consider the time
span between consecutive incoming measurements that have the same
unique value for the
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
field. Measurements often have
the same unique value for the
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
field if they come
from the same source.
</p>
<p class="leafygreen-ui-1kp3ins">
If you do not specify
<code class="leafygreen-ui-1l06pbn">
timeseries.metaField
</code>
, consider the time
span between all measurements that are inserted in the collection.
</p>
<p class="leafygreen-ui-1kp3ins">
If you set the
<code class="leafygreen-ui-1l06pbn">
granularity
</code>
parameter, you can't set the
<code class="leafygreen-ui-1l06pbn">
bucketMaxSpanSeconds
</code>
and
<code class="leafygreen-ui-1l06pbn">
bucketRoundingSeconds
</code>
parameters.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</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">
Shard Keys
<a class="headerlink leafygreen-ui-1kepx47" href="#shard-keys" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-keys">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
While you can
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-change-a-shard-key/#std-label-change-a-shard-key">
change your shard key
</a>
later, it is important to carefully consider your shard key choice to
avoid scalability and perfomance issues.
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Shard Keys on Time Series Collections
<a class="headerlink leafygreen-ui-1kepx47" href="#shard-keys-on-time-series-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="shard-keys-on-time-series-collections">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
When sharding time series collections, you can only specify
the following fields in the shard key:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Sub-fields of
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
You may specify combinations of these fields in the shard key. No other
fields, including
<code class="leafygreen-ui-1l06pbn">
_id
</code>
, are allowed in the shard key pattern.
</p>
<p class="leafygreen-ui-1kp3ins">
When you specify the shard key:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
can be either a:
</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/hashed-sharding/#std-label-sharding-hashed-sharding">
Hashed shard key
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/ranged-sharding/#std-label-sharding-ranged">
Ranged shard key
</a>
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
must be:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/ranged-sharding/#std-label-sharding-ranged">
ranged shard key
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
At the end of the shard key pattern
</p>
</li>
</ul>
</li>
</ul>
<div class="leafygreen-ui-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Avoid specifying
<strong>
only
</strong>
the
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
as the shard key. Since
the
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-choose-a-shard-key/#std-label-shard-key-monotonic">
increases monotonically
</a>
, it may result in all writes appearing on a
single chunk within the cluster. Ideally, data is evenly distributed
across chunks.
</p>
<p class="leafygreen-ui-1kp3ins">
To learn how to best choose a shard key, 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/core/sharding-choose-a-shard-key/#std-label-sharding-shard-key-requirements">
Choose a Shard Key
</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/blog/post/on-selecting-a-shard-key-for-mongodb?tck=docs_server" target="_self">
<span>
MongoDB Blog: On Selecting a Shard Key for MongoDB
<!-- -->
.
</span>
</a>
</p>
</li>
</ul>
</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/core/sharding-shard-key/#std-label-sharding-shard-key">
Shard Keys
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-choose-a-shard-key/#std-label-sharding-shard-key-selection">
Choose a Shard Key
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Hashed Shard Keys
<a class="headerlink leafygreen-ui-1kepx47" href="#hashed-shard-keys" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="hashed-shard-keys">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/hashed-sharding/#std-label-sharding-hashed-sharding">
Hashed shard keys
</a>
use a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-hashed/#std-label-index-type-hashed">
hashed index
</a>
or a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-hashed/create/#std-label-index-type-compound-hashed">
compound hashed index
</a>
as the shard key.
</p>
<p class="leafygreen-ui-1kp3ins">
Use the form
<code class="leafygreen-ui-1l06pbn">
field: "hashed"
</code>
to specify a hashed shard key field.
</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 chunk migrations are in progress while creating a hashed
shard key collection, the initial chunk distribution may be
uneven until the balancer automatically balances the
collection.
</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">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/hashed-sharding/">
Hashed Sharding
</a>
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sh.shardCollection-zones">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Zone Sharding and Initial Chunk Distribution
<a class="headerlink leafygreen-ui-1kepx47" href="#zone-sharding-and-initial-chunk-distribution" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="zone-sharding-and-initial-chunk-distribution">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The shard collection operation (i.e.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/shardCollection/#mongodb-dbcommand-dbcmd.shardCollection">
<code class="leafygreen-ui-1l06pbn">
shardCollection
</code>
</a>
command and the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.shardCollection" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.shardCollection()
</code>
</span>
</a>
helper) can perform
initial chunk creation and distribution for an empty or a
non-existing collection if
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/zone-sharding/">
zones and zone ranges
</a>
have been defined for the collection. Initial
chunk distribution allows for a faster setup of zoned sharding.
After the initial distribution, the balancer manages the chunk
distribution going forward per usual.
</p>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.updateZoneKeyRange/#std-label-pre-define-zone-range-example">
Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection
</a>
for an example. If sharding a
collection using a ranged or single-field hashed shard key, the
<code class="leafygreen-ui-1l06pbn">
numInitialChunks
</code>
option has no effect if zones and zone ranges have
been defined for the empty collection.
</p>
<p class="leafygreen-ui-1kp3ins">
To shard a collection using a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-hashed/create/#std-label-index-type-compound-hashed">
compound hashed index
</a>
, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-sh.shardCollection-zones-compound-hashed" target="_self">
<span>
Initial Chunk Distribution with Compound Hashed Indexes
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sh.shardCollection-zones-compound-hashed">
</span>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Initial Chunk Distribution with Compound Hashed Indexes
<a class="headerlink leafygreen-ui-1kepx47" href="#initial-chunk-distribution-with-compound-hashed-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="initial-chunk-distribution-with-compound-hashed-indexes">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
MongoDB supports sharding collections on
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-hashed/create/#std-label-index-type-compound-hashed">
compound hashed indexes
</a>
. When
sharding an empty or non-existing collection using a compound hashed
shard key, additional requirements apply in order for MongoDB to
perform initial chunk creation and distribution.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
numInitialChunks
</code>
option has no effect if zones and zone ranges
have been defined for the empty collection
<em>
and
</em>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-method-shard-collection-presplitHashedZones" target="_self">
<span>
presplitHashedZones
</span>
</a>
is
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.updateZoneKeyRange/#std-label-pre-define-zone-range-hashed-example">
Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection
</a>
for an example.
</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-data-partitioning/#std-label-initial-ranges">
Initial Chunks
</a>
</p>
</div>
</div>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Uniqueness
<a class="headerlink leafygreen-ui-1kepx47" href="#uniqueness" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="uniqueness">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If specifying
<code class="leafygreen-ui-1l06pbn">
unique: true
</code>
:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the collection is empty,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.shardCollection" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.shardCollection()
</code>
</span>
</a>
creates the unique index on the
shard key if such an index does not already exist.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the collection is not empty, you must create the index first
before using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.shardCollection" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.shardCollection()
</code>
.
</span>
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Although you can have a unique
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-compound-index">
compound index
</a>
where the shard
key is a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-compound/#std-label-compound-index-prefix">
prefix
</a>
, if using
<code class="leafygreen-ui-1l06pbn">
unique
</code>
parameter, the collection must have a unique index that is on the shard
key.
</p>
<p class="leafygreen-ui-1kp3ins">
See also
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-shard-key/#std-label-sharding-shard-key-unique">
Sharded Collection and Unique Indexes
</a>
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
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>
</h4>
<p class="leafygreen-ui-1kp3ins">
If the collection has a default
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
collation
</a>
,
the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.shardCollection" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.shardCollection()
</code>
</span>
</a>
command must include a
<code class="leafygreen-ui-1l06pbn">
collation
</code>
parameter with the
value
<code class="leafygreen-ui-1l06pbn">
{ locale: "simple" }
</code>
. For non-empty collections with a
default collation, you must have at least one index with the simple
collation whose fields support the shard key pattern.
</p>
<p class="leafygreen-ui-1kp3ins">
You do not need to specify the
<code class="leafygreen-ui-1l06pbn">
collation
</code>
option for collections
without a collation. If you do specify the collation option for
a collection with no collation, it will have no effect.
</p>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Write Concern
<a class="headerlink leafygreen-ui-1kepx47" href="#write-concern" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="write-concern">
</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>
uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
for the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-write-concern">
write concern
</a>
of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/shardCollection/#mongodb-dbcommand-dbcmd.shardCollection">
<code class="leafygreen-ui-1l06pbn">
shardCollection
</code>
</a>
command, its helper
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.shardCollection" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.shardCollection()
</code>
</span>
</a>
, and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.shardAndDistributeCollection/#mongodb-method-sh.shardAndDistributeCollection">
<code class="leafygreen-ui-1l06pbn">
sh.shardAndDistributeCollection()
</code>
</a>
method.
</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">
Simple Usage
<a class="headerlink leafygreen-ui-1kepx47" href="#simple-usage" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="simple-usage">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Given a collection named
<code class="leafygreen-ui-1l06pbn">
people
</code>
in a database named
<code class="leafygreen-ui-1l06pbn">
records
</code>
,
the following command shards the collection by the
<code class="leafygreen-ui-1l06pbn">
zipcode
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.shardCollection(\"records.people\", { zipcode: 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">shardCollection</span>(<span class="lg-highlight-string">"records.people"</span>, <!-- -->{ <span class="lg-highlight-attr">zipcode</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>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Usage with Options
<a class="headerlink leafygreen-ui-1kepx47" href="#usage-with-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="usage-with-options">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
phonebook
</code>
database has a collection
<code class="leafygreen-ui-1l06pbn">
contacts
</code>
with no
default
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.createCollection/#std-label-createCollection-collation-example">
collation
</a>
. The
following example uses
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-sh.shardCollection" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
sh.shardCollection()
</code>
</span>
</a>
to shard the
<code class="leafygreen-ui-1l06pbn">
phonebook.contacts
</code>
with:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/hashed-sharding/#std-label-sharding-hashed-sharding">
hashed shard key
</a>
on the
<code class="leafygreen-ui-1l06pbn">
last_name
</code>
field,
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
5
</code>
initial chunks, and
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
a collation of
<code class="leafygreen-ui-1l06pbn">
simple
</code>
.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.shardCollection(\n \"phonebook.contacts\",\n { last_name: \"hashed\" },\n false,\n {\n numInitialChunks: 5,\n collation: { locale: \"simple\" }\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">sh.<span class="lg-highlight-title lg-highlight-function">shardCollection</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"phonebook.contacts"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">last_name</span>: <span class="lg-highlight-string">"hashed"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">numInitialChunks</span>: <span class="lg-highlight-number">5</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">"simple"</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>
<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/command/refineCollectionShardKey/#mongodb-dbcommand-dbcmd.refineCollectionShardKey">
<code class="leafygreen-ui-1l06pbn">
refineCollectionShardKey
</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.balancerCollectionStatus/#mongodb-method-sh.balancerCollectionStatus">
<code class="leafygreen-ui-1l06pbn">
sh.balancerCollectionStatus()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/shardCollection/#mongodb-dbcommand-dbcmd.shardCollection">
<code class="leafygreen-ui-1l06pbn">
shardCollection
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/sharding/">
Sharding
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/find/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
find
<a class="headerlink leafygreen-ui-1kepx47" href="#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="find">
</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="dbcommand">
<dt>
<code class="leafygreen-ui-1l06pbn">
find
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.find" 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.find">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Executes a query and returns the first batch of results and the
cursor id, from which the client can construct a cursor.
</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.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</a>
or
<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>
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-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This command has limited support 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
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
command has the following syntax:
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n {\n find: <string>,\n filter: <document>,\n sort: <document>,\n projection: <document>,\n hint: <document>,\n skip: <int>,\n limit: <int>,\n batchSize: <int>,\n singleBatch: <bool>,\n comment: <any>,\n maxTimeMS: <int>,\n readConcern: <document>,\n max: <document>,\n min: <document>,\n returnKey: <bool>,\n showRecordId: <bool>,\n tailable: <bool>,\n oplogReplay: <bool>,\n noCursorTimeout: <bool>,\n awaitData: <bool>,\n allowPartialResults: <bool>,\n collation: <document>,\n allowDiskUse : <bool>,\n let: <document> // Added in MongoDB 5.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">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">find</span>: <<!-- -->string<!-- -->><!-- -->,</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">sort</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">projection</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">hint</span>: <!-- --><document or string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> skip: <int>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> limit: <int>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> batchSize: <int>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> singleBatch: <bool>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> comment: <any>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> maxTimeMS: <int>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> readConcern: <document>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> max: <document>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> min: <document>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> returnKey: <bool>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> showRecordId: <bool>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> tailable: <bool>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> oplogReplay: <bool>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> noCursorTimeout: <bool>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> awaitData: <bool>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> allowPartialResults: <bool>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> collation: <document>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> allowDiskUse : <bool>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> let: <document> // Added in MongoDB 5.0</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-find-cmd-fields">
</span>
<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 accepts 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">
find
</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 collection or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/views/#std-label-views-landing-page">
view
</a>
to query.
</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. The query predicate. If unspecified, then all documents in
the collection will match the predicate.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-find-cmd-sort">
</span>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-find-cmd-sort" target="_self">
<span>
sort
</span>
</a>
</p>
</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. The sort specification for the ordering of the 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">
projection
</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
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/project-fields-from-query-results/#std-label-projections">
projection specification
</a>
to
determine which fields to include in the returned documents. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.find/#std-label-find-projection">
Projection
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/projection/">
Projection Operators
<!-- -->
.
</a>
</p>
<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>
operations on views do not support
the following
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/#std-label-query-projection-operators-top">
Query and Projection Operators
</a>
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/projection/positional/#mongodb-projection-proj.-">
<code class="leafygreen-ui-1l06pbn">
$
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/projection/elemMatch/#mongodb-projection-proj.-elemMatch">
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/projection/slice/#mongodb-projection-proj.-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/meta/#mongodb-expression-exp.-meta">
<code class="leafygreen-ui-1l06pbn">
$meta
</code>
</a>
</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">
hint
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
string or document
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. Index specification. Specify either the index name as a
string or the index key pattern. If specified, then the query system
will only consider plans using the hinted index.
</p>
<p class="leafygreen-ui-1kp3ins">
With the following exception,
<code class="leafygreen-ui-1l06pbn">
hint
</code>
is required if the command
includes the
<code class="leafygreen-ui-1l06pbn">
min
</code>
and/or
<code class="leafygreen-ui-1l06pbn">
max
</code>
fields;
<code class="leafygreen-ui-1l06pbn">
hint
</code>
is not required
with
<code class="leafygreen-ui-1l06pbn">
min
</code>
and/or
<code class="leafygreen-ui-1l06pbn">
max
</code>
if the
<code class="leafygreen-ui-1l06pbn">
filter
</code>
is an equality condition
on the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field
<code class="leafygreen-ui-1l06pbn">
{ _id: <value> }
</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">
skip
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Positive integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional. Number of documents to skip. Defaults to 0.
</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">
limit
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Non-negative integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Optional. The maximum number of documents to return. If unspecified,
then defaults to no limit. A limit of 0 is equivalent to setting no
limit.
</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">
batchSize
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
non-negative integer
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. The number of documents to return in the first batch.
Defaults to 101. A batchSize of 0 means that the cursor will be
established, but no documents will be returned in the first batch.
</p>
<p class="leafygreen-ui-1kp3ins">
Unlike the previous wire protocol version, a batchSize of 1 for
the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
command does not close the cursor.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-find-single-batch">
</span>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
singleBatch
</code>
</p>
</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. Determines whether to close the cursor after the first
batch. Defaults to false.
</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>
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>
<p class="leafygreen-ui-1kp3ins">
Any comment set on a
<code class="leafygreen-ui-1l06pbn">
find
</code>
command is inherited
by any subsequent
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
commands run on the
<code class="leafygreen-ui-1l06pbn">
find
</code>
cursor.
</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">
maxTimeMS
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
non-negative integer
</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">
Specifies a time limit in milliseconds.
If you do not specify a value for
<code class="leafygreen-ui-1l06pbn">
maxTimeMS
</code>
, operations will not time out.
A value of
<code class="leafygreen-ui-1l06pbn">
0
</code>
explicitly specifies the default unbounded behavior.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB terminates operations that exceed their allotted time limit
using the same mechanism as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.killOp/#mongodb-method-db.killOp">
<code class="leafygreen-ui-1l06pbn">
db.killOp()
</code>
</a>
. MongoDB only
terminates an operation at one of its designated
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-interrupt-point">
interrupt
points
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
When specifying
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-linearizable/#mongodb-readconcern-readconcern.-linearizable-">
<code class="leafygreen-ui-1l06pbn">
linearizable read concern
</code>
</a>
, always use
<code class="leafygreen-ui-1l06pbn">
maxTimeMS
</code>
in case a majority of
data bearing members are unavailable.
<code class="leafygreen-ui-1l06pbn">
maxTimeMS
</code>
ensures that
the operation does not block indefinitely and instead returns
an error if the read concern cannot be fulfilled.
</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">
readConcern
</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. Specifies the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-read-concern">
read concern
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
readConcern
</code>
option has the following syntax:
<code class="leafygreen-ui-1l06pbn">
readConcern: { level: <value> }
</code>
</p>
<p class="leafygreen-ui-1kp3ins">
Possible read concern levels 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/read-concern-local/#mongodb-readconcern-readconcern.-local-">
<code class="leafygreen-ui-1l06pbn">
"local"
</code>
</a>
. This is the default read concern level for
read operations against the primary and secondaries.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-available/#mongodb-readconcern-readconcern.-available-">
<code class="leafygreen-ui-1l06pbn">
"available"
</code>
</a>
. Available for read operations against
the primary and secondaries.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-available/#mongodb-readconcern-readconcern.-available-">
<code class="leafygreen-ui-1l06pbn">
"available"
</code>
</a>
behaves the
same as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-local/#mongodb-readconcern-readconcern.-local-">
<code class="leafygreen-ui-1l06pbn">
"local"
</code>
</a>
against the primary and non-sharded
secondaries. The query returns the instance's most recent data.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-majority/#mongodb-readconcern-readconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
. Available for replica sets that use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger storage engine
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-linearizable/#mongodb-readconcern-readconcern.-linearizable-">
<code class="leafygreen-ui-1l06pbn">
"linearizable"
</code>
</a>
. Available for read operations on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-states/#mongodb-replstate-replstate.PRIMARY">
<code class="leafygreen-ui-1l06pbn">
primary
</code>
</a>
only.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For more formation on the read concern levels, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern/#std-label-read-concern-levels">
Read Concern Levels
<!-- -->
.
</a>
</p>
<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 uses the
<code class="leafygreen-ui-1l06pbn">
readConcern
</code>
level
specified in the originating
<code class="leafygreen-ui-1l06pbn">
find
</code>
command.
</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">
max
</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
<em>
exclusive
</em>
upper bound for a specific index. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.max/#mongodb-method-cursor.max">
<code class="leafygreen-ui-1l06pbn">
cursor.max()
</code>
</a>
for details.
</p>
<p class="leafygreen-ui-1kp3ins">
To use the
<code class="leafygreen-ui-1l06pbn">
max
</code>
field, the command must also use
<code class="leafygreen-ui-1l06pbn">
hint
</code>
unless the
specified
<code class="leafygreen-ui-1l06pbn">
filter
</code>
is an equality condition on the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field
<code class="leafygreen-ui-1l06pbn">
{ _id: <value> }
</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">
min
</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
<em>
inclusive
</em>
lower bound for a specific index. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.min/#mongodb-method-cursor.min">
<code class="leafygreen-ui-1l06pbn">
cursor.min()
</code>
</a>
for details.
</p>
<p class="leafygreen-ui-1kp3ins">
To use the
<code class="leafygreen-ui-1l06pbn">
min
</code>
field, the command must also use
<code class="leafygreen-ui-1l06pbn">
hint
</code>
unless the
specified
<code class="leafygreen-ui-1l06pbn">
filter
</code>
is an equality condition on the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field
<code class="leafygreen-ui-1l06pbn">
{ _id: <value> }
</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">
returnKey
</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. If true, returns only the index keys in the resulting documents.
Default value is false. If returnKey is true and the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
command does not use an index, the returned documents will be empty.
</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">
showRecordId
</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. Determines whether to return the record identifier for each document.
If true, adds a field $recordId to the returned 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">
tailable
</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. Returns a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-tailable-cursor">
tailable cursor
</a>
for a capped collections.
</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">
awaitData
</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. Use in conjunction with the tailable option to block a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
command on the cursor temporarily at the end
of data rather than returning no data. After a timeout period,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
returns as normal.
</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">
noCursorTimeout
</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. Prevents the server from timing out non-session idle cursors
after an inactivity period of 30 minutes. Ignored for cursors that are
part of a session. For more information, refer to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-session-idle-timeout" target="_self">
<span>
Session Idle Timeout
<!-- -->
.
</span>
</a>
</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="#std-label-cmd-find-allowPartialResults" target="_self">
<span>
allowPartialResults
</span>
</a>
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-cmd-find-allowPartialResults">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. For queries against a sharded collection, allows the
command (or subsequent
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
commands) to return
partial results, rather than an error, if one or more queried
shards are unavailable.
</p>
<p class="leafygreen-ui-1kp3ins">
If
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
(or subsequent
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
commands)
returns partial results because the queried shard(s) aren't available,
the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-cmd-find-output" target="_self">
<span>
find output
</span>
</a>
includes a
<code class="leafygreen-ui-1l06pbn">
partialResultsReturned
</code>
indicator field. If the queried shards are
available for the initial
<code class="leafygreen-ui-1l06pbn">
find
</code>
command, but one or more shards
become unavailable for subsequent
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
commands, only the
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
commands that run while the shards aren't available include
<code class="leafygreen-ui-1l06pbn">
partialResultsReturned
</code>
in their output.
</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">
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>
<p class="leafygreen-ui-1kp3ins">
Optional.
</p>
<p class="leafygreen-ui-1kp3ins">
Specifies the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
collation
</a>
to use for the operation.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
Collation
</a>
allows users to specify
language-specific rules for string comparison, such as rules for
lettercase and accent marks.
</p>
<p class="leafygreen-ui-1kp3ins">
The collation option has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"collation: {\n locale: <string>,\n caseLevel: <boolean>,\n caseFirst: <string>,\n strength: <int>,\n numericOrdering: <boolean>,\n alternate: <string>,\n maxVariable: <string>,\n backwards: <boolean>\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">collation: {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> locale: <string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> caseLevel: <boolean>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> caseFirst: <string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> strength: <int>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> numericOrdering: <boolean>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> alternate: <string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> maxVariable: <string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> backwards: <boolean></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 specifying collation, the
<code class="leafygreen-ui-1l06pbn">
locale
</code>
field is mandatory; all
other collation fields are optional. For descriptions of the fields,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation-document-fields">
Collation Document
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
If the collation is unspecified but the collection has a
default collation (see
<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>
), the
operation uses the collation specified for the collection.
</p>
<p class="leafygreen-ui-1kp3ins">
If no collation is specified for the collection or for the
operations, MongoDB uses the simple binary comparison used in prior
versions for string comparisons.
</p>
<p class="leafygreen-ui-1kp3ins">
You cannot specify multiple collations for an operation. For
example, you cannot specify different collations per field, or if
performing a find with a sort, you cannot use one collation for the
find and another for the sort.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-find-cmd-allowDiskUse">
</span>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-find-cmd-allowDiskUse" target="_self">
<span>
allowDiskUse
</span>
</a>
</p>
</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.
</p>
<p class="leafygreen-ui-1kp3ins">
Use this option to override
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.allowDiskUseByDefault">
<code class="leafygreen-ui-1l06pbn">
allowDiskUseByDefault
</code>
</a>
for a specific query. You can use this option to either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Prohibit disk use on a system where disk use is allowed by
default.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Allow disk use on a system where disk use is prohibited by
default.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0, if
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.allowDiskUseByDefault">
<code class="leafygreen-ui-1l06pbn">
allowDiskUseByDefault
</code>
</a>
is set
to
<code class="leafygreen-ui-1l06pbn">
true
</code>
and the server requires more than 100 megabytes of memory
for a pipeline execution stage, MongoDB automatically writes
temporary files to disk unless the query specifies
<code class="leafygreen-ui-1l06pbn">
{ allowDiskUse: false }
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.allowDiskUseByDefault">
<code class="leafygreen-ui-1l06pbn">
allowDiskUseByDefault
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
allowDiskUse
</code>
has no effect if MongoDB can satisfy the
specified
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-find-cmd-sort" target="_self">
<span>
sort
</span>
</a>
using an index,
<em>
or
</em>
if the
blocking sort requires less than 100 megabytes of memory.
</p>
<p class="leafygreen-ui-1kp3ins">
For more complete documentation on
<code class="leafygreen-ui-1l06pbn">
allowDiskUse
</code>
,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.allowDiskUse/#mongodb-method-cursor.allowDiskUse">
<code class="leafygreen-ui-1l06pbn">
cursor.allowDiskUse()
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on memory restrictions for large blocking
sorts, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.sort/#std-label-sort-index-use">
Sort and Index Use
<!-- -->
.
</a>
</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="#std-label-find-let-syntax" target="_self">
<span>
let
</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-find-let-syntax">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional.
</p>
<p class="leafygreen-ui-1kp3ins">
Specifies a document with a list of variables. This allows you to
improve command readability by separating the variables from the query
text.
</p>
<p class="leafygreen-ui-1kp3ins">
The document syntax is:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n <variable_name_1>: <expression_1>,\n ...,\n <variable_name_n>: <expression_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"> <<!-- -->variable_name_1<!-- -->><!-- -->: <<!-- -->expression_1<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <<!-- -->variable_name_n<!-- -->><!-- -->: <<!-- -->expression_n<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The variable is set to the value returned by the expression, and cannot
be changed afterwards.
</p>
<p class="leafygreen-ui-1kp3ins">
To access the value of a variable in the command, use the double
dollar sign prefix (
<code class="leafygreen-ui-1l06pbn">
$$
</code>
) together with your variable name in the form
<code class="leafygreen-ui-1l06pbn">
$$<variable_name>
</code>
. For example:
<code class="leafygreen-ui-1l06pbn">
$$targetTotal
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
To use a variable to filter results, you must access the variable
within the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator.
</p>
<p class="leafygreen-ui-1kp3ins">
For a complete example using
<code class="leafygreen-ui-1l06pbn">
let
</code>
and variables, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-find-let-example" target="_self">
<span>
Use Variables in
<code class="leafygreen-ui-1l06pbn">
let
</code>
.
</span>
</a>
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-cmd-find-output">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" 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>
</h3>
<p class="leafygreen-ui-1kp3ins">
The command returns a document that contains the cursor information,
including the cursor ID and the first batch of documents. For example,
the following document is returned when run against a sharded
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"cursor\" : {\n \"firstBatch\" : [\n {\n \"_id\" : ObjectId(\"5e8e2ca217b5324fa9847435\"),\n \"zipcode\" : \"20001\",\n \"x\" : 1\n },\n {\n \"_id\" : ObjectId(\"5e8e2ca517b5324fa9847436\"),\n \"zipcode\" : \"30001\",\n \"x\" : 1\n }\n ],\n \"partialResultsReturned\" : true,\n \"id\" : NumberLong(\"668860441858272439\"),\n \"ns\" : \"test.contacts\"\n },\n \"ok\" : 1,\n \"operationTime\" : Timestamp(1586380205, 1),\n \"$clusterTime\" : {\n \"clusterTime\" : Timestamp(1586380225, 2),\n \"signature\" : {\n \"hash\" : BinData(0,\"aI/jWsUVUSkMw8id+A+AVVTQh9Y=\"),\n \"keyId\" : NumberLong(\"6813364731999420435\")\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-string">"cursor"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"firstBatch"</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">"5e8e2ca217b5324fa9847435"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"zipcode"</span> <!-- -->: <span class="lg-highlight-string">"20001"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"x"</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-string">"_id"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5e8e2ca517b5324fa9847436"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"zipcode"</span> <!-- -->: <span class="lg-highlight-string">"30001"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"x"</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-string">"partialResultsReturned"</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-title lg-highlight-class">NumberLong</span>(<span class="lg-highlight-string">"668860441858272439"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"ns"</span> <!-- -->: <span class="lg-highlight-string">"test.contacts"</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">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">1586380205</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">1586380225</span>, <span class="lg-highlight-number">2</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">"aI/jWsUVUSkMw8id+A+AVVTQh9Y="</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">"6813364731999420435"</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-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">
cursor
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Contains the cursor information, including the
cursor
<code class="leafygreen-ui-1l06pbn">
id
</code>
and the
<code class="leafygreen-ui-1l06pbn">
firstBatch
</code>
of documents.
</p>
<p class="leafygreen-ui-1kp3ins">
If the operation against a sharded collection returns partial results
due to the unavailability of the queried shard(s), the
<code class="leafygreen-ui-1l06pbn">
cursor
</code>
document includes a
<code class="leafygreen-ui-1l06pbn">
partialResultsReturned
</code>
field. To return partial
results, rather than error, due to the unavailability of the queried
shard(s), the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
command must run with
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-cmd-find-allowPartialResults" target="_self">
<span>
allowPartialResults
</span>
</a>
set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
. See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-cmd-find-allowPartialResults" target="_self">
<span>
allowPartialResults
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
If the queried shards are initially available for the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
command but one or more shards become
unavailable in subsequent
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
commands, only
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
commands run when a queried shard or
shards are unavailable include the
<code class="leafygreen-ui-1l06pbn">
partialResultsReturned
</code>
flag in the output.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<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>
Indicates whether the command has succeeded (
<code class="leafygreen-ui-1l06pbn">
1
</code>
) or failed
(
<code class="leafygreen-ui-1l06pbn">
0
</code>
).
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
In addition to the aforementioned
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
-specific fields,
the
<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>
includes the following information for
replica sets and sharded clusters:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$clusterTime
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
operationTime
</code>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.runCommand/#std-label-command-response">
db.runCommand() Results
</a>
for details.
</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">
<code class="leafygreen-ui-1l06pbn">
$regex
</code>
Find Queries No Longer Ignore Invalid Regex
<a class="headerlink leafygreen-ui-1kepx47" href="#-regex-find-queries-no-longer-ignore-invalid-regex" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-regex-find-queries-no-longer-ignore-invalid-regex">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.1, invalid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/regex/#mongodb-query-op.-regex">
<code class="leafygreen-ui-1l06pbn">
$regex options
</code>
</a>
options are no longer ignored. This change makes
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/regex/#mongodb-query-op.-regex">
<code class="leafygreen-ui-1l06pbn">
$regex options
</code>
</a>
more consistent with
the use of
<code class="leafygreen-ui-1l06pbn">
$regex
</code>
in 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 and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/project-fields-from-query-results/#std-label-projection">
projection
</a>
queries.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Sessions
<a class="headerlink leafygreen-ui-1kepx47" href="#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="sessions">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
For cursors created inside a session, you cannot call
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
outside the session.
</p>
<p class="leafygreen-ui-1kp3ins">
Similarly, for cursors created outside of a session, you cannot call
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
inside a session.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-session-idle-timeout">
</span>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Session Idle Timeout
<a class="headerlink leafygreen-ui-1kepx47" href="#session-idle-timeout" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-idle-timeout">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
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>
associate all operations with a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/server-sessions/">
server session
</a>
, with the exception of unacknowledged
write operations. For operations not explicitly associated with a
session (i.e. using
<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>
), MongoDB drivers
and
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
create an implicit session and associate it
with the operation.
</p>
<p class="leafygreen-ui-1kp3ins">
If a session is idle for longer than 30 minutes, the MongoDB server
marks that session as expired and may close it at any time. When the
MongoDB server closes the session, it also kills any in-progress
operations and open cursors associated with the session. This
includes cursors configured with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.noCursorTimeout/#mongodb-method-cursor.noCursorTimeout">
<code class="leafygreen-ui-1l06pbn">
noCursorTimeout()
</code>
</a>
or
a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.maxTimeMS/#mongodb-method-cursor.maxTimeMS">
<code class="leafygreen-ui-1l06pbn">
maxTimeMS()
</code>
</a>
greater than 30 minutes.
</p>
<p class="leafygreen-ui-1kp3ins">
For operations that return a cursor, if the cursor may be idle for
longer than 30 minutes, issue the operation within an explicit session
using
<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>
and periodically refresh the
session using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/refreshSessions/#mongodb-dbcommand-dbcmd.refreshSessions">
<code class="leafygreen-ui-1l06pbn">
refreshSessions
</code>
</a>
command. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/limits/#mongodb-limit-Session-Idle-Timeout">
Session Idle Timeout
</a>
for more information.
</p>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Transactions
<a class="headerlink leafygreen-ui-1kepx47" href="#transactions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="transactions">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
can be used inside
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
distributed transactions
<!-- -->
.
</a>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For cursors created outside of a transaction, you cannot call
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
inside the transaction.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For cursors created in a transaction, you cannot call
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
outside the transaction.
</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">
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>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Client Disconnection
<a class="headerlink leafygreen-ui-1kepx47" href="#client-disconnection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-disconnection">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 4.2, if the client that issued
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
disconnects before the operation completes, MongoDB marks
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
for termination using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/killOp/#mongodb-dbcommand-dbcmd.killOp">
<code class="leafygreen-ui-1l06pbn">
killOp
</code>
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Stable API
<a class="headerlink leafygreen-ui-1kepx47" href="#stable-api" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/stable-api/#std-label-stable-api">
Stable API
</a>
V1, the following
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
command fields are not supported:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
awaitData
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
max
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
min
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
noCursorTimeout
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
oplogReplay
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
returnKey
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
showRecordId
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
tailable
</code>
</p>
</li>
</ul>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Index Filters and Collations
<a class="headerlink leafygreen-ui-1kepx47" href="#index-filters-and-collations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-filters-and-collations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0, an index filter uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
collation
</a>
previously set using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/planCacheSetFilter/#mongodb-dbcommand-dbcmd.planCacheSetFilter">
<code class="leafygreen-ui-1l06pbn">
planCacheSetFilter
</code>
</a>
command.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, use query settings instead of adding
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-index-filters">
index filters
</a>
. Index filters are deprecated
starting in MongoDB 8.0.
</p>
<p class="leafygreen-ui-1kp3ins">
Query settings have more functionality than index filters. Also, index
filters aren't persistent and you cannot easily create index filters for
all cluster nodes. To add query settings and explore examples, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setQuerySettings/#mongodb-dbcommand-dbcmd.setQuerySettings">
<code class="leafygreen-ui-1l06pbn">
setQuerySettings
</code>
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Find Cursor Behavior on Views
<a class="headerlink leafygreen-ui-1kepx47" href="#find-cursor-behavior-on-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="find-cursor-behavior-on-views">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 7.3, when you use a find command on a view
with the
<code class="leafygreen-ui-1l06pbn">
singleBatch: true
</code>
and
<code class="leafygreen-ui-1l06pbn">
batchSize: 1
</code>
options, a cursor
is no longer returned. In previous versions of MongoDB these find queries
would return a cursor even when you set the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-find-single-batch" target="_self">
<span>
single batch
</span>
</a>
option to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Query Settings
<a class="headerlink leafygreen-ui-1kepx47" href="#query-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="query-settings">
</div>
</a>
</h3>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
8.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
You can use query settings to set index hints, set
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/operation-rejection-filters/#std-label-operation-rejection-filters">
operation
rejection filters
</a>
, and other fields. The
settings apply to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/queryStats/#std-label-queryStats-queryShape">
query shape
</a>
on the
entire cluster. The cluster retains the settings after shutdown.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-query-optimizer">
query optimizer
</a>
uses the query settings as an additional
input during query planning, which affects the plan selected to run the
query. You can also use query settings to block a query shape.
</p>
<p class="leafygreen-ui-1kp3ins">
To add query settings and explore examples, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setQuerySettings/#mongodb-dbcommand-dbcmd.setQuerySettings">
<code class="leafygreen-ui-1l06pbn">
setQuerySettings
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
You can add query settings for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/distinct/#mongodb-dbcommand-dbcmd.distinct">
<code class="leafygreen-ui-1l06pbn">
distinct
</code>
</a>
,
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#mongodb-dbcommand-dbcmd.aggregate">
<code class="leafygreen-ui-1l06pbn">
aggregate
</code>
</a>
commands.
</p>
<p class="leafygreen-ui-1kp3ins">
Query settings have more functionality and are preferred over
deprecated
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-index-filters">
index filters
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To remove query settings, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/removeQuerySettings/#mongodb-dbcommand-dbcmd.removeQuerySettings">
<code class="leafygreen-ui-1l06pbn">
removeQuerySettings
</code>
</a>
. To
obtain the query settings, use a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/querySettings/#mongodb-pipeline-pipe.-querySettings">
<code class="leafygreen-ui-1l06pbn">
$querySettings
</code>
</a>
stage in an
aggregation pipeline.
</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">
Specify a Sort and Limit
<a class="headerlink leafygreen-ui-1kepx47" href="#specify-a-sort-and-limit" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-a-sort-and-limit">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following command runs the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
command filtering on the
<code class="leafygreen-ui-1l06pbn">
rating
</code>
field and the
<code class="leafygreen-ui-1l06pbn">
cuisine
</code>
field.
The command includes a
<code class="leafygreen-ui-1l06pbn">
projection
</code>
to only return the
following fields in the matching documents:
<code class="leafygreen-ui-1l06pbn">
_id
</code>
,
<code class="leafygreen-ui-1l06pbn">
name
</code>
,
<code class="leafygreen-ui-1l06pbn">
rating
</code>
, and
<code class="leafygreen-ui-1l06pbn">
address
</code>
fields.
</p>
<p class="leafygreen-ui-1kp3ins">
The command sorts the documents in the result set by the
<code class="leafygreen-ui-1l06pbn">
name
</code>
field and limits the result set to 5 documents.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n {\n find: \"restaurants\",\n filter: { rating: { $gte: 9 }, cuisine: \"italian\" },\n projection: { name: 1, rating: 1, address: 1 },\n sort: { name: 1 },\n limit: 5\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">find</span>: <span class="lg-highlight-string">"restaurants"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">rating</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-number">9</span> }<!-- -->, <span class="lg-highlight-attr">cuisine</span>: <span class="lg-highlight-string">"italian"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">projection</span>: <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">rating</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">address</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sort</span>: <!-- -->{ <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">limit</span>: <span class="lg-highlight-number">5</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">
Override Default Read Concern
<a class="headerlink leafygreen-ui-1kepx47" href="#override-default-read-concern" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="override-default-read-concern">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To override the default read concern level of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-local/#mongodb-readconcern-readconcern.-local-">
<code class="leafygreen-ui-1l06pbn">
"local"
</code>
</a>
,
use the
<code class="leafygreen-ui-1l06pbn">
readConcern
</code>
option.
</p>
<p class="leafygreen-ui-1kp3ins">
The following operation on a replica set specifies a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-read-concern">
read concern
</a>
of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-majority/#mongodb-readconcern-readconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
to read the most recent copy of
the data confirmed as having been written to a majority of the nodes.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n {\n find: \"restaurants\",\n filter: { rating: { $lt: 5 } },\n readConcern: { level: \"majority\" }\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">find</span>: <span class="lg-highlight-string">"restaurants"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">rating</span>: <!-- -->{ <span class="lg-highlight-attr">$lt</span>: <span class="lg-highlight-number">5</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">readConcern</span>: <!-- -->{ <span class="lg-highlight-attr">level</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>
<p class="leafygreen-ui-1kp3ins">
Regardless of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-read-concern">
read concern
</a>
level, the most recent data on a
node may not reflect the most recent version of the data in the system.
</p>
<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 uses the
<code class="leafygreen-ui-1l06pbn">
readConcern
</code>
level
specified in the originating
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
command.
</p>
<p class="leafygreen-ui-1kp3ins">
A
<code class="leafygreen-ui-1l06pbn">
readConcern
</code>
can be specified for 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.find/#mongodb-method-db.collection.find">
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</a>
using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.readConcern/#mongodb-method-cursor.readConcern">
<code class="leafygreen-ui-1l06pbn">
cursor.readConcern()
</code>
</a>
method:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.restaurants.find( { rating: { $lt: 5 } } ).readConcern(\"majority\")","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">restaurants</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">rating</span>: <!-- -->{ <span class="lg-highlight-attr">$lt</span>: <span class="lg-highlight-number">5</span> } } ).<span class="lg-highlight-title lg-highlight-function">readConcern</span>(<span class="lg-highlight-string">"majority"</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 available read concerns, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern/#std-label-read-concern">
Read Concern
<!-- -->
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Specify Collation
<a class="headerlink leafygreen-ui-1kepx47" href="#specify-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="specify-collation">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
Collation
</a>
allows users to specify
language-specific rules for string comparison, such as rules for
lettercase and accent marks.
</p>
<p class="leafygreen-ui-1kp3ins">
The following operation runs the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find
</code>
</span>
</a>
command with the collation specified:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.runCommand(\n {\n find: \"myColl\",\n filter: { category: \"cafe\", status: \"a\" },\n sort: { category: 1 },\n collation: { locale: \"fr\", strength: 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">find</span>: <span class="lg-highlight-string">"myColl"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"cafe"</span>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"a"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sort</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><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="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
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.collation/#mongodb-method-cursor.collation">
<code class="leafygreen-ui-1l06pbn">
cursor.collation()
</code>
</a>
to
specify
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
collation
</a>
for a
<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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-find-let-example">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Use Variables in
<code class="leafygreen-ui-1l06pbn">
let
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#use-variables-in-let" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-variables-in-let">
</div>
</a>
</h3>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
To define variables that you can access elsewhere in the command, use
the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-find-let-syntax" target="_self">
<span>
let
</span>
</a>
option.
</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 filter results using a variable, you must access the variable
within the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Create a collection
<code class="leafygreen-ui-1l06pbn">
cakeFlavors
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeFlavors.insertMany( [\n { _id: 1, flavor: \"chocolate\" },\n { _id: 2, flavor: \"strawberry\" },\n { _id: 3, flavor: \"cherry\" }\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">cakeFlavors</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">flavor</span>: <span class="lg-highlight-string">"chocolate"</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">flavor</span>: <span class="lg-highlight-string">"strawberry"</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">flavor</span>: <span class="lg-highlight-string">"cherry"</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 defines a
<code class="leafygreen-ui-1l06pbn">
targetFlavor
</code>
variable in
<code class="leafygreen-ui-1l06pbn">
let
</code>
and
uses the variable to retrieve the chocolate cake flavor:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeFlavors.runCommand( {\n find: db.cakeFlavors.getName(),\n filter: { $expr: { $eq: [ \"$flavor\", \"$$targetFlavor\" ] } },\n let : { targetFlavor: \"chocolate\" }\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">cakeFlavors</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">find</span>: <!-- -->db.<span class="lg-highlight-property">cakeFlavors</span>.<span class="lg-highlight-title lg-highlight-function">getName</span>(<!-- -->)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">$expr</span>: <!-- -->{ <span class="lg-highlight-attr">$eq</span>: <!-- -->[ <span class="lg-highlight-string">"$flavor"</span>, <span class="lg-highlight-string">"$$targetFlavor"</span> ] } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">let</span> <!-- -->: <!-- -->{ <span class="lg-highlight-attr">targetFlavor</span>: <span class="lg-highlight-string">"chocolate"</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>
|
https://www.mongodb.com/docs/manual/reference/method/db.collection.updateMany/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.collection.updateMany()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.collection.updatemany--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.updatemany--">
</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="#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-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://www.mongodb.com/docs/drivers/csharp/current/fundamentals/crud/write-operations/modify/#update-operations" 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/v5.2/fundamentals/crud/write-operations/modify/#update" 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://www.mongodb.com/docs/drivers/node/current/fundamentals/crud/write-operations/modify/#update-documents" 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(#:R2dtlqcmqn9:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":R2dtlqcmqn9: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=":R2dtlqcmqn9: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=":R2dtlqcmqn9: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/write/update/#update-many-documents" 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=":R2htlqcmqn9: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(#:R2htlqcmqn9:aa)">
</path>
<lineargradient gradienttransform="matrix(.5625 0 0 -.568 -160.323 -146.09)" gradientunits="userSpaceOnUse" id=":R2htlqcmqn9: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(#:R2htlqcmqn9:bb)">
</path>
<radialgradient cx="-2274.721" cy="144.526" gradienttransform="matrix(0 -.2399 -1.0547 0 208.637 -414.922)" gradientunits="userSpaceOnUse" id=":R2htlqcmqn9: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(#:R2htlqcmqn9:cc)" opacity=".444">
</path>
</svg>
<span>
PyMongo
</span>
</a>
<a class="leafygreen-ui-18cpm0b" href="https://mongoc.org/libmongoc/current/mongoc_collection_update_many.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_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/v3.11/write/update/#update-many-documents" 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/crud/write-operations/modify/#update" 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/write/write-update-documents/" 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://www.mongodb.com/docs/drivers/kotlin/coroutine/current/fundamentals/crud/write-operations/modify/#update" 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(#:R35tlqcmqn9:paint0_linear_211_40588)">
</path>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":R35tlqcmqn9: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://www.mongodb.com/docs/languages/kotlin/kotlin-sync-driver/current/write/update/#update-many-documents" 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(#:R39tlqcmqn9:paint0_linear_211_40588)">
</path>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":R39tlqcmqn9: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/write/update/" 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.mongodb.com/docs/mongoid/master/api/Mongoid/Contextual/Mongo.html#update_all-instance_method" 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=":R3htlqcmqn9: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=":R3htlqcmqn9: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=":R3htlqcmqn9: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=":R3htlqcmqn9: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=":R3htlqcmqn9: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=":R3htlqcmqn9: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=":R3htlqcmqn9: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=":R3htlqcmqn9: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=":R3htlqcmqn9: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=":R3htlqcmqn9: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=":R3htlqcmqn9: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=":R3htlqcmqn9: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=":R3htlqcmqn9: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=":R3htlqcmqn9: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=":R3htlqcmqn9: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=":R3htlqcmqn9: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(#:R3htlqcmqn9:a)">
</path>
<path d="M240.677 241.257L224.482 129.48l-44.113 58.25 60.308 53.528z" fill="url(#:R3htlqcmqn9:b)">
</path>
<path d="M240.896 241.257l-118.646-9.313-69.674 21.986 188.32-12.673z" fill="url(#:R3htlqcmqn9:c)">
</path>
<path d="M52.744 253.955l29.64-97.1L17.16 170.8l35.583 83.154z" fill="url(#:R3htlqcmqn9:d)">
</path>
<path d="M180.358 188.05L153.085 81.226l-78.047 73.16 105.32 33.666z" fill="url(#:R3htlqcmqn9:e)">
</path>
<path d="M248.693 82.73l-73.777-60.256-20.544 66.418 94.321-6.162z" fill="url(#:R3htlqcmqn9:f)">
</path>
<path d="M214.191.99L170.8 24.97 143.424.669l70.767.322z" fill="url(#:R3htlqcmqn9:g)">
</path>
<path d="M0 203.372l18.177-33.151-14.704-39.494L0 203.372z" fill="url(#:R3htlqcmqn9: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(#:R3htlqcmqn9: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(#:R3htlqcmqn9: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(#:R3htlqcmqn9: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(#:R3htlqcmqn9: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(#:R3htlqcmqn9: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(#:R3htlqcmqn9: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(#:R3htlqcmqn9: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(#:R3htlqcmqn9:p)">
</path>
</svg>
<span>
Mongoid
</span>
</a>
<a class="leafygreen-ui-18cpm0b" href="https://www.mongodb.com/docs/drivers/rust/current/fundamentals/crud/write-operations/change/#update-documents" 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://mongodb.github.io/mongo-java-driver/5.2/apidocs/mongo-scala-driver/org/mongodb/scala/MongoCollection.html#updateMany(filter:org.mongodb.scala.bson.conversions.Bson,update:org.mongodb.scala.bson.conversions.Bson):org.mongodb.scala.SingleObservable[org.mongodb.scala.result.UpdateResult]" 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=":R3ptlqcmqn9: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(#:R3ptlqcmqn9: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">
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.updateMany(filter, update, options)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.collection.updateMany" 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.updateMany">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Updates all documents that match the specified filter for a
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">
You can use
<code class="leafygreen-ui-1l06pbn">
db.collection.updateMany()
</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-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
updateMany()
</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.updateMany(\n <filter>,\n <update>,\n {\n upsert: <boolean>,\n writeConcern: <document>,\n collation: <document>,\n arrayFilters: [ <filterdocument1>, ... ],\n hint: <document|string>,\n let: <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">db.<span class="lg-highlight-property">collection</span>.<span class="lg-highlight-title lg-highlight-function">updateMany</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <<!-- -->filter<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- --><<!-- -->update<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">upsert</span>: <<!-- -->boolean<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">writeConcern</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"> <span class="lg-highlight-attr">arrayFilters</span>: <!-- -->[ <<!-- -->filterdocument1<!-- -->><!-- -->,<!-- --> <!-- -->...<!-- --> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">hint</span>: <!-- --><document|string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> let: <document></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">
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>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
updateMany()
</code>
</span>
</a>
method takes the following
parameters:
</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-update-many-filter" target="_self">
<span>
filter
</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-update-many-filter">
</span>
<p class="leafygreen-ui-1kp3ins">
The selection criteria for the update. The same
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/#std-label-query-selectors">
query
selectors
</a>
as in 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 are available.
</p>
<p class="leafygreen-ui-1kp3ins">
Specify an empty document
<code class="leafygreen-ui-1l06pbn">
{ }
</code>
to update all documents in
the 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-update-many-update" 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-update-many-update">
</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 class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-update-many-method-update-document" target="_self">
<span>
Update document
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-update-many-method-update-document">
</span>
<p class="leafygreen-ui-1kp3ins">
Contains only
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/#std-label-update-operators">
update operator expressions
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
For more information, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-updateMany-behavior-update-expressions" target="_self">
<span>
Update with an Update Operator Expressions Document
</span>
</a>
</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-update-many-method-agg-pipeline" target="_self">
<span>
Aggregation pipeline
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-update-many-method-agg-pipeline">
</span>
<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>
<p class="leafygreen-ui-1kp3ins">
For more information, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-updateMany-behavior-aggregation-pipeline" target="_self">
<span>
Update with an Aggregation Pipeline
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
To update with a replacement document, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.replaceOne/#mongodb-method-db.collection.replaceOne">
<code class="leafygreen-ui-1l06pbn">
db.collection.replaceOne()
</code>
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
upsert
</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. When
<code class="leafygreen-ui-1l06pbn">
true
</code>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
updateMany()
</code>
</span>
</a>
either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Creates a new document if no documents match the
<code class="leafygreen-ui-1l06pbn">
filter
</code>
.
For more details see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.update/#std-label-upsert-behavior">
upsert behavior
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Updates documents that match the
<code class="leafygreen-ui-1l06pbn">
filter
</code>
.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
To avoid multiple upserts, ensure that the
<code class="leafygreen-ui-1l06pbn">
filter
</code>
fields
are
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-unique/#std-label-index-type-unique">
uniquely indexed
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Defaults to
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
</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. A document expressing the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/">
write concern
</a>
. Omit to use the default write concern.
</p>
<p class="leafygreen-ui-1kp3ins">
Do not explicitly set the write concern for the operation if run in
a transaction. To use write concern with transactions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions-write-concern">
Transactions and Write Concern
<!-- -->
.
</a>
</p>
</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>
<p class="leafygreen-ui-1kp3ins">
Optional.
</p>
<p class="leafygreen-ui-1kp3ins">
Specifies the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
collation
</a>
to use for the operation.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
Collation
</a>
allows users to specify
language-specific rules for string comparison, such as rules for
lettercase and accent marks.
</p>
<p class="leafygreen-ui-1kp3ins">
The collation option has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"collation: {\n locale: <string>,\n caseLevel: <boolean>,\n caseFirst: <string>,\n strength: <int>,\n numericOrdering: <boolean>,\n alternate: <string>,\n maxVariable: <string>,\n backwards: <boolean>\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">collation: {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> locale: <string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> caseLevel: <boolean>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> caseFirst: <string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> strength: <int>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> numericOrdering: <boolean>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> alternate: <string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> maxVariable: <string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> backwards: <boolean></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 specifying collation, the
<code class="leafygreen-ui-1l06pbn">
locale
</code>
field is mandatory; all
other collation fields are optional. For descriptions of the fields,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation-document-fields">
Collation Document
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
If the collation is unspecified but the collection has a
default collation (see
<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>
), the
operation uses the collation specified for the collection.
</p>
<p class="leafygreen-ui-1kp3ins">
If no collation is specified for the collection or for the
operations, MongoDB uses the simple binary comparison used in prior
versions for string comparisons.
</p>
<p class="leafygreen-ui-1kp3ins">
You cannot specify multiple collations for an operation. For
example, you cannot specify different collations per field, or if
performing a find with a sort, you cannot use one collation for the
find and another for the sort.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<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>
array
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. An array of filter documents that determine which array elements to
modify for an update operation on an array field.
</p>
<p class="leafygreen-ui-1kp3ins">
In the update document, use the
<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>
filtered
positional operator to define an identifier, which you then reference
in the array filter documents. You cannot have an array filter
document for an identifier if the identifier is not included in the
update document.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
<identifier>
</code>
must begin with a lowercase letter and
contain only alphanumeric characters.
</p>
<p class="leafygreen-ui-1kp3ins">
You can include the same identifier multiple times in the update
document; however, for each distinct identifier (
<code class="leafygreen-ui-1l06pbn">
$[identifier]
</code>
)
in the update document, you must specify
<strong>
exactly one
</strong>
corresponding array filter document. That is, you cannot specify
multiple array filter documents for the same identifier. For
example, if the update statement includes the identifier
<code class="leafygreen-ui-1l06pbn">
x
</code>
(possibly multiple times), you cannot specify the following for
<code class="leafygreen-ui-1l06pbn">
arrayFilters
</code>
that includes 2 separate filter documents for
<code class="leafygreen-ui-1l06pbn">
x
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"// INVALID\n\n[\n { \"x.a\": { $gt: 85 } },\n { \"x.b\": { $gt: 80 } }\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"><span class="lg-highlight-comment">// INVALID</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">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x.a"</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">85</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x.b"</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></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
However, you can specify compound conditions on the same identifier
in a single filter document, such as in the following examples:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"// Example 1\n[\n { $or: [{\"x.a\": {$gt: 85}}, {\"x.b\": {$gt: 80}}] }\n]\n// Example 2\n[\n { $and: [{\"x.a\": {$gt: 85}}, {\"x.b\": {$gt: 80}}] }\n]\n// Example 3\n[\n { \"x.a\": { $gt: 85 }, \"x.b\": { $gt: 80 } }\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">// Example 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">$or</span>: <!-- -->[<!-- -->{<span class="lg-highlight-string">"x.a"</span>: <!-- -->{<span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">85</span>}}<!-- -->, <!-- -->{<span class="lg-highlight-string">"x.b"</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-comment">// Example 2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$and</span>: <!-- -->[<!-- -->{<span class="lg-highlight-string">"x.a"</span>: <!-- -->{<span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">85</span>}}<!-- -->, <!-- -->{<span class="lg-highlight-string">"x.b"</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-comment">// Example 3</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"x.a"</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">85</span> }<!-- -->, <span class="lg-highlight-string">"x.b"</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></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 examples, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-updateMany-arrayFilters" target="_self">
<span>
Specify
<code class="leafygreen-ui-1l06pbn">
arrayFilters
</code>
for an Array Update Operations
<!-- -->
.
</span>
</a>
</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-update-many-hint" target="_self">
<span>
hint
</span>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Document or string
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-update-many-hint">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. A document or string that specifies the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/indexes/#std-label-indexes">
index
</a>
to use to support the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-update-many-filter" target="_self">
<span>
query predicate
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The option can take an index specification document or the index
name string.
</p>
<p class="leafygreen-ui-1kp3ins">
If you specify an index that does not exist, the operation
errors.
</p>
<p class="leafygreen-ui-1kp3ins">
For an example, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-ex-update-many-hint" target="_self">
<span>
Specify
<code class="leafygreen-ui-1l06pbn">
hint
</code>
for Update Operations
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-updateMany-let-syntax">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional.
</p>
<p class="leafygreen-ui-1kp3ins">
Specifies a document with a list of variables. This allows you to
improve command readability by separating the variables from the query
text.
</p>
<p class="leafygreen-ui-1kp3ins">
The document syntax is:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n <variable_name_1>: <expression_1>,\n ...,\n <variable_name_n>: <expression_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"> <<!-- -->variable_name_1<!-- -->><!-- -->: <<!-- -->expression_1<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <<!-- -->variable_name_n<!-- -->><!-- -->: <<!-- -->expression_n<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The variable is set to the value returned by the expression, and cannot
be changed afterwards.
</p>
<p class="leafygreen-ui-1kp3ins">
To access the value of a variable in the command, use the double
dollar sign prefix (
<code class="leafygreen-ui-1l06pbn">
$$
</code>
) together with your variable name in the form
<code class="leafygreen-ui-1l06pbn">
$$<variable_name>
</code>
. For example:
<code class="leafygreen-ui-1l06pbn">
$$targetTotal
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
To use a variable to filter results, you must access the variable
within the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator.
</p>
<p class="leafygreen-ui-1kp3ins">
For a complete example using
<code class="leafygreen-ui-1l06pbn">
let
</code>
and variables,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/update-documents-with-aggregation-pipeline/#std-label-updateMany-let-example">
Update with let Variables
<!-- -->
.
</a>
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Returns
<a class="headerlink leafygreen-ui-1kepx47" href="#returns" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="returns">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The method returns a document that contains:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A boolean
<code class="leafygreen-ui-1l06pbn">
acknowledged
</code>
as
<code class="leafygreen-ui-1l06pbn">
true
</code>
if the operation ran with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-write-concern">
write concern
</a>
or
<code class="leafygreen-ui-1l06pbn">
false
</code>
if write concern was disabled
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
matchedCount
</code>
containing the number of matched documents
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
modifiedCount
</code>
containing the number of modified documents
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
upsertedId
</code>
containing the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
for the upserted document
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
upsertedCount
</code>
containing the number of upserted documents
</p>
</li>
</ul>
</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 deployments 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 access that includes the following privileges:
</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/privilege-actions/#mongodb-authaction-update">
<code class="leafygreen-ui-1l06pbn">
update
</code>
</a>
action on the specified collection(s).
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-find">
<code class="leafygreen-ui-1l06pbn">
find
</code>
</a>
action on the specified collection(s).
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-insert">
<code class="leafygreen-ui-1l06pbn">
insert
</code>
</a>
action on the specified collection(s) if the
operation results in an upsert.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The built-in role
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-readWrite">
<code class="leafygreen-ui-1l06pbn">
readWrite
</code>
</a>
provides the required
privileges.
</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-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
updateMany()
</code>
</span>
</a>
updates all matching documents in
the collection that match the
<code class="leafygreen-ui-1l06pbn">
filter
</code>
, using the
<code class="leafygreen-ui-1l06pbn">
update
</code>
criteria
to apply modifications.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Upsert
<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">
If
<code class="leafygreen-ui-1l06pbn">
upsert: true
</code>
and no documents match the
<code class="leafygreen-ui-1l06pbn">
filter
</code>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.updateMany()
</code>
</span>
</a>
creates a new
document based on the
<code class="leafygreen-ui-1l06pbn">
filter
</code>
and
<code class="leafygreen-ui-1l06pbn">
update
</code>
parameters.
</p>
<p class="leafygreen-ui-1kp3ins">
If you specify
<code class="leafygreen-ui-1l06pbn">
upsert: true
</code>
on a sharded collection, you must
include the full shard key in the
<code class="leafygreen-ui-1l06pbn">
filter
</code>
. For additional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.updateMany()
</code>
</span>
</a>
behavior, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-updateMany-sharded-collection" target="_self">
<span>
Sharded Collections
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-updateMany-example-update-multiple-documents-with-upsert" target="_self">
<span>
Update Multiple Documents with Upsert
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-updateMany-behavior-update-expressions">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Update with an Update Operator Expressions Document
<a class="headerlink leafygreen-ui-1kepx47" href="#update-with-an-update-operator-expressions-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="update-with-an-update-operator-expressions-document">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
For the modification specification, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.updateMany()
</code>
</span>
</a>
method can accept a document that
only contains
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/#std-label-update-operators">
update operator
</a>
expressions to
perform.
</p>
<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.updateMany(\n <query>,\n { $set: { status: \"D\" }, $inc: { quantity: 2 } },\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">db.<span class="lg-highlight-property">collection</span>.<span class="lg-highlight-title lg-highlight-function">updateMany</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <<!-- -->query<!-- -->><!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><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> }<!-- -->, <span class="lg-highlight-attr">$inc</span>: <!-- -->{ <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></tbody></table></code></pre>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-updateMany-behavior-aggregation-pipeline">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Update with an Aggregation Pipeline
<a class="headerlink leafygreen-ui-1kepx47" href="#update-with-an-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-an-aggregation-pipeline">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.updateMany()
</code>
</span>
</a>
method can accept an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
aggregation pipeline
</a>
<code class="leafygreen-ui-1l06pbn">
[ <stage1>, <stage2>, ... ]
</code>
that specifies the modifications to
perform. The pipeline can consist of the following 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>
<p class="leafygreen-ui-1kp3ins">
Using the aggregation pipeline allows for a more expressive update
statement, such as expressing conditional updates based on current
field values or updating one field using the value of another field(s).
</p>
<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.updateMany(\n <query>,\n [\n { $set: { status: \"Modified\", comments: [ \"$misc1\", \"$misc2\" ] } },\n { $unset: [ \"misc1\", \"misc2\" ] }\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">db.<span class="lg-highlight-property">collection</span>.<span class="lg-highlight-title lg-highlight-function">updateMany</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <<!-- -->query<!-- -->><!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"Modified"</span>, <span class="lg-highlight-attr">comments</span>: <!-- -->[ <span class="lg-highlight-string">"$misc1"</span>, <span class="lg-highlight-string">"$misc2"</span> ] } }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$unset</span>: <!-- -->[ <span class="lg-highlight-string">"misc1"</span>, <span class="lg-highlight-string">"misc2"</span> ] }</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>
</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">
In this pipeline,
<code class="leafygreen-ui-1l06pbn">
$set
</code>
and
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
are aggregation stages, as
opposed to update operators. The aggregation stages
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
add new fields to documents and do not modify
existing field values.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on the update operators, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/set/#mongodb-update-up.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/unset/#mongodb-update-up.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
.
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For examples, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-updateMany-example-agg" target="_self">
<span>
Update with Aggregation Pipeline
<!-- -->
.
</span>
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-updateMany-capped-collections">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Capped Collections
<a class="headerlink leafygreen-ui-1kepx47" href="#capped-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="capped-collections">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If an update operation changes the document size, the operation will fail.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-updateMany-timeseries-collection">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Time Series Collections
<a class="headerlink leafygreen-ui-1kepx47" href="#time-series-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="time-series-collections">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
updateMany()
</code>
</span>
</a>
method is available for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-time-series-collection">
time series collections
</a>
starting in
MongoDB 5.1.
</p>
<p class="leafygreen-ui-1kp3ins">
Update commands must meet the following requirements:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
You can only match on the
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
field value.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
You can only modify the
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
field value.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Your update document can only contain
<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">
Your update command must not limit the number of documents to be
updated. Set
<code class="leafygreen-ui-1l06pbn">
multi: true
</code>
or use the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
updateMany()
</code>
</span>
</a>
method.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Your update command must not set
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.update/#std-label-update-upsert">
upsert: true
<!-- -->
.
</a>
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-updateMany-sharded-collection">
</span>
</section>
<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">
For a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.updateMany()
</code>
</span>
</a>
operation that includes
<code class="leafygreen-ui-1l06pbn">
upsert: true
</code>
and is on a sharded collection, you must include the
full shard key in the
<code class="leafygreen-ui-1l06pbn">
filter
</code>
.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Explainability
<a class="headerlink leafygreen-ui-1kepx47" href="#explainability" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="explainability">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
updateMany()
</code>
</span>
</a>
is not compatible with
<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>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Transactions
<a class="headerlink leafygreen-ui-1kepx47" href="#transactions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="transactions">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.updateMany()
</code>
</span>
</a>
can be used inside
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
distributed 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>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Upsert within Transactions
<a class="headerlink leafygreen-ui-1kepx47" href="#upsert-within-transactions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-within-transactions">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
You can create collections and indexes inside a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions-create-collections-indexes">
distributed
transaction
</a>
if the
transaction is
<strong class="css-4dei7l">
not
</strong>
a cross-shard write transaction.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.updateMany()
</code>
</span>
</a>
with
<code class="leafygreen-ui-1l06pbn">
upsert: true
</code>
can be run on an existing
collection or a non-existing collection. If run on a non-existing
collection, the operation creates the 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/transactions/#std-label-transactions-create-collections-indexes">
Create Collections and Indexes in a Transaction
</a>
</p>
</div>
</div>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Write Concerns and Transactions
<a class="headerlink leafygreen-ui-1kepx47" href="#write-concerns-and-transactions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="write-concerns-and-transactions">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Do not explicitly set the write concern for the operation if run in
a transaction. To use write concern with transactions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions-write-concern">
Transactions and Write Concern
<!-- -->
.
</a>
</p>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Oplog Entries
<a class="headerlink leafygreen-ui-1kepx47" href="#oplog-entries" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="oplog-entries">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If a
<code class="leafygreen-ui-1l06pbn">
db.collection.updateMany()
</code>
operation successfully updates one
or more documents, the operation adds an entry on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-oplog">
oplog
</a>
(operations log) for each updated document. If the operation fails or
does not find any documents to update, the operation does not add an
entry on the oplog.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-updateMany-method-examples">
</span>
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-updateMany-example-update-multiple-documents">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Update Multiple Documents
<a class="headerlink leafygreen-ui-1kepx47" href="#update-multiple-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="update-multiple-documents">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
restaurant
</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, \"name\" : \"Central Perk Cafe\", \"violations\" : 3 }\n{ \"_id\" : 2, \"name\" : \"Rock A Feller Bar and Grill\", \"violations\" : 2 }\n{ \"_id\" : 3, \"name\" : \"Empire State Sub\", \"violations\" : 5 }\n{ \"_id\" : 4, \"name\" : \"Pizza Rat's Pizzaria\", \"violations\" : 8 }","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">"Central Perk Cafe"</span>, <span class="lg-highlight-string">"violations"</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">2</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Rock A Feller Bar and Grill"</span>, <span class="lg-highlight-string">"violations"</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>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Empire State Sub"</span>, <span class="lg-highlight-string">"violations"</span> <!-- -->: <span class="lg-highlight-number">5</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">"Pizza Rat's Pizzaria"</span>, <span class="lg-highlight-string">"violations"</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">
The following operation updates all documents where
<code class="leafygreen-ui-1l06pbn">
violations
</code>
are
greater than
<code class="leafygreen-ui-1l06pbn">
4
</code>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/set/#mongodb-update-up.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
a flag for review:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"try {\n db.restaurant.updateMany(\n { violations: { $gt: 4 } },\n { $set: { \"Review\" : true } }\n );\n} catch (e) {\n print(e);\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">try</span> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->db.<span class="lg-highlight-property">restaurant</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">violations</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">4</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-string">"Review"</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">} <span class="lg-highlight-keyword">catch</span> <!-- -->(<!-- -->e) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">print</span>(<!-- -->e)<!-- -->;</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:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"acknowledged\" : true, \"matchedCount\" : 2, \"modifiedCount\" : 2 }","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">"acknowledged"</span> <!-- -->: <span class="lg-highlight-literal">true</span>, <span class="lg-highlight-string">"matchedCount"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"modifiedCount"</span> <!-- -->: <span class="lg-highlight-number">2</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 collection now 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\" : \"Central Perk Cafe\", \"violations\" : 3 }\n{ \"_id\" : 2, \"name\" : \"Rock A Feller Bar and Grill\", \"violations\" : 2 }\n{ \"_id\" : 3, \"name\" : \"Empire State Sub\", \"violations\" : 5, \"Review\" : true }\n{ \"_id\" : 4, \"name\" : \"Pizza Rat's Pizzaria\", \"violations\" : 8, \"Review\" : 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">"name"</span> <!-- -->: <span class="lg-highlight-string">"Central Perk Cafe"</span>, <span class="lg-highlight-string">"violations"</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">2</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Rock A Feller Bar and Grill"</span>, <span class="lg-highlight-string">"violations"</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>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Empire State Sub"</span>, <span class="lg-highlight-string">"violations"</span> <!-- -->: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-string">"Review"</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">4</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Pizza Rat's Pizzaria"</span>, <span class="lg-highlight-string">"violations"</span> <!-- -->: <span class="lg-highlight-number">8</span>, <span class="lg-highlight-string">"Review"</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">
If no matches were found, the operation instead returns:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"acknowledged\" : true, \"matchedCount\" : 0, \"modifiedCount\" : 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">{ <span class="lg-highlight-string">"acknowledged"</span> <!-- -->: <span class="lg-highlight-literal">true</span>, <span class="lg-highlight-string">"matchedCount"</span> <!-- -->: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-string">"modifiedCount"</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">
Setting
<code class="leafygreen-ui-1l06pbn">
upsert: true
</code>
would insert a document if no match was found.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-updateMany-example-agg">
</span>
</section>
<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">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.updateMany()
</code>
</span>
</a>
can use an aggregation pipeline for the
update. The pipeline can consist of the following 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>
<p class="leafygreen-ui-1kp3ins">
Using the aggregation pipeline allows for a more expressive update
statement, such as expressing conditional updates based on current
field values or updating one field using the value of another field(s).
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Example 1: Update with Aggregation Pipeline Using Existing Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#example-1--update-with-aggregation-pipeline-using-existing-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="example-1--update-with-aggregation-pipeline-using-existing-fields">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following examples uses the aggregation pipeline to modify a field
using the values of the other fields in the document.
</p>
<p class="leafygreen-ui-1kp3ins">
Create a
<code class="leafygreen-ui-1l06pbn">
students
</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.students.insertMany( [\n { \"_id\" : 1, \"student\" : \"Skye\", \"points\" : 75, \"commentsSemester1\" : \"great at math\", \"commentsSemester2\" : \"loses temper\", \"lastUpdate\" : ISODate(\"2019-01-01T00:00:00Z\") },\n { \"_id\" : 2, \"students\" : \"Elizabeth\", \"points\" : 60, \"commentsSemester1\" : \"well behaved\", \"commentsSemester2\" : \"needs improvement\", \"lastUpdate\" : ISODate(\"2019-01-01T00:00:00Z\") }\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">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">"student"</span> <!-- -->: <span class="lg-highlight-string">"Skye"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-string">"commentsSemester1"</span> <!-- -->: <span class="lg-highlight-string">"great at math"</span>, <span class="lg-highlight-string">"commentsSemester2"</span> <!-- -->: <span class="lg-highlight-string">"loses temper"</span>, <span class="lg-highlight-string">"lastUpdate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2019-01-01T00:00:00Z"</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">"students"</span> <!-- -->: <span class="lg-highlight-string">"Elizabeth"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->: <span class="lg-highlight-number">60</span>, <span class="lg-highlight-string">"commentsSemester1"</span> <!-- -->: <span class="lg-highlight-string">"well behaved"</span>, <span class="lg-highlight-string">"commentsSemester2"</span> <!-- -->: <span class="lg-highlight-string">"needs improvement"</span>, <span class="lg-highlight-string">"lastUpdate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2019-01-01T00:00:00Z"</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">
Assume that instead of separate
<code class="leafygreen-ui-1l06pbn">
commentsSemester1
</code>
and
<code class="leafygreen-ui-1l06pbn">
commentsSemester2
</code>
fields, you want to gather these into a new
<code class="leafygreen-ui-1l06pbn">
comments
</code>
field. The following
update operation uses an aggregation pipeline to:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
add the new
<code class="leafygreen-ui-1l06pbn">
comments
</code>
field and set the
<code class="leafygreen-ui-1l06pbn">
lastUpdate
</code>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
remove the
<code class="leafygreen-ui-1l06pbn">
commentsSemester1
</code>
and
<code class="leafygreen-ui-1l06pbn">
commentsSemester2
</code>
fields for all
documents in the collection.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students.updateMany(\n { },\n [\n { $set: { comments: [ \"$commentsSemester1\", \"$commentsSemester2\" ], lastUpdate: \"$$NOW\" } },\n { $unset: [ \"commentsSemester1\", \"commentsSemester2\" ] }\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">students</span>.<span class="lg-highlight-title lg-highlight-function">updateMany</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">$set</span>: <!-- -->{ <span class="lg-highlight-attr">comments</span>: <!-- -->[ <span class="lg-highlight-string">"$commentsSemester1"</span>, <span class="lg-highlight-string">"$commentsSemester2"</span> ]<!-- -->, <span class="lg-highlight-attr">lastUpdate</span>: <span class="lg-highlight-string">"$$NOW"</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$unset</span>: <!-- -->[ <span class="lg-highlight-string">"commentsSemester1"</span>, <span class="lg-highlight-string">"commentsSemester2"</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>
<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 this pipeline,
<code class="leafygreen-ui-1l06pbn">
$set
</code>
and
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
are aggregation stages, as
opposed to update operators. The aggregation stages
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
add new fields to documents and do not modify
existing field values.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on the update operators, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/set/#mongodb-update-up.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/unset/#mongodb-update-up.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
.
</a>
</p>
</div>
</div>
<dl>
<dt>
First Stage
</dt>
<dd class="css-1ufxuu7">
<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:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
creates a new array field
<code class="leafygreen-ui-1l06pbn">
comments
</code>
whose elements are the current
content of the
<code class="leafygreen-ui-1l06pbn">
commentsSemester1
</code>
and
<code class="leafygreen-ui-1l06pbn">
commentsSemester2
</code>
fields and
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
sets the field
<code class="leafygreen-ui-1l06pbn">
lastUpdate
</code>
to the value of 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>
. 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>
resolves to the current datetime value and remains
the same throughout the pipeline. To access aggregation
variables, prefix the variable with double dollar signs
<code class="leafygreen-ui-1l06pbn">
$$
</code>
and enclose in quotes.
</p>
</li>
</ul>
</dd>
<dt>
Second Stage
</dt>
<dd class="css-1ufxuu7">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
stage removes the
<code class="leafygreen-ui-1l06pbn">
commentsSemester1
</code>
and
<code class="leafygreen-ui-1l06pbn">
commentsSemester2
</code>
fields.
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
After the command, the 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, \"student\" : \"Skye\", \"status\" : \"Modified\", \"points\" : 75, \"lastUpdate\" : ISODate(\"2020-01-23T05:11:45.784Z\"), \"comments\" : [ \"great at math\", \"loses temper\" ] }\n{ \"_id\" : 2, \"student\" : \"Elizabeth\", \"status\" : \"Modified\", \"points\" : 60, \"lastUpdate\" : ISODate(\"2020-01-23T05:11:45.784Z\"), \"comments\" : [ \"well behaved\", \"needs improvement\" ] }","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">"student"</span> <!-- -->: <span class="lg-highlight-string">"Skye"</span>, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"Modified"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-string">"lastUpdate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-01-23T05:11:45.784Z"</span>)<!-- -->, <span class="lg-highlight-string">"comments"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"great at math"</span>, <span class="lg-highlight-string">"loses temper"</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">"student"</span> <!-- -->: <span class="lg-highlight-string">"Elizabeth"</span>, <span class="lg-highlight-string">"status"</span> <!-- -->: <span class="lg-highlight-string">"Modified"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->: <span class="lg-highlight-number">60</span>, <span class="lg-highlight-string">"lastUpdate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-01-23T05:11:45.784Z"</span>)<!-- -->, <span class="lg-highlight-string">"comments"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"well behaved"</span>, <span class="lg-highlight-string">"needs improvement"</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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Example 2: Update with Aggregation Pipeline Using Existing Fields Conditionally
<a class="headerlink leafygreen-ui-1kepx47" href="#example-2--update-with-aggregation-pipeline-using-existing-fields-conditionally" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-2--update-with-aggregation-pipeline-using-existing-fields-conditionally">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The aggregation pipeline allows the update to perform conditional
updates based on the current field values as well as use current field
values to calculate a separate field value.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, create a
<code class="leafygreen-ui-1l06pbn">
students3
</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.students3.insertMany( [\n { \"_id\" : 1, \"tests\" : [ 95, 92, 90 ], \"lastUpdate\" : ISODate(\"2019-01-01T00:00:00Z\") },\n { \"_id\" : 2, \"tests\" : [ 94, 88, 90 ], \"lastUpdate\" : ISODate(\"2019-01-01T00:00:00Z\") },\n { \"_id\" : 3, \"tests\" : [ 70, 75, 82 ], \"lastUpdate\" : ISODate(\"2019-01-01T00:00:00Z\") }\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">students3</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">"tests"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">95</span>, <span class="lg-highlight-number">92</span>, <span class="lg-highlight-number">90</span> ]<!-- -->, <span class="lg-highlight-string">"lastUpdate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2019-01-01T00:00:00Z"</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">"tests"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">94</span>, <span class="lg-highlight-number">88</span>, <span class="lg-highlight-number">90</span> ]<!-- -->, <span class="lg-highlight-string">"lastUpdate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2019-01-01T00:00:00Z"</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">"tests"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">70</span>, <span class="lg-highlight-number">75</span>, <span class="lg-highlight-number">82</span> ]<!-- -->, <span class="lg-highlight-string">"lastUpdate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2019-01-01T00:00:00Z"</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">
Using an aggregation pipeline, you can update the documents with the
calculated grade average and letter grade.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students3.updateMany(\n { },\n [\n { $set: { average : { $trunc: [ { $avg: \"$tests\" }, 0 ] } , lastUpdate: \"$$NOW\" } },\n { $set: { grade: { $switch: {\n branches: [\n { case: { $gte: [ \"$average\", 90 ] }, then: \"A\" },\n { case: { $gte: [ \"$average\", 80 ] }, then: \"B\" },\n { case: { $gte: [ \"$average\", 70 ] }, then: \"C\" },\n { case: { $gte: [ \"$average\", 60 ] }, then: \"D\" }\n ],\n default: \"F\"\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">students3</span>.<span class="lg-highlight-title lg-highlight-function">updateMany</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">$set</span>: <!-- -->{ <!-- -->average <!-- -->: <!-- -->{ <span class="lg-highlight-attr">$trunc</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$avg</span>: <span class="lg-highlight-string">"$tests"</span> }<!-- -->, <span class="lg-highlight-number">0</span> ] } <!-- -->, <span class="lg-highlight-attr">lastUpdate</span>: <span class="lg-highlight-string">"$$NOW"</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">grade</span>: <!-- -->{ <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">$gte</span>: <!-- -->[ <span class="lg-highlight-string">"$average"</span>, <span class="lg-highlight-number">90</span> ] }<!-- -->, <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"A"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">case</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <!-- -->[ <span class="lg-highlight-string">"$average"</span>, <span class="lg-highlight-number">80</span> ] }<!-- -->, <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"B"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">case</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <!-- -->[ <span class="lg-highlight-string">"$average"</span>, <span class="lg-highlight-number">70</span> ] }<!-- -->, <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"C"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">case</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <!-- -->[ <span class="lg-highlight-string">"$average"</span>, <span class="lg-highlight-number">60</span> ] }<!-- -->, <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"D"</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">"F"</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-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
In this pipeline,
<code class="leafygreen-ui-1l06pbn">
$set
</code>
and
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
are aggregation stages, as
opposed to update operators. The aggregation stages
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/unset/#mongodb-pipeline-pipe.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
add new fields to documents and do not modify
existing field values.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on the update operators, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/set/#mongodb-update-up.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/unset/#mongodb-update-up.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
.
</a>
</p>
</div>
</div>
<dl>
<dt>
First Stage
</dt>
<dd class="css-1ufxuu7">
<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:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
calculates a new field
<code class="leafygreen-ui-1l06pbn">
average
</code>
based on the average of the
<code class="leafygreen-ui-1l06pbn">
tests
</code>
field. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/avg/#mongodb-group-grp.-avg">
<code class="leafygreen-ui-1l06pbn">
$avg
</code>
</a>
for more information on the
<code class="leafygreen-ui-1l06pbn">
$avg
</code>
aggregation operator and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/trunc/#mongodb-expression-exp.-trunc">
<code class="leafygreen-ui-1l06pbn">
$trunc
</code>
</a>
for more
information on the
<code class="leafygreen-ui-1l06pbn">
$trunc
</code>
truncate aggregation operator.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
sets the field
<code class="leafygreen-ui-1l06pbn">
lastUpdate
</code>
to the value of 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>
. 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>
resolves to the current datetime value and remains
the same throughout the pipeline. To access aggregation
variables, prefix the variable with double dollar signs
<code class="leafygreen-ui-1l06pbn">
$$
</code>
and enclose in quotes.
</p>
</li>
</ul>
</dd>
<dt>
Second Stage
</dt>
<dd class="css-1ufxuu7">
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 calculates a new field
<code class="leafygreen-ui-1l06pbn">
grade
</code>
based on
the
<code class="leafygreen-ui-1l06pbn">
average
</code>
field calculated in the previous stage. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/switch/#mongodb-expression-exp.-switch">
<code class="leafygreen-ui-1l06pbn">
$switch
</code>
</a>
for more information on the
<code class="leafygreen-ui-1l06pbn">
$switch
</code>
aggregation operator.
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
After the command, the 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, \"tests\" : [ 95, 92, 90 ], \"lastUpdate\" : ISODate(\"2020-01-24T17:31:01.670Z\"), \"average\" : 92, \"grade\" : \"A\" }\n{ \"_id\" : 2, \"tests\" : [ 94, 88, 90 ], \"lastUpdate\" : ISODate(\"2020-01-24T17:31:01.670Z\"), \"average\" : 90, \"grade\" : \"A\" }\n{ \"_id\" : 3, \"tests\" : [ 70, 75, 82 ], \"lastUpdate\" : ISODate(\"2020-01-24T17:31:01.670Z\"), \"average\" : 75, \"grade\" : \"C\" }","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">"tests"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">95</span>, <span class="lg-highlight-number">92</span>, <span class="lg-highlight-number">90</span> ]<!-- -->, <span class="lg-highlight-string">"lastUpdate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-01-24T17:31:01.670Z"</span>)<!-- -->, <span class="lg-highlight-string">"average"</span> <!-- -->: <span class="lg-highlight-number">92</span>, <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-string">"A"</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">"tests"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">94</span>, <span class="lg-highlight-number">88</span>, <span class="lg-highlight-number">90</span> ]<!-- -->, <span class="lg-highlight-string">"lastUpdate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-01-24T17:31:01.670Z"</span>)<!-- -->, <span class="lg-highlight-string">"average"</span> <!-- -->: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-string">"A"</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">"tests"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">70</span>, <span class="lg-highlight-number">75</span>, <span class="lg-highlight-number">82</span> ]<!-- -->, <span class="lg-highlight-string">"lastUpdate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-01-24T17:31:01.670Z"</span>)<!-- -->, <span class="lg-highlight-string">"average"</span> <!-- -->: <span class="lg-highlight-number">75</span>, <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-string">"C"</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/tutorial/update-documents-with-aggregation-pipeline/">
Updates with Aggregation Pipeline
</a>
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-updateMany-example-update-multiple-documents-with-upsert">
</span>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Update Multiple Documents with Upsert
<a class="headerlink leafygreen-ui-1kepx47" href="#update-multiple-documents-with-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="update-multiple-documents-with-upsert">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
inspectors
</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\" : 92412, \"inspector\" : \"F. Drebin\", \"Sector\" : 1, \"Patrolling\" : true },\n{ \"_id\" : 92413, \"inspector\" : \"J. Clouseau\", \"Sector\" : 2, \"Patrolling\" : false },\n{ \"_id\" : 92414, \"inspector\" : \"J. Clouseau\", \"Sector\" : 3, \"Patrolling\" : true },\n{ \"_id\" : 92415, \"inspector\" : \"R. Coltrane\", \"Sector\" : 3, \"Patrolling\" : false }","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">92412</span>, <span class="lg-highlight-string">"inspector"</span> <!-- -->: <span class="lg-highlight-string">"F. Drebin"</span>, <span class="lg-highlight-string">"Sector"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"Patrolling"</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">92413</span>, <span class="lg-highlight-string">"inspector"</span> <!-- -->: <span class="lg-highlight-string">"J. Clouseau"</span>, <span class="lg-highlight-string">"Sector"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"Patrolling"</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">92414</span>, <span class="lg-highlight-string">"inspector"</span> <!-- -->: <span class="lg-highlight-string">"J. Clouseau"</span>, <span class="lg-highlight-string">"Sector"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"Patrolling"</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">92415</span>, <span class="lg-highlight-string">"inspector"</span> <!-- -->: <span class="lg-highlight-string">"R. Coltrane"</span>, <span class="lg-highlight-string">"Sector"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"Patrolling"</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>
<p class="leafygreen-ui-1kp3ins">
The following operation updates all documents with
<code class="leafygreen-ui-1l06pbn">
Sector
</code>
greater
than 4 and
<code class="leafygreen-ui-1l06pbn">
inspector
</code>
equal to
<code class="leafygreen-ui-1l06pbn">
"R. Coltrane"
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"try {\n db.inspectors.updateMany(\n { \"Sector\" : { $gt : 4 }, \"inspector\" : \"R. Coltrane\" },\n { $set: { \"Patrolling\" : false } },\n { upsert: true }\n );\n} catch (e) {\n print(e);\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">try</span> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->db.<span class="lg-highlight-property">inspectors</span>.<span class="lg-highlight-title lg-highlight-function">updateMany</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"Sector"</span> <!-- -->: <!-- -->{ <!-- -->$gt<!-- --> <!-- -->: <span class="lg-highlight-number">4</span> }<!-- -->, <span class="lg-highlight-string">"inspector"</span> <!-- -->: <span class="lg-highlight-string">"R. Coltrane"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-string">"Patrolling"</span> <!-- -->: <span class="lg-highlight-literal">false</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">upsert</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">} <span class="lg-highlight-keyword">catch</span> <!-- -->(<!-- -->e) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">print</span>(<!-- -->e)<!-- -->;</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:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"acknowledged\" : true,\n \"matchedCount\" : 0,\n \"modifiedCount\" : 0,\n \"upsertedId\" : ObjectId(\"56fc5dcb39ee682bdc609b02\"),\n \"upsertedCount\": 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">"acknowledged"</span> <!-- -->: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"matchedCount"</span> <!-- -->: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"modifiedCount"</span> <!-- -->: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"upsertedId"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"56fc5dcb39ee682bdc609b02"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"upsertedCount"</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 collection now contains the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 92412, \"inspector\" : \"F. Drebin\", \"Sector\" : 1, \"Patrolling\" : true },\n{ \"_id\" : 92413, \"inspector\" : \"J. Clouseau\", \"Sector\" : 2, \"Patrolling\" : false },\n{ \"_id\" : 92414, \"inspector\" : \"J. Clouseau\", \"Sector\" : 3, \"Patrolling\" : true },\n{ \"_id\" : 92415, \"inspector\" : \"R. Coltrane\", \"Sector\" : 3, \"Patrolling\" : false },\n{ \"_id\" : ObjectId(\"56fc5dcb39ee682bdc609b02\"), \"inspector\" : \"R. Coltrane\", \"Patrolling\" : false }","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">92412</span>, <span class="lg-highlight-string">"inspector"</span> <!-- -->: <span class="lg-highlight-string">"F. Drebin"</span>, <span class="lg-highlight-string">"Sector"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"Patrolling"</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">92413</span>, <span class="lg-highlight-string">"inspector"</span> <!-- -->: <span class="lg-highlight-string">"J. Clouseau"</span>, <span class="lg-highlight-string">"Sector"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"Patrolling"</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">92414</span>, <span class="lg-highlight-string">"inspector"</span> <!-- -->: <span class="lg-highlight-string">"J. Clouseau"</span>, <span class="lg-highlight-string">"Sector"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"Patrolling"</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">92415</span>, <span class="lg-highlight-string">"inspector"</span> <!-- -->: <span class="lg-highlight-string">"R. Coltrane"</span>, <span class="lg-highlight-string">"Sector"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"Patrolling"</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-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"56fc5dcb39ee682bdc609b02"</span>)<!-- -->, <span class="lg-highlight-string">"inspector"</span> <!-- -->: <span class="lg-highlight-string">"R. Coltrane"</span>, <span class="lg-highlight-string">"Patrolling"</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>
<p class="leafygreen-ui-1kp3ins">
Since no documents matched the filter, and
<code class="leafygreen-ui-1l06pbn">
upsert
</code>
was
<code class="leafygreen-ui-1l06pbn">
true
</code>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.updateMany" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
updateMany()
</code>
</span>
</a>
inserted the document with a
generated
<code class="leafygreen-ui-1l06pbn">
_id
</code>
, the equality conditions from the
<code class="leafygreen-ui-1l06pbn">
filter
</code>
, and the
<code class="leafygreen-ui-1l06pbn">
update
</code>
modifiers.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-updateMany-example-update-with-write-concern">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Update with Write Concern
<a class="headerlink leafygreen-ui-1kepx47" href="#update-with-write-concern" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-write-concern">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Given a three member replica set, the following operation specifies a
<code class="leafygreen-ui-1l06pbn">
w
</code>
of
<code class="leafygreen-ui-1l06pbn">
majority
</code>
and
<code class="leafygreen-ui-1l06pbn">
wtimeout
</code>
of
<code class="leafygreen-ui-1l06pbn">
100
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"try {\n db.restaurant.updateMany(\n { \"name\" : \"Pizza Rat's Pizzaria\" },\n { $inc: { \"violations\" : 3}, $set: { \"Closed\" : true } },\n { w: \"majority\", wtimeout: 100 }\n );\n} catch (e) {\n print(e);\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">try</span> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->db.<span class="lg-highlight-property">restaurant</span>.<span class="lg-highlight-title lg-highlight-function">updateMany</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Pizza Rat's Pizzaria"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$inc</span>: <!-- -->{ <span class="lg-highlight-string">"violations"</span> <!-- -->: <span class="lg-highlight-number">3</span>}<!-- -->, <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-string">"Closed"</span> <!-- -->: <span class="lg-highlight-literal">true</span> } }<!-- -->,</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">100</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> <!-- -->(<!-- -->e) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">print</span>(<!-- -->e)<!-- -->;</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">
If the acknowledgment takes longer than the
<code class="leafygreen-ui-1l06pbn">
wtimeout
</code>
limit, the following
exception is thrown:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"WriteConcernError({\n \"code\" : 64,\n \"errmsg\" : \"waiting for replication timed out\",\n \"errInfo\" : {\n \"wtimeout\" : true,\n \"writeConcern\" : {\n \"w\" : \"majority\",\n \"wtimeout\" : 100,\n \"provenance\" : \"getLastErrorDefaults\"\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-title lg-highlight-class">WriteConcernError</span>(<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"code"</span> <!-- -->: <span class="lg-highlight-number">64</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"errmsg"</span> <!-- -->: <span class="lg-highlight-string">"waiting for replication timed out"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"errInfo"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"wtimeout"</span> <!-- -->: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"writeConcern"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"w"</span> <!-- -->: <span class="lg-highlight-string">"majority"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"wtimeout"</span> <!-- -->: <span class="lg-highlight-number">100</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"provenance"</span> <!-- -->: <span class="lg-highlight-string">"getLastErrorDefaults"</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 table explains the possible values of
<code class="leafygreen-ui-1l06pbn">
errInfo.writeConcern.provenance
</code>
:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<col style="width:40%"/>
</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>
Provenance
</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">
clientSupplied
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The write concern was specified in the application.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
customDefault
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The write concern originated from a custom defined
default value. See
<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>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
getLastErrorDefaults
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The write concern originated from the replica set's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.settings.getLastErrorDefaults">
<code class="leafygreen-ui-1l06pbn">
settings.getLastErrorDefaults
</code>
</a>
field.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
implicitDefault
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The write concern originated from the server in absence
of all other write concern specifications.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Specify Collation
<a class="headerlink leafygreen-ui-1kepx47" href="#specify-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="specify-collation">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
Collation
</a>
allows users to specify
language-specific rules for string comparison, such as rules for
lettercase and accent marks.
</p>
<p class="leafygreen-ui-1kp3ins">
A collection
<code class="leafygreen-ui-1l06pbn">
myColl
</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, category: \"café\", status: \"A\" }\n{ _id: 2, category: \"cafe\", status: \"a\" }\n{ _id: 3, category: \"cafE\", status: \"a\" }","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">1</span>, <span class="lg-highlight-attr">category</span>: <span class="lg-highlight-string">"café"</span>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"A"</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>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"a"</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>, <span class="lg-highlight-attr">status</span>: <span class="lg-highlight-string">"a"</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 operation includes the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
collation
</a>
option:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.myColl.updateMany(\n { category: \"cafe\" },\n { $set: { status: \"Updated\" } },\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-property">myColl</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">category</span>: <span class="lg-highlight-string">"cafe"</span> }<!-- -->,</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">"Updated"</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-updateMany-arrayFilters">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Specify
<code class="leafygreen-ui-1l06pbn">
arrayFilters
</code>
for an Array Update Operations
<a class="headerlink leafygreen-ui-1kepx47" href="#specify-arrayfilters-for-an-array-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="specify-arrayfilters-for-an-array-update-operations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When updating an array field, you can specify
<code class="leafygreen-ui-1l06pbn">
arrayFilters
</code>
that
determine which array elements to update.
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Update Elements Match
<code class="leafygreen-ui-1l06pbn">
arrayFilters
</code>
Criteria
<a class="headerlink leafygreen-ui-1kepx47" href="#update-elements-match-arrayfilters-criteria" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-elements-match-arrayfilters-criteria">
</div>
</a>
</h4>
<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\" : [ 95, 92, 90 ] },\n { \"_id\" : 2, \"grades\" : [ 98, 100, 102 ] },\n { \"_id\" : 3, \"grades\" : [ 95, 110, 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">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">95</span>, <span class="lg-highlight-number">92</span>, <span class="lg-highlight-number">90</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">98</span>, <span class="lg-highlight-number">100</span>, <span class="lg-highlight-number">102</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">95</span>, <span class="lg-highlight-number">110</span>, <span class="lg-highlight-number">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">
To update all elements that are greater than or equal to
<code class="leafygreen-ui-1l06pbn">
100
</code>
in the
<code class="leafygreen-ui-1l06pbn">
grades
</code>
array, 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>
with the
<code class="leafygreen-ui-1l06pbn">
arrayFilters
</code>
option:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students.updateMany(\n { grades: { $gte: 100 } },\n { $set: { \"grades.$[element]\" : 100 } },\n { arrayFilters: [ { \"element\": { $gte: 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">students</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">grades</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-number">100</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-string">"grades.$[element]"</span> <!-- -->: <span class="lg-highlight-number">100</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">arrayFilters</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"element"</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-number">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">
After the operation, the 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\" : [ 95, 92, 90 ] }\n{ \"_id\" : 2, \"grades\" : [ 98, 100, 100 ] }\n{ \"_id\" : 3, \"grades\" : [ 95, 100, 100 ] }","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">95</span>, <span class="lg-highlight-number">92</span>, <span class="lg-highlight-number">90</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">"grades"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">98</span>, <span class="lg-highlight-number">100</span>, <span class="lg-highlight-number">100</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">3</span>, <span class="lg-highlight-string">"grades"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-number">95</span>, <span class="lg-highlight-number">100</span>, <span class="lg-highlight-number">100</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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Update Specific Elements of an Array of Documents
<a class="headerlink leafygreen-ui-1kepx47" href="#update-specific-elements-of-an-array-of-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="update-specific-elements-of-an-array-of-documents">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Create a collection
<code class="leafygreen-ui-1l06pbn">
students2
</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.students2.insertMany( [\n {\n \"_id\" : 1,\n \"grades\" : [\n { \"grade\" : 80, \"mean\" : 75, \"std\" : 6 },\n { \"grade\" : 85, \"mean\" : 90, \"std\" : 4 },\n { \"grade\" : 85, \"mean\" : 85, \"std\" : 6 }\n ]\n },\n {\n \"_id\" : 2,\n \"grades\" : [\n { \"grade\" : 90, \"mean\" : 75, \"std\" : 6 },\n { \"grade\" : 87, \"mean\" : 90, \"std\" : 3 },\n { \"grade\" : 85, \"mean\" : 85, \"std\" : 4 }\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">students2</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-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</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">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">90</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">4</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">6</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">"grades"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">90</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">6</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">87</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">3</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">4</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">
To modify the value of the
<code class="leafygreen-ui-1l06pbn">
mean
</code>
field for all elements in the
<code class="leafygreen-ui-1l06pbn">
grades
</code>
array where the grade is greater than or equal to
<code class="leafygreen-ui-1l06pbn">
85
</code>
,
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>
with
the
<code class="leafygreen-ui-1l06pbn">
arrayFilters
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students2.updateMany(\n { },\n { $set: { \"grades.$[elem].mean\" : 100 } },\n { arrayFilters: [ { \"elem.grade\": { $gte: 85 } } ] }\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">students2</span>.<span class="lg-highlight-title lg-highlight-function">updateMany</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.$[elem].mean"</span> <!-- -->: <span class="lg-highlight-number">100</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">arrayFilters</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-string">"elem.grade"</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-number">85</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 collection has 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 \"grades\" : [\n { \"grade\" : 80, \"mean\" : 75, \"std\" : 6 },\n { \"grade\" : 85, \"mean\" : 100, \"std\" : 4 },\n { \"grade\" : 85, \"mean\" : 100, \"std\" : 6 }\n ]\n}\n{\n \"_id\" : 2,\n \"grades\" : [\n { \"grade\" : 90, \"mean\" : 100, \"std\" : 6 },\n { \"grade\" : 87, \"mean\" : 100, \"std\" : 3 },\n { \"grade\" : 85, \"mean\" : 100, \"std\" : 4 }\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">"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">6</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><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">100</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">4</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><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">100</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">6</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">"grades"</span> <!-- -->: <!-- -->[</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">6</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-number">87</span>, <span class="lg-highlight-string">"mean"</span> <!-- -->: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">3</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><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">100</span>, <span class="lg-highlight-string">"std"</span> <!-- -->: <span class="lg-highlight-number">4</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-ex-update-many-hint">
</span>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Specify
<code class="leafygreen-ui-1l06pbn">
hint
</code>
for Update Operations
<a class="headerlink leafygreen-ui-1kepx47" href="#specify-hint-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="specify-hint-for-update-operations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Create a sample
<code class="leafygreen-ui-1l06pbn">
students
</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.students.insertMany( [\n { \"_id\" : 1, \"student\" : \"Richard\", \"grade\" : \"F\", \"points\" : 0, \"comments1\" : null, \"comments2\" : null },\n { \"_id\" : 2, \"student\" : \"Jane\", \"grade\" : \"A\", \"points\" : 60, \"comments1\" : \"well behaved\", \"comments2\" : \"fantastic student\" },\n { \"_id\" : 3, \"student\" : \"Ronan\", \"grade\" : \"F\", \"points\" : 0, \"comments1\" : null, \"comments2\" : null },\n { \"_id\" : 4, \"student\" : \"Noah\", \"grade\" : \"D\", \"points\" : 20, \"comments1\" : \"needs improvement\", \"comments2\" : null },\n { \"_id\" : 5, \"student\" : \"Adam\", \"grade\" : \"F\", \"points\" : 0, \"comments1\" : null, \"comments2\" : null },\n { \"_id\" : 6, \"student\" : \"Henry\", \"grade\" : \"A\", \"points\" : 86, \"comments1\" : \"fantastic student\", \"comments2\" : \"well behaved\" }\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">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">"student"</span> <!-- -->: <span class="lg-highlight-string">"Richard"</span>, <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-string">"F"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-string">"comments1"</span> <!-- -->: <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-string">"comments2"</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-number">2</span>, <span class="lg-highlight-string">"student"</span> <!-- -->: <span class="lg-highlight-string">"Jane"</span>, <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->: <span class="lg-highlight-number">60</span>, <span class="lg-highlight-string">"comments1"</span> <!-- -->: <span class="lg-highlight-string">"well behaved"</span>, <span class="lg-highlight-string">"comments2"</span> <!-- -->: <span class="lg-highlight-string">"fantastic student"</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">"student"</span> <!-- -->: <span class="lg-highlight-string">"Ronan"</span>, <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-string">"F"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-string">"comments1"</span> <!-- -->: <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-string">"comments2"</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-number">4</span>, <span class="lg-highlight-string">"student"</span> <!-- -->: <span class="lg-highlight-string">"Noah"</span>, <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-string">"D"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->: <span class="lg-highlight-number">20</span>, <span class="lg-highlight-string">"comments1"</span> <!-- -->: <span class="lg-highlight-string">"needs improvement"</span>, <span class="lg-highlight-string">"comments2"</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-number">5</span>, <span class="lg-highlight-string">"student"</span> <!-- -->: <span class="lg-highlight-string">"Adam"</span>, <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-string">"F"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-string">"comments1"</span> <!-- -->: <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-string">"comments2"</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-number">6</span>, <span class="lg-highlight-string">"student"</span> <!-- -->: <span class="lg-highlight-string">"Henry"</span>, <span class="lg-highlight-string">"grade"</span> <!-- -->: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-string">"points"</span> <!-- -->: <span class="lg-highlight-number">86</span>, <span class="lg-highlight-string">"comments1"</span> <!-- -->: <span class="lg-highlight-string">"fantastic student"</span>, <span class="lg-highlight-string">"comments2"</span> <!-- -->: <span class="lg-highlight-string">"well behaved"</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">
Create the following indexes on the collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students.createIndex( { grade: 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">students</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <span class="lg-highlight-attr">grade</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 following update operation explicitly hints to use the index
<code class="leafygreen-ui-1l06pbn">
{
grade: 1 }
</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">
If you specify an index that does not exist, the operation errors.
</p>
</div>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students.updateMany(\n { \"points\": { $lte: 20 }, \"grade\": \"F\" },\n { $set: { \"comments1\": \"failed class\" } },\n { hint: { grade: 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-property">students</span>.<span class="lg-highlight-title lg-highlight-function">updateMany</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"points"</span>: <!-- -->{ <span class="lg-highlight-attr">$lte</span>: <span class="lg-highlight-number">20</span> }<!-- -->, <span class="lg-highlight-string">"grade"</span>: <span class="lg-highlight-string">"F"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-string">"comments1"</span>: <span class="lg-highlight-string">"failed class"</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">hint</span>: <!-- -->{ <span class="lg-highlight-attr">grade</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 update command returns the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"acknowledged\" : true, \"matchedCount\" : 3, \"modifiedCount\" : 3 }","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">"acknowledged"</span> <!-- -->: <span class="lg-highlight-literal">true</span>, <span class="lg-highlight-string">"matchedCount"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"modifiedCount"</span> <!-- -->: <span class="lg-highlight-number">3</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 see if the hinted index is used, run the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/indexStats/#mongodb-pipeline-pipe.-indexStats">
<code class="leafygreen-ui-1l06pbn">
$indexStats
</code>
</a>
pipeline:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students.aggregate( [ { $indexStats: { } }, { $sort: { name: 1 } }, { $match: {key: { grade: 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">students</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$indexStats</span>: <!-- -->{ <!-- -->} }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">$sort</span>: <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-number">1</span> } }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">$match</span>: <!-- -->{<span class="lg-highlight-attr">key</span>: <!-- -->{ <span class="lg-highlight-attr">grade</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-updateMany-example-user-roles-system-variable">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
User Roles and Document Updates
<a class="headerlink leafygreen-ui-1kepx47" href="#user-roles-and-document-updates" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-roles-and-document-updates">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 7.0, you can use the new
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/aggregation-variables/#mongodb-variable-variable.USER_ROLES">
<code class="leafygreen-ui-1l06pbn">
USER_ROLES
</code>
</a>
system variable to return user
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-roles">
roles
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The example in this section shows updates to fields in a collection
containing medical information. The example reads the current user roles
from the
<code class="leafygreen-ui-1l06pbn">
USER_ROLES
</code>
system variable and only performs the updates if
the user has a specific role.
</p>
<p class="leafygreen-ui-1kp3ins">
To use a system variable, add
<code class="leafygreen-ui-1l06pbn">
$$
</code>
to the start of the variable name.
Specify the
<code class="leafygreen-ui-1l06pbn">
USER_ROLES
</code>
system variable as
<code class="leafygreen-ui-1l06pbn">
$$USER_ROLES
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
The example creates these users:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
James
</code>
with a
<code class="leafygreen-ui-1l06pbn">
Billing
</code>
role.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
Michelle
</code>
with a
<code class="leafygreen-ui-1l06pbn">
Provider
</code>
role.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Perform the following steps to create the roles, users, and collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Create roles named Billing and Provider with the required\nprivileges and resources.Run:","name":"Create the roles"},{"@type":"HowToStep","text":"Create users named James and Michelle with the required\nroles.","name":"Create the users"},{"@type":"HowToStep","text":"Run:","name":"Create the collection"}],"name":"User Roles and Document Updates","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 roles
<a class="headerlink leafygreen-ui-1kepx47" href="#create-the-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="create-the-roles">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Create roles named
<code class="leafygreen-ui-1l06pbn">
Billing
</code>
and
<code class="leafygreen-ui-1l06pbn">
Provider
</code>
with the required
privileges and resources.
</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":"db.createRole( { role: \"Billing\", privileges: [ { resource: { db: \"test\",\n collection: \"medicalView\" }, actions: [ \"find\" ] } ], roles: [ ] } )\ndb.createRole( { role: \"Provider\", privileges: [ { resource: { db: \"test\",\n collection: \"medicalView\" }, actions: [ \"find\" ] } ], roles: [ ] } )","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">createRole</span>( <!-- -->{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"Billing"</span>, <span class="lg-highlight-attr">privileges</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">resource</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">collection</span>: <span class="lg-highlight-string">"medicalView"</span> }<!-- -->, <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"find"</span> ] } ]<!-- -->, <span class="lg-highlight-attr">roles</span>: <!-- -->[ <!-- -->] } )</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">createRole</span>( <!-- -->{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"Provider"</span>, <span class="lg-highlight-attr">privileges</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">resource</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">collection</span>: <span class="lg-highlight-string">"medicalView"</span> }<!-- -->, <span class="lg-highlight-attr">actions</span>: <!-- -->[ <span class="lg-highlight-string">"find"</span> ] } ]<!-- -->, <span class="lg-highlight-attr">roles</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">
Create the users
<a class="headerlink leafygreen-ui-1kepx47" href="#create-the-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="create-the-users">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Create users named
<code class="leafygreen-ui-1l06pbn">
James
</code>
and
<code class="leafygreen-ui-1l06pbn">
Michelle
</code>
with the required
roles.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.createUser( {\n user: \"James\",\n pwd: \"js008\",\n roles: [\n { role: \"Billing\", db: \"test\" }\n ]\n} )\n\ndb.createUser( {\n user: \"Michelle\",\n pwd: \"me009\",\n roles: [\n { role: \"Provider\", db: \"test\" }\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">createUser</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">user</span>: <span class="lg-highlight-string">"James"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pwd</span>: <span class="lg-highlight-string">"js008"</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">"Billing"</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><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">createUser</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">user</span>: <span class="lg-highlight-string">"Michelle"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pwd</span>: <span class="lg-highlight-string">"me009"</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">"Provider"</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><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">
Create the collection
<a class="headerlink leafygreen-ui-1kepx47" href="#create-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="create-the-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.medical.insertMany( [\n {\n _id: 0,\n patientName: \"Jack Jones\",\n diagnosisCode: \"CAS 17\",\n creditCard: \"1234-5678-9012-3456\"\n },\n {\n _id: 1,\n patientName: \"Mary Smith\",\n diagnosisCode: \"ACH 01\",\n creditCard: \"6541-7534-9637-3456\"\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">medical</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">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">patientName</span>: <span class="lg-highlight-string">"Jack Jones"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">diagnosisCode</span>: <span class="lg-highlight-string">"CAS 17"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">creditCard</span>: <span class="lg-highlight-string">"1234-5678-9012-3456"</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">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">patientName</span>: <span class="lg-highlight-string">"Mary Smith"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">diagnosisCode</span>: <span class="lg-highlight-string">"ACH 01"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">creditCard</span>: <span class="lg-highlight-string">"6541-7534-9637-3456"</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>
<p class="leafygreen-ui-1kp3ins">
Log in as as
<code class="leafygreen-ui-1l06pbn">
Michelle
</code>
, who has the
<code class="leafygreen-ui-1l06pbn">
Provider
</code>
role, and perform an
update:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Run:","name":"Log in as Michelle"},{"@type":"HowToStep","text":"Run:","name":"Perform update"}],"name":"User Roles and Document Updates","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">
Log in as
<code class="leafygreen-ui-1l06pbn">
Michelle
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#log-in-as-michelle" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="log-in-as-michelle">
</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.auth( \"Michelle\", \"me009\" )","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">auth</span>( <span class="lg-highlight-string">"Michelle"</span>, <span class="lg-highlight-string">"me009"</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">
Perform update
<a class="headerlink leafygreen-ui-1kepx47" href="#perform-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="perform-update">
</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":"// Attempt to update many documents\ndb.medical.updateMany(\n // User must have the Provider role to perform the update\n { $expr: { $ne: [ {\n $setIntersection: [ [ \"Provider\" ], \"$$USER_ROLES.role\" ] }, []\n ] } },\n // Update diagnosisCode\n { $set: { diagnosisCode: \"ACH 02\"} }\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">//</span><span class="lg-highlight-comment"> Attempt to update </span><span class="lg-highlight-comment">many documents</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">medical</span>.<span class="lg-highlight-title lg-highlight-function">updateMany</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> User must have </span><span class="lg-highlight-comment">the</span><span class="lg-highlight-comment"> Provider role to </span><span class="lg-highlight-comment">perform the update</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$expr</span>: <!-- -->{ <span class="lg-highlight-attr">$ne</span>: <!-- -->[ <!-- -->{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$setIntersection</span>: <!-- -->[ <!-- -->[ <span class="lg-highlight-string">"Provider"</span> ]<!-- -->, <span class="lg-highlight-string">"$$USER_ROLES.role"</span> ] }<!-- -->, <!-- -->[<!-- -->]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ] } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Update diagnosisCode</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">diagnosisCode</span>: <span class="lg-highlight-string">"ACH 02"</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>
<p class="leafygreen-ui-1kp3ins">
The previous example uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setIntersection/#mongodb-expression-exp.-setIntersection">
<code class="leafygreen-ui-1l06pbn">
$setIntersection
</code>
</a>
to return
documents where the intersection between the
<code class="leafygreen-ui-1l06pbn">
"Provider"
</code>
string and
the user roles from
<code class="leafygreen-ui-1l06pbn">
$$USER_ROLES.role
</code>
is not empty.
<code class="leafygreen-ui-1l06pbn">
Michelle
</code>
has
the
<code class="leafygreen-ui-1l06pbn">
Provider
</code>
role, so the update is performed.
</p>
<p class="leafygreen-ui-1kp3ins">
Next, log in as as
<code class="leafygreen-ui-1l06pbn">
James
</code>
, who does not have the
<code class="leafygreen-ui-1l06pbn">
Provider
</code>
role,
and attempt to perform the same update:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Run:","name":"Log in as James"},{"@type":"HowToStep","text":"Run:","name":"Attempt to perform update"}],"name":"User Roles and Document Updates","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">
Log in as
<code class="leafygreen-ui-1l06pbn">
James
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#log-in-as-james" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="log-in-as-james">
</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.auth( \"James\", \"js008\" )","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">auth</span>( <span class="lg-highlight-string">"James"</span>, <span class="lg-highlight-string">"js008"</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">
Attempt to perform update
<a class="headerlink leafygreen-ui-1kepx47" href="#attempt-to-perform-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="attempt-to-perform-update">
</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":"// Attempt to update many documents\ndb.medical.updateMany(\n // User must have the Provider role to perform the update\n { $expr: { $ne: [ {\n $setIntersection: [ [ \"Provider\" ], \"$$USER_ROLES.role\" ] }, []\n ] } },\n // Update diagnosisCode\n { $set: { diagnosisCode: \"ACH 02\"} }\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">//</span><span class="lg-highlight-comment"> Attempt to update </span><span class="lg-highlight-comment">many documents</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">medical</span>.<span class="lg-highlight-title lg-highlight-function">updateMany</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> User must have </span><span class="lg-highlight-comment">the</span><span class="lg-highlight-comment"> Provider role to </span><span class="lg-highlight-comment">perform the update</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$expr</span>: <!-- -->{ <span class="lg-highlight-attr">$ne</span>: <!-- -->[ <!-- -->{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$setIntersection</span>: <!-- -->[ <!-- -->[ <span class="lg-highlight-string">"Provider"</span> ]<!-- -->, <span class="lg-highlight-string">"$$USER_ROLES.role"</span> ] }<!-- -->, <!-- -->[<!-- -->]</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ] } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">// Update diagnosisCode</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <span class="lg-highlight-attr">diagnosisCode</span>: <span class="lg-highlight-string">"ACH 02"</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>
<p class="leafygreen-ui-1kp3ins">
The previous example does not update any documents.
</p>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Aggregation Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#aggregation-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="aggregation-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="#compatibility" target="_self">
<span>
Compatibility
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#expression-operators" target="_self">
<span>
Expression Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#arithmetic-expression-operators" target="_self">
<span>
Arithmetic Expression Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#array-expression-operators" target="_self">
<span>
Array Expression Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#bitwise-operators" target="_self">
<span>
Bitwise Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#boolean-expression-operators" target="_self">
<span>
Boolean Expression Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#comparison-expression-operators" target="_self">
<span>
Comparison Expression Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#conditional-expression-operators" target="_self">
<span>
Conditional Expression Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#custom-aggregation-expression-operators" target="_self">
<span>
Custom Aggregation Expression Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#data-size-operators" target="_self">
<span>
Data Size Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#date-expression-operators" target="_self">
<span>
Date Expression Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#literal-expression-operator" target="_self">
<span>
Literal Expression Operator
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#miscellaneous-operators" target="_self">
<span>
Miscellaneous Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#object-expression-operators" target="_self">
<span>
Object Expression Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#set-expression-operators" target="_self">
<span>
Set Expression Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#string-expression-operators" target="_self">
<span>
String Expression Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#text-expression-operator" target="_self">
<span>
Text Expression Operator
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#timestamp-expression-operators" target="_self">
<span>
Timestamp Expression Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#trigonometry-expression-operators" target="_self">
<span>
Trigonometry Expression Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#type-expression-operators" target="_self">
<span>
Type Expression Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#accumulators---group---bucket---bucketauto---setwindowfields-" target="_self">
<span>
Accumulators (
<code class="css-1kekw28 e1wawog0">
$group, $bucket, $bucketAuto, $setWindowFields
</code>
)
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#accumulators--in-other-stages-" target="_self">
<span>
Accumulators (in Other Stages)
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#variable-expression-operators" target="_self">
<span>
Variable Expression Operators
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#window-operators" target="_self">
<span>
Window Operators
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
The aggregation pipeline operators are compatible with MongoDB Atlas and on-premise environments.
</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>
<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 aggregation pipeline operators
<!-- -->
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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-aggregation-expression-operators">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-aggregation-expressions">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-agg-quick-ref-operator-expressions">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Expression Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#expression-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="expression-operators">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Expression operators are similar to functions that take arguments. In
general, these operators take an array of arguments and have the
following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ <operator>: [ <argument1>, <argument2> ... ] }","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">{ <<!-- -->operator<!-- -->><!-- -->: <!-- -->[ <<!-- -->argument1<!-- -->><!-- -->,<!-- --> <!-- --><<!-- -->argument2<!-- -->> <!-- -->...<!-- --> ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 an operator accepts a single argument, you can omit the outer array
designating the argument list:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ <operator>: <argument> }","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">{ <<!-- -->operator<!-- -->><!-- -->: <<!-- -->argument<!-- -->> <!-- -->}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 avoid parsing ambiguity if the argument is a literal array, you must
wrap the literal array 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 or keep
the outer array that designates the argument list.
</p>
<p class="leafygreen-ui-1kp3ins">
This page lists the available operators that can be used to construct
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-expression">
expressions
</a>
. Expressions are used in the following
contexts in
<span class="leafygreen-ui-vm4wms" tabindex="0">
MQL
</span>
:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Some aggregation pipeline stages, such as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/project/#mongodb-pipeline-pipe.-project">
<code class="leafygreen-ui-1l06pbn">
$project
</code>
</a>
,
<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
<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">
Inside
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-query-predicate">
query predicates
</a>
that use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Find query
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/project-fields-from-query-results/#std-label-read-operations-projection">
projections
</a>
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-agg-quick-ref-operator-arithmetic">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Arithmetic Expression Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#arithmetic-expression-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="arithmetic-expression-operators">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Arithmetic expressions perform mathematic operations on numbers. Some
arithmetic expressions can also support date arithmetic.
</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>
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="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/abs/#mongodb-expression-exp.-abs">
<code class="leafygreen-ui-1l06pbn">
$abs
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the absolute value of a number.
</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/add/#mongodb-expression-exp.-add">
<code class="leafygreen-ui-1l06pbn">
$add
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Adds numbers to return the sum, or adds numbers and a date to
return a new date. If adding numbers and a date, treats the
numbers as milliseconds. Accepts any number of argument
expressions, but at most, one expression can resolve to a
date.
</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/ceil/#mongodb-expression-exp.-ceil">
<code class="leafygreen-ui-1l06pbn">
$ceil
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the smallest integer greater than or equal to the specified number.
</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/divide/#mongodb-expression-exp.-divide">
<code class="leafygreen-ui-1l06pbn">
$divide
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the result of dividing the first number by the
second. Accepts two argument expressions.
</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/exp/#mongodb-expression-exp.-exp">
<code class="leafygreen-ui-1l06pbn">
$exp
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Raises
<em>
e
</em>
to the specified exponent.
</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/floor/#mongodb-expression-exp.-floor">
<code class="leafygreen-ui-1l06pbn">
$floor
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the largest integer less than or equal to the
specified number.
</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/ln/#mongodb-expression-exp.-ln">
<code class="leafygreen-ui-1l06pbn">
$ln
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Calculates the natural log of a number.
</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/log/#mongodb-expression-exp.-log">
<code class="leafygreen-ui-1l06pbn">
$log
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Calculates the log of a number in the specified base.
</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/log10/#mongodb-expression-exp.-log10">
<code class="leafygreen-ui-1l06pbn">
$log10
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Calculates the log base 10 of a number.
</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/mod/#mongodb-expression-exp.-mod">
<code class="leafygreen-ui-1l06pbn">
$mod
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the remainder of the first number divided by the
second. Accepts two argument expressions.
</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/multiply/#mongodb-expression-exp.-multiply">
<code class="leafygreen-ui-1l06pbn">
$multiply
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Multiplies numbers to return the product. Accepts any number
of argument expressions.
</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/pow/#mongodb-expression-exp.-pow">
<code class="leafygreen-ui-1l06pbn">
$pow
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Raises a number to the specified exponent.
</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/round/#mongodb-expression-exp.-round">
<code class="leafygreen-ui-1l06pbn">
$round
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Rounds a number to to a whole integer
<em>
or
</em>
to a specified
decimal place.
</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/sqrt/#mongodb-expression-exp.-sqrt">
<code class="leafygreen-ui-1l06pbn">
$sqrt
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Calculates the square root.
</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/subtract/#mongodb-expression-exp.-subtract">
<code class="leafygreen-ui-1l06pbn">
$subtract
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the result of subtracting the second value from the
first. If the two values are numbers, return the difference.
If the two values are dates, return the difference in
milliseconds. If the two values are a date and a number in
milliseconds, return the resulting date. Accepts two argument
expressions. If the two values are a date and a number,
specify the date argument first as it is not meaningful to
subtract a date from a number.
</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/trunc/#mongodb-expression-exp.-trunc">
<code class="leafygreen-ui-1l06pbn">
$trunc
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Truncates a number to a whole integer
<em>
or
</em>
to a specified
decimal place.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-agg-quick-ref-operator-array">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Array Expression Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#array-expression-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="array-expression-operators">
</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>
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="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/arrayElemAt/#mongodb-expression-exp.-arrayElemAt">
<code class="leafygreen-ui-1l06pbn">
$arrayElemAt
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the element at the specified array index.
</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/arrayToObject/#mongodb-expression-exp.-arrayToObject">
<code class="leafygreen-ui-1l06pbn">
$arrayToObject
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts an array of key value pairs to a document.
</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/concatArrays/#mongodb-expression-exp.-concatArrays">
<code class="leafygreen-ui-1l06pbn">
$concatArrays
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Concatenates arrays to return the concatenated array.
</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/filter/#mongodb-expression-exp.-filter">
<code class="leafygreen-ui-1l06pbn">
$filter
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Selects a subset of the array to return an array with only
the elements that match the filter condition.
</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/firstN/#mongodb-expression-exp.-firstN">
<code class="leafygreen-ui-1l06pbn">
$firstN
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a specified number of elements from the beginning of an array.
Distinct from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/firstN/#mongodb-group-grp.-firstN">
<code class="leafygreen-ui-1l06pbn">
$firstN
</code>
</a>
accumulator.
</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/in/#mongodb-expression-exp.-in">
<code class="leafygreen-ui-1l06pbn">
$in
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a boolean indicating whether a specified value is in
an array.
</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/indexOfArray/#mongodb-expression-exp.-indexOfArray">
<code class="leafygreen-ui-1l06pbn">
$indexOfArray
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Searches an array for an occurrence of a specified value and
returns the array index of the first occurrence. Array indexes start
at zero.
</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/isArray/#mongodb-expression-exp.-isArray">
<code class="leafygreen-ui-1l06pbn">
$isArray
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Determines if the operand is an array. Returns a boolean.
</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/lastN/#mongodb-expression-exp.-lastN">
<code class="leafygreen-ui-1l06pbn">
$lastN
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a specified number of elements from the end of an array.
Distinct from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lastN/#mongodb-group-grp.-lastN">
<code class="leafygreen-ui-1l06pbn">
$lastN
</code>
</a>
accumulator.
</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/map/#mongodb-expression-exp.-map">
<code class="leafygreen-ui-1l06pbn">
$map
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Applies a subexpression to each element of an array and
returns the array of resulting values in order. Accepts named
parameters.
</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/maxN-array-element/#mongodb-expression-exp.-maxN">
<code class="leafygreen-ui-1l06pbn">
$maxN
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the
<code class="leafygreen-ui-1l06pbn">
n
</code>
largest values in an array.
Distinct from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/maxN/#mongodb-group-grp.-maxN">
<code class="leafygreen-ui-1l06pbn">
$maxN
</code>
</a>
accumulator.
</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/minN-array-element/#mongodb-expression-exp.-minN">
<code class="leafygreen-ui-1l06pbn">
$minN
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the
<code class="leafygreen-ui-1l06pbn">
n
</code>
smallest values in an array.
Distinct from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/minN/#mongodb-group-grp.-minN">
<code class="leafygreen-ui-1l06pbn">
$minN
</code>
</a>
accumulator.
</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/objectToArray/#mongodb-expression-exp.-objectToArray">
<code class="leafygreen-ui-1l06pbn">
$objectToArray
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts a document to an array of documents representing
key-value pairs.
</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/range/#mongodb-expression-exp.-range">
<code class="leafygreen-ui-1l06pbn">
$range
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Outputs an array containing a sequence of integers according
to user-defined inputs.
</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/reduce/#mongodb-expression-exp.-reduce">
<code class="leafygreen-ui-1l06pbn">
$reduce
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Applies an expression to each element in an array and
combines them into a single value.
</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/reverseArray/#mongodb-expression-exp.-reverseArray">
<code class="leafygreen-ui-1l06pbn">
$reverseArray
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns an array with the elements in reverse order.
</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/size/#mongodb-expression-exp.-size">
<code class="leafygreen-ui-1l06pbn">
$size
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the number of elements in the array. Accepts a single
expression as argument.
</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/slice/#mongodb-expression-exp.-slice">
<code class="leafygreen-ui-1l06pbn">
$slice
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a subset of an array.
</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/sortArray/#mongodb-expression-exp.-sortArray">
<code class="leafygreen-ui-1l06pbn">
$sortArray
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Sorts the elements of an array.
</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/zip/#mongodb-expression-exp.-zip">
<code class="leafygreen-ui-1l06pbn">
$zip
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Merge two arrays together.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Bitwise Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#bitwise-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="bitwise-operators">
</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>
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/aggregation/bitAnd/#mongodb-expression-exp.-bitAnd">
<code class="leafygreen-ui-1l06pbn">
$bitAnd
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the result of a bitwise
<code class="leafygreen-ui-1l06pbn">
and
</code>
operation on an array of
<code class="leafygreen-ui-1l06pbn">
int
</code>
or
<code class="leafygreen-ui-1l06pbn">
long
</code>
values.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.3
</em>
.
</p>
</div>
</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/aggregation/bitNot/#mongodb-expression-exp.-bitNot">
<code class="leafygreen-ui-1l06pbn">
$bitNot
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the result of a bitwise
<code class="leafygreen-ui-1l06pbn">
not
</code>
operation on a single argument
or an array that contains a single
<code class="leafygreen-ui-1l06pbn">
int
</code>
or
<code class="leafygreen-ui-1l06pbn">
long
</code>
value.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.3
</em>
.
</p>
</div>
</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/aggregation/bitOr/#mongodb-expression-exp.-bitOr">
<code class="leafygreen-ui-1l06pbn">
$bitOr
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the result of a bitwise
<code class="leafygreen-ui-1l06pbn">
or
</code>
operation on an array of
<code class="leafygreen-ui-1l06pbn">
int
</code>
or
<code class="leafygreen-ui-1l06pbn">
long
</code>
values.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.3
</em>
.
</p>
</div>
</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/aggregation/bitXor/#mongodb-expression-exp.-bitXor">
<code class="leafygreen-ui-1l06pbn">
$bitXor
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the result of a bitwise
<code class="leafygreen-ui-1l06pbn">
xor
</code>
(exclusive or) operation on an
array of
<code class="leafygreen-ui-1l06pbn">
int
</code>
and
<code class="leafygreen-ui-1l06pbn">
long
</code>
values.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.3
</em>
.
</p>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Boolean Expression Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#boolean-expression-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="boolean-expression-operators">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Boolean expressions evaluate their argument expressions as booleans and
return a boolean as the result.
</p>
<p class="leafygreen-ui-1kp3ins">
In addition to the
<code class="leafygreen-ui-1l06pbn">
false
</code>
boolean value, Boolean expression 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 Boolean expression 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: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>
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/aggregation/and/#mongodb-expression-exp.-and">
<code class="leafygreen-ui-1l06pbn">
$and
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
only when
<em>
all
</em>
its expressions evaluate to
<code class="leafygreen-ui-1l06pbn">
true
</code>
. Accepts any number of argument expressions.
</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/aggregation/not/#mongodb-expression-exp.-not">
<code class="leafygreen-ui-1l06pbn">
$not
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the boolean value that is the opposite of its
argument expression. Accepts a single argument expression.
</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/aggregation/or/#mongodb-expression-exp.-or">
<code class="leafygreen-ui-1l06pbn">
$or
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
when
<em>
any
</em>
of its expressions evaluates to
<code class="leafygreen-ui-1l06pbn">
true
</code>
. Accepts any number of argument expressions.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Comparison Expression Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#comparison-expression-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="comparison-expression-operators">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Comparison expressions return a boolean except for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/cmp/#mongodb-expression-exp.-cmp">
<code class="leafygreen-ui-1l06pbn">
$cmp
</code>
</a>
which returns a number.
</p>
<p class="leafygreen-ui-1kp3ins">
The comparison expressions take two argument expressions and compare
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>
<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>
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="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/cmp/#mongodb-expression-exp.-cmp">
<code class="leafygreen-ui-1l06pbn">
$cmp
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns
<code class="leafygreen-ui-1l06pbn">
0
</code>
if the two values are equivalent,
<code class="leafygreen-ui-1l06pbn">
1
</code>
if the
first value is greater than the second, and
<code class="leafygreen-ui-1l06pbn">
-1
</code>
if the
first value is less than the second.
</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/eq/#mongodb-expression-exp.-eq">
<code class="leafygreen-ui-1l06pbn">
$eq
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
if the values are equivalent.
</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/gt/#mongodb-expression-exp.-gt">
<code class="leafygreen-ui-1l06pbn">
$gt
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
if the first value is greater than the
second.
</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/gte/#mongodb-expression-exp.-gte">
<code class="leafygreen-ui-1l06pbn">
$gte
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
if the first value is greater than or equal
to the second.
</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/lt/#mongodb-expression-exp.-lt">
<code class="leafygreen-ui-1l06pbn">
$lt
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
if the first value is less than the second.
</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/lte/#mongodb-expression-exp.-lte">
<code class="leafygreen-ui-1l06pbn">
$lte
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
if the first value is less than or equal to
the second.
</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/ne/#mongodb-expression-exp.-ne">
<code class="leafygreen-ui-1l06pbn">
$ne
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
if the values are
<em>
not
</em>
equivalent.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-aggregation-conditional-expression-operators">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Conditional Expression Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#conditional-expression-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="conditional-expression-operators">
</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>
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/aggregation/cond/#mongodb-expression-exp.-cond">
<code class="leafygreen-ui-1l06pbn">
$cond
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A ternary operator that evaluates one expression, and
depending on the result, returns the value of one of the
other two expressions. Accepts either three expressions in an
ordered list or three named parameters.
</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/aggregation/ifNull/#mongodb-expression-exp.-ifNull">
<code class="leafygreen-ui-1l06pbn">
$ifNull
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns either the non-null result of the first expression or
the result of the second expression if the first expression
results in a null result. Null result encompasses instances
of undefined values or missing fields. Accepts two
expressions as arguments. The result of the second expression
can be null.
</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/aggregation/switch/#mongodb-expression-exp.-switch">
<code class="leafygreen-ui-1l06pbn">
$switch
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
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.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Custom Aggregation Expression Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#custom-aggregation-expression-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="custom-aggregation-expression-operators">
</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>
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/aggregation/accumulator/#mongodb-group-grp.-accumulator">
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Defines a custom accumulator function.
</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/aggregation/function/#mongodb-expression-exp.-function">
<code class="leafygreen-ui-1l06pbn">
$function
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Defines a custom function.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Data Size Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#data-size-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="data-size-operators">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following operators return the size of a data element:
</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>
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/aggregation/binarySize/#mongodb-expression-exp.-binarySize">
<code class="leafygreen-ui-1l06pbn">
$binarySize
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the size of a given string or binary data value's
content in bytes.
</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/aggregation/bsonSize/#mongodb-expression-exp.-bsonSize">
<code class="leafygreen-ui-1l06pbn">
$bsonSize
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the size in bytes of a given document (i.e. bsontype
<code class="leafygreen-ui-1l06pbn">
Object
</code>
) when encoded as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-BSON">
BSON
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-agg-qr-date-operators">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Date Expression Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#date-expression-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="date-expression-operators">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following operators returns date objects or components of a date
object:
</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>
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="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/dateAdd/#mongodb-expression-exp.-dateAdd">
<code class="leafygreen-ui-1l06pbn">
$dateAdd
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Adds a number of time units to a date object.
</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/dateDiff/#mongodb-expression-exp.-dateDiff">
<code class="leafygreen-ui-1l06pbn">
$dateDiff
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the difference between two dates.
</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/dateFromParts/#mongodb-expression-exp.-dateFromParts">
<code class="leafygreen-ui-1l06pbn">
$dateFromParts
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Constructs a BSON Date object given the date's constituent
parts.
</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/dateFromString/#mongodb-expression-exp.-dateFromString">
<code class="leafygreen-ui-1l06pbn">
$dateFromString
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts a date/time string to a date object.
</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/dateSubtract/#mongodb-expression-exp.-dateSubtract">
<code class="leafygreen-ui-1l06pbn">
$dateSubtract
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Subtracts a number of time units from a date object.
</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/dateToParts/#mongodb-expression-exp.-dateToParts">
<code class="leafygreen-ui-1l06pbn">
$dateToParts
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a document containing the constituent parts of a date.
</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/dateToString/#mongodb-expression-exp.-dateToString">
<code class="leafygreen-ui-1l06pbn">
$dateToString
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the date as a formatted string.
</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/dateTrunc/#mongodb-expression-exp.-dateTrunc">
<code class="leafygreen-ui-1l06pbn">
$dateTrunc
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Truncates a date.
</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/dayOfMonth/#mongodb-expression-exp.-dayOfMonth">
<code class="leafygreen-ui-1l06pbn">
$dayOfMonth
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the day of the month for a date as a number between 1
and 31.
</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/dayOfWeek/#mongodb-expression-exp.-dayOfWeek">
<code class="leafygreen-ui-1l06pbn">
$dayOfWeek
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the day of the week for a date as a number between 1
(Sunday) and 7 (Saturday).
</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/dayOfYear/#mongodb-expression-exp.-dayOfYear">
<code class="leafygreen-ui-1l06pbn">
$dayOfYear
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the day of the year for a date as a number between 1
and 366 (leap year).
</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/hour/#mongodb-expression-exp.-hour">
<code class="leafygreen-ui-1l06pbn">
$hour
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the hour for a date as a number between 0 and 23.
</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/isoDayOfWeek/#mongodb-expression-exp.-isoDayOfWeek">
<code class="leafygreen-ui-1l06pbn">
$isoDayOfWeek
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the weekday number in ISO 8601 format, ranging from
<code class="leafygreen-ui-1l06pbn">
1
</code>
(for Monday) to
<code class="leafygreen-ui-1l06pbn">
7
</code>
(for Sunday).
</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/isoWeek/#mongodb-expression-exp.-isoWeek">
<code class="leafygreen-ui-1l06pbn">
$isoWeek
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the week number in ISO 8601 format, ranging from
<code class="leafygreen-ui-1l06pbn">
1
</code>
to
<code class="leafygreen-ui-1l06pbn">
53
</code>
. Week numbers start at
<code class="leafygreen-ui-1l06pbn">
1
</code>
with the week
(Monday through Sunday) that contains the year's first
Thursday.
</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/isoWeekYear/#mongodb-expression-exp.-isoWeekYear">
<code class="leafygreen-ui-1l06pbn">
$isoWeekYear
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the year number in ISO 8601 format. The year starts
with the Monday of week 1 (ISO 8601) and ends with the Sunday
of the last week (ISO 8601).
</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/millisecond/#mongodb-expression-exp.-millisecond">
<code class="leafygreen-ui-1l06pbn">
$millisecond
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the milliseconds of a date as a number between 0 and
999.
</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/minute/#mongodb-expression-exp.-minute">
<code class="leafygreen-ui-1l06pbn">
$minute
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the minute for a date as a number between 0 and 59.
</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/month/#mongodb-expression-exp.-month">
<code class="leafygreen-ui-1l06pbn">
$month
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the month for a date as a number between 1 (January)
and 12 (December).
</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/second/#mongodb-expression-exp.-second">
<code class="leafygreen-ui-1l06pbn">
$second
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the seconds for a date as a number between 0 and 60
(leap seconds).
</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/toDate/#mongodb-expression-exp.-toDate">
<code class="leafygreen-ui-1l06pbn">
$toDate
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts value to a Date.
</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/week/#mongodb-expression-exp.-week">
<code class="leafygreen-ui-1l06pbn">
$week
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the week number for a date as a number between 0 (the
partial week that precedes the first Sunday of the year) and
53 (leap year).
</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/year/#mongodb-expression-exp.-year">
<code class="leafygreen-ui-1l06pbn">
$year
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the year for a date as a number (e.g. 2014).
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The following arithmetic operators can take date operands:
</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>
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/aggregation/add/#mongodb-expression-exp.-add">
<code class="leafygreen-ui-1l06pbn">
$add
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Adds numbers and a date to return a new date. If adding
numbers and a date, treats the numbers as milliseconds.
Accepts any number of argument expressions, but at most, one
expression can resolve to a date.
</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/aggregation/subtract/#mongodb-expression-exp.-subtract">
<code class="leafygreen-ui-1l06pbn">
$subtract
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the result of subtracting the second value from the
first. If the two values are dates, return the difference in
milliseconds. If the two values are a date and a number in
milliseconds, return the resulting date. Accepts two argument
expressions. If the two values are a date and a number,
specify the date argument first as it is not meaningful to
subtract a date from a number.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Literal Expression Operator
<a class="headerlink leafygreen-ui-1kepx47" href="#literal-expression-operator" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="literal-expression-operator">
</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>
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/aggregation/literal/#mongodb-expression-exp.-literal">
<code class="leafygreen-ui-1l06pbn">
$literal
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Return a value without parsing. Use for values that the
aggregation pipeline may interpret as an expression. For
example, use 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 to a string
that starts with a dollar sign (
<code class="leafygreen-ui-1l06pbn">
$
</code>
) to avoid parsing as a
field path.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Miscellaneous Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#miscellaneous-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="miscellaneous-operators">
</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>
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/aggregation/getField/#mongodb-expression-exp.-getField">
<code class="leafygreen-ui-1l06pbn">
$getField
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the value of a specified field from a document.
You can use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/getField/#mongodb-expression-exp.-getField">
<code class="leafygreen-ui-1l06pbn">
$getField
</code>
</a>
to retrieve the value of
fields with names that contain periods (
<code class="leafygreen-ui-1l06pbn">
.
</code>
) or start
with dollar signs (
<code class="leafygreen-ui-1l06pbn">
$
</code>
).
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
</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/aggregation/rand/#mongodb-expression-exp.-rand">
<code class="leafygreen-ui-1l06pbn">
$rand
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a random float between 0 and 1
</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/aggregation/sampleRate/#mongodb-expression-exp.-sampleRate">
<code class="leafygreen-ui-1l06pbn">
$sampleRate
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Randomly select documents at a given rate. Although the exact
number of documents selected varies on each run, the quantity
chosen approximates the sample rate expressed as a percentage
of the total number of documents.
</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/aggregation/toHashedIndexKey/#mongodb-expression-exp.-toHashedIndexKey">
<code class="leafygreen-ui-1l06pbn">
$toHashedIndexKey
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Computes and returns the hash of the input expression using
the same hash function that MongoDB uses to create a hashed
index.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Object Expression Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#object-expression-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="object-expression-operators">
</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>
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/aggregation/mergeObjects/#mongodb-expression-exp.-mergeObjects">
<code class="leafygreen-ui-1l06pbn">
$mergeObjects
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Combines multiple documents into a single document.
</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/aggregation/objectToArray/#mongodb-expression-exp.-objectToArray">
<code class="leafygreen-ui-1l06pbn">
$objectToArray
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts a document to an array of documents representing
key-value pairs.
</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/aggregation/setField/#mongodb-expression-exp.-setField">
<code class="leafygreen-ui-1l06pbn">
$setField
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Adds, updates, or removes a specified field in a document. You
can use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setField/#mongodb-expression-exp.-setField">
<code class="leafygreen-ui-1l06pbn">
$setField
</code>
</a>
to add, update,
or remove fields with names that contain periods (
<code class="leafygreen-ui-1l06pbn">
.
</code>
) or
start with dollar signs (
<code class="leafygreen-ui-1l06pbn">
$
</code>
).
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-agg-qr-set-operators">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Set Expression Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#set-expression-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="set-expression-operators">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Set expressions performs set operation on arrays, treating arrays as
sets. Set expressions ignores the duplicate entries in each input array
and the order of the elements.
</p>
<p class="leafygreen-ui-1kp3ins">
If the set operation returns a set, the operation filters out
duplicates in the result to output an array that contains only unique
entries. The order of the elements in the output array is unspecified.
</p>
<p class="leafygreen-ui-1kp3ins">
If a set contains a nested array element, the set expression does
<em>
not
</em>
descend
into the nested array but evaluates the array at top-level.
</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>
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="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/allElementsTrue/#mongodb-expression-exp.-allElementsTrue">
<code class="leafygreen-ui-1l06pbn">
$allElementsTrue
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
if
<em>
no
</em>
element of a set evaluates to
<code class="leafygreen-ui-1l06pbn">
false
</code>
, otherwise, returns
<code class="leafygreen-ui-1l06pbn">
false
</code>
. Accepts a single
argument expression.
</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/anyElementTrue/#mongodb-expression-exp.-anyElementTrue">
<code class="leafygreen-ui-1l06pbn">
$anyElementTrue
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
if
<em>
any
</em>
elements of a set evaluate to
<code class="leafygreen-ui-1l06pbn">
true
</code>
; otherwise, returns
<code class="leafygreen-ui-1l06pbn">
false
</code>
. Accepts a single
argument expression.
</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/setDifference/#mongodb-expression-exp.-setDifference">
<code class="leafygreen-ui-1l06pbn">
$setDifference
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a set with elements that appear in the first set
but not in the second set; i.e. performs a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="http://en.wikipedia.org/wiki/Complement_(set_theory)" rel="noopener noreferrer">
<span>
relative
complement
</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>
of the second set relative to the first. Accepts exactly
two argument expressions.
</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/setEquals/#mongodb-expression-exp.-setEquals">
<code class="leafygreen-ui-1l06pbn">
$setEquals
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
if the input sets have the same distinct
elements. Accepts two or more argument expressions.
</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/setIntersection/#mongodb-expression-exp.-setIntersection">
<code class="leafygreen-ui-1l06pbn">
$setIntersection
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a set with elements that appear in
<em>
all
</em>
of the
input sets. Accepts any number of argument expressions.
</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/setIsSubset/#mongodb-expression-exp.-setIsSubset">
<code class="leafygreen-ui-1l06pbn">
$setIsSubset
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns
<code class="leafygreen-ui-1l06pbn">
true
</code>
if all elements of the first set appear
in the second set, including when the first set equals the
second set; i.e. not a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="http://en.wikipedia.org/wiki/Subset" rel="noopener noreferrer">
<span>
strict subset
</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>
. Accepts exactly
two argument expressions.
</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/setUnion/#mongodb-expression-exp.-setUnion">
<code class="leafygreen-ui-1l06pbn">
$setUnion
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a set with elements that appear in
<em>
any
</em>
of the
input sets.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
String Expression Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#string-expression-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="string-expression-operators">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
String expressions, with the exception of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/concat/#mongodb-expression-exp.-concat">
<code class="leafygreen-ui-1l06pbn">
$concat
</code>
</a>
, only have
<!-- -->
a well-defined behavior for strings of ASCII characters.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/concat/#mongodb-expression-exp.-concat">
<code class="leafygreen-ui-1l06pbn">
$concat
</code>
</a>
behavior is well-defined regardless of
the characters used.
</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>
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="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/concat/#mongodb-expression-exp.-concat">
<code class="leafygreen-ui-1l06pbn">
$concat
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Concatenates any number of strings.
</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/dateFromString/#mongodb-expression-exp.-dateFromString">
<code class="leafygreen-ui-1l06pbn">
$dateFromString
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts a date/time string to a date object.
</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/dateToString/#mongodb-expression-exp.-dateToString">
<code class="leafygreen-ui-1l06pbn">
$dateToString
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the date as a formatted string.
</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/indexOfBytes/#mongodb-expression-exp.-indexOfBytes">
<code class="leafygreen-ui-1l06pbn">
$indexOfBytes
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Searches a string for an occurrence of a substring and returns
the UTF-8 byte index of the first occurrence. If the substring
is not found, returns
<code class="leafygreen-ui-1l06pbn">
-1
</code>
.
</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/indexOfCP/#mongodb-expression-exp.-indexOfCP">
<code class="leafygreen-ui-1l06pbn">
$indexOfCP
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Searches a string for an occurrence of a substring and returns
the UTF-8 code point index of the first occurrence. If the
substring is not found, returns
<code class="leafygreen-ui-1l06pbn">
-1
</code>
</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/ltrim/#mongodb-expression-exp.-ltrim">
<code class="leafygreen-ui-1l06pbn">
$ltrim
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Removes whitespace or the specified characters from the
beginning of a string.
</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/regexFind/#mongodb-expression-exp.-regexFind">
<code class="leafygreen-ui-1l06pbn">
$regexFind
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Applies a regular expression (regex) to a string and returns
information on the
<em>
first
</em>
matched substring.
</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/regexFindAll/#mongodb-expression-exp.-regexFindAll">
<code class="leafygreen-ui-1l06pbn">
$regexFindAll
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Applies a regular expression (regex) to a string and returns
information on the all matched substrings.
</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/regexMatch/#mongodb-expression-exp.-regexMatch">
<code class="leafygreen-ui-1l06pbn">
$regexMatch
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Applies a regular expression (regex) to a string and returns
a boolean that indicates if a match is found or not.
</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/replaceOne/#mongodb-expression-exp.-replaceOne">
<code class="leafygreen-ui-1l06pbn">
$replaceOne
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Replaces the first instance of a matched string in a given
input.
</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/replaceAll/#mongodb-expression-exp.-replaceAll">
<code class="leafygreen-ui-1l06pbn">
$replaceAll
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Replaces all instances of a matched string in a given input.
</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/rtrim/#mongodb-expression-exp.-rtrim">
<code class="leafygreen-ui-1l06pbn">
$rtrim
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Removes whitespace or the specified characters from the
end of a string.
</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/split/#mongodb-expression-exp.-split">
<code class="leafygreen-ui-1l06pbn">
$split
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Splits a string into substrings based on a delimiter. Returns
an array of substrings. If the delimiter is not found within
the string, returns an array containing the original string.
</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/strLenBytes/#mongodb-expression-exp.-strLenBytes">
<code class="leafygreen-ui-1l06pbn">
$strLenBytes
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the number of UTF-8 encoded bytes in a string.
</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/strLenCP/#mongodb-expression-exp.-strLenCP">
<code class="leafygreen-ui-1l06pbn">
$strLenCP
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the number of UTF-8
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="http://www.unicode.org/glossary/#code_point" rel="noopener noreferrer">
<span>
code points
</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>
in a string.
</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/strcasecmp/#mongodb-expression-exp.-strcasecmp">
<code class="leafygreen-ui-1l06pbn">
$strcasecmp
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Performs case-insensitive string comparison and returns:
<code class="leafygreen-ui-1l06pbn">
0
</code>
if two strings are equivalent,
<code class="leafygreen-ui-1l06pbn">
1
</code>
if the first
string is greater than the second, and
<code class="leafygreen-ui-1l06pbn">
-1
</code>
if the first
string is less than the second.
</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/substr/#mongodb-expression-exp.-substr">
<code class="leafygreen-ui-1l06pbn">
$substr
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Deprecated. Use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/substrBytes/#mongodb-expression-exp.-substrBytes">
<code class="leafygreen-ui-1l06pbn">
$substrBytes
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/substrCP/#mongodb-expression-exp.-substrCP">
<code class="leafygreen-ui-1l06pbn">
$substrCP
</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/operator/aggregation/substrBytes/#mongodb-expression-exp.-substrBytes">
<code class="leafygreen-ui-1l06pbn">
$substrBytes
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the substring of a string. Starts with the character
at the specified UTF-8 byte index (zero-based) in the string
and continues for the specified number of bytes.
</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/substrCP/#mongodb-expression-exp.-substrCP">
<code class="leafygreen-ui-1l06pbn">
$substrCP
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the substring of a string. Starts with the character
at the specified UTF-8
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="http://www.unicode.org/glossary/#code_point" rel="noopener noreferrer">
<span>
code point (CP)
</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>
index
(zero-based) in the string and continues for the number of
code points specified.
</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/toLower/#mongodb-expression-exp.-toLower">
<code class="leafygreen-ui-1l06pbn">
$toLower
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts a string to lowercase. Accepts a single argument
expression.
</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/toString/#mongodb-expression-exp.-toString">
<code class="leafygreen-ui-1l06pbn">
$toString
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts value to a string.
</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/trim/#mongodb-expression-exp.-trim">
<code class="leafygreen-ui-1l06pbn">
$trim
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Removes whitespace or the specified characters from the
beginning and end of a string.
</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/toUpper/#mongodb-expression-exp.-toUpper">
<code class="leafygreen-ui-1l06pbn">
$toUpper
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts a string to uppercase. Accepts a single argument
expression.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Text Expression Operator
<a class="headerlink leafygreen-ui-1kepx47" href="#text-expression-operator" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-expression-operator">
</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>
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/aggregation/meta/#mongodb-expression-exp.-meta">
<code class="leafygreen-ui-1l06pbn">
$meta
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Access available per-document metadata related to the
aggregation operation.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Timestamp Expression Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#timestamp-expression-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="timestamp-expression-operators">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Timestamp expression operators return values from a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-timestamp">
timestamp
<!-- -->
.
</a>
</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>
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/aggregation/tsIncrement/#mongodb-expression-exp.-tsIncrement">
<code class="leafygreen-ui-1l06pbn">
$tsIncrement
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the incrementing ordinal from a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-timestamp">
timestamp
</a>
as a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json-v1/#mongodb-bsontype-data_numberlong">
<code class="leafygreen-ui-1l06pbn">
long
</code>
.
</a>
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.1
</em>
.
</p>
</div>
</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/aggregation/tsSecond/#mongodb-expression-exp.-tsSecond">
<code class="leafygreen-ui-1l06pbn">
$tsSecond
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the seconds from a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-timestamp">
timestamp
</a>
as a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json-v1/#mongodb-bsontype-data_numberlong">
<code class="leafygreen-ui-1l06pbn">
long
</code>
.
</a>
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.1
</em>
.
</p>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Trigonometry Expression Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#trigonometry-expression-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="trigonometry-expression-operators">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Trigonometry expressions perform trigonometric operations on numbers.
Values that represent angles are always input or output in radians. Use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/degreesToRadians/#mongodb-expression-exp.-degreesToRadians">
<code class="leafygreen-ui-1l06pbn">
$degreesToRadians
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/radiansToDegrees/#mongodb-expression-exp.-radiansToDegrees">
<code class="leafygreen-ui-1l06pbn">
$radiansToDegrees
</code>
</a>
to
convert between degree and radian measurements.
</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>
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="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/sin/#mongodb-expression-exp.-sin">
<code class="leafygreen-ui-1l06pbn">
$sin
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the sine of a value that is measured in radians.
</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/cos/#mongodb-expression-exp.-cos">
<code class="leafygreen-ui-1l06pbn">
$cos
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the cosine of a value that is measured in radians.
</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/tan/#mongodb-expression-exp.-tan">
<code class="leafygreen-ui-1l06pbn">
$tan
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the tangent of a value that is measured in radians.
</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/asin/#mongodb-expression-exp.-asin">
<code class="leafygreen-ui-1l06pbn">
$asin
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the inverse sin (arc sine) of a value in radians.
</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/acos/#mongodb-expression-exp.-acos">
<code class="leafygreen-ui-1l06pbn">
$acos
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the inverse cosine (arc cosine) of a value in radians.
</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/atan/#mongodb-expression-exp.-atan">
<code class="leafygreen-ui-1l06pbn">
$atan
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the inverse tangent (arc tangent) of a value in
radians.
</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/atan2/#mongodb-expression-exp.-atan2">
<code class="leafygreen-ui-1l06pbn">
$atan2
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the inverse tangent (arc tangent) of
<code class="leafygreen-ui-1l06pbn">
y / x
</code>
in
radians, where
<code class="leafygreen-ui-1l06pbn">
y
</code>
and
<code class="leafygreen-ui-1l06pbn">
x
</code>
are the first and second
values passed to the expression respectively.
</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/asinh/#mongodb-expression-exp.-asinh">
<code class="leafygreen-ui-1l06pbn">
$asinh
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the inverse hyperbolic sine (hyperbolic arc sine) of a
value in radians.
</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/acosh/#mongodb-expression-exp.-acosh">
<code class="leafygreen-ui-1l06pbn">
$acosh
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the inverse hyperbolic cosine (hyperbolic arc cosine)
of a value in radians.
</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/atanh/#mongodb-expression-exp.-atanh">
<code class="leafygreen-ui-1l06pbn">
$atanh
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the inverse hyperbolic tangent (hyperbolic arc
tangent) of a value in radians.
</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/sinh/#mongodb-expression-exp.-sinh">
<code class="leafygreen-ui-1l06pbn">
$sinh
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the hyperbolic sine of a value that is measured in
radians.
</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/cosh/#mongodb-expression-exp.-cosh">
<code class="leafygreen-ui-1l06pbn">
$cosh
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the hyperbolic cosine of a value that is measured in
radians.
</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/tanh/#mongodb-expression-exp.-tanh">
<code class="leafygreen-ui-1l06pbn">
$tanh
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the hyperbolic tangent of a value that is measured in
radians.
</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/degreesToRadians/#mongodb-expression-exp.-degreesToRadians">
<code class="leafygreen-ui-1l06pbn">
$degreesToRadians
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts a value from degrees to radians.
</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/radiansToDegrees/#mongodb-expression-exp.-radiansToDegrees">
<code class="leafygreen-ui-1l06pbn">
$radiansToDegrees
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts a value from radians to degrees.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Type Expression Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#type-expression-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="type-expression-operators">
</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>
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="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/convert/#mongodb-expression-exp.-convert">
<code class="leafygreen-ui-1l06pbn">
$convert
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts a value to a specified type.
</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/isNumber/#mongodb-expression-exp.-isNumber">
<code class="leafygreen-ui-1l06pbn">
$isNumber
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns boolean
<code class="leafygreen-ui-1l06pbn">
true
</code>
if the specified expression resolves
to an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-Int32">
<code class="leafygreen-ui-1l06pbn">
integer
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-Decimal128">
<code class="leafygreen-ui-1l06pbn">
decimal
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-Double">
<code class="leafygreen-ui-1l06pbn">
double
</code>
</a>
, or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-Int64">
<code class="leafygreen-ui-1l06pbn">
long
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Returns boolean
<code class="leafygreen-ui-1l06pbn">
false
</code>
if the expression resolves to any
other
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-bson-types">
BSON type
</a>
,
<code class="leafygreen-ui-1l06pbn">
null
</code>
, or a missing field.
</p>
</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/toBool/#mongodb-expression-exp.-toBool">
<code class="leafygreen-ui-1l06pbn">
$toBool
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts value to a boolean.
</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/toDate/#mongodb-expression-exp.-toDate">
<code class="leafygreen-ui-1l06pbn">
$toDate
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts value to a Date.
</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/toDecimal/#mongodb-expression-exp.-toDecimal">
<code class="leafygreen-ui-1l06pbn">
$toDecimal
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts value to a Decimal128.
</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/toDouble/#mongodb-expression-exp.-toDouble">
<code class="leafygreen-ui-1l06pbn">
$toDouble
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts value to a double.
</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/toInt/#mongodb-expression-exp.-toInt">
<code class="leafygreen-ui-1l06pbn">
$toInt
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts value to an integer.
</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/toLong/#mongodb-expression-exp.-toLong">
<code class="leafygreen-ui-1l06pbn">
$toLong
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts value to a long.
</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/toObjectId/#mongodb-expression-exp.-toObjectId">
<code class="leafygreen-ui-1l06pbn">
$toObjectId
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts value to an ObjectId.
</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/toString/#mongodb-expression-exp.-toString">
<code class="leafygreen-ui-1l06pbn">
$toString
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts value to a string.
</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/type/#mongodb-expression-exp.-type">
<code class="leafygreen-ui-1l06pbn">
$type
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Return the BSON data type of the field.
</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/toUUID/#mongodb-expression-exp.-toUUID">
<code class="leafygreen-ui-1l06pbn">
$toUUID
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Converts a string to a
<span class="leafygreen-ui-vm4wms" tabindex="0">
UUID
</span>
.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-agg-operators-group-accumulators">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-agg-quick-reference-accumulators">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Accumulators (
<code class="leafygreen-ui-1l06pbn">
$group, $bucket, $bucketAuto, $setWindowFields
</code>
)
<a class="headerlink leafygreen-ui-1kepx47" href="#accumulators---group---bucket---bucketauto---setwindowfields-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="accumulators---group---bucket---bucketauto---setwindowfields-">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Aggregation accumulator operators:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Maintain their state as documents progress through the aggregation
pipeline.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Return totals, maxima, minima, and other values.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Can be used in these aggregation pipeline 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/bucket/#mongodb-pipeline-pipe.-bucket">
<code class="leafygreen-ui-1l06pbn">
$bucket
</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/bucketAuto/#mongodb-pipeline-pipe.-bucketAuto">
<code class="leafygreen-ui-1l06pbn">
$bucketAuto
</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/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">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
starting in MongoDB 5.0 (except when
you are using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/accumulator/#mongodb-group-grp.-accumulator">
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/mergeObjects/#mongodb-expression-exp.-mergeObjects">
<code class="leafygreen-ui-1l06pbn">
$mergeObjects
</code>
</a>
operators, which cannot be used with
<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>
</li>
</ul>
</li>
</ul>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</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>
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="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/accumulator/#mongodb-group-grp.-accumulator">
<code class="leafygreen-ui-1l06pbn">
$accumulator
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the result of a user-defined accumulator function.
</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/addToSet/#mongodb-group-grp.-addToSet">
<code class="leafygreen-ui-1l06pbn">
$addToSet
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an array of
<em>
unique
</em>
expression values for each group.
Order of the array elements is undefined.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/avg/#mongodb-group-grp.-avg">
<code class="leafygreen-ui-1l06pbn">
$avg
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an average of numerical values. Ignores non-numeric values.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/bottom/#mongodb-group-grp.-bottom">
<code class="leafygreen-ui-1l06pbn">
$bottom
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the bottom element within a group according to the specified
sort order.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Available in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stages.
</p>
</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/bottomN/#mongodb-group-grp.-bottomN">
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an aggregation of the bottom
<code class="leafygreen-ui-1l06pbn">
n
</code>
fields within a group,
according to the specified sort order.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Available in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stages.
</p>
</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/count-accumulator/#mongodb-group-grp.-count">
<code class="leafygreen-ui-1l06pbn">
$count
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the number of documents in a group.
</p>
<p class="leafygreen-ui-1kp3ins">
Distinct from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/count/#mongodb-pipeline-pipe.-count">
<code class="leafygreen-ui-1l06pbn">
$count
</code>
</a>
pipeline stage.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
Available in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stages.
</p>
</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/first/#mongodb-group-grp.-first">
<code class="leafygreen-ui-1l06pbn">
$first
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the result of an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
</span>
</a>
for the first document in a group.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/firstN/#mongodb-group-grp.-firstN">
<code class="leafygreen-ui-1l06pbn">
$firstN
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an aggregation of the first
<code class="leafygreen-ui-1l06pbn">
n
</code>
elements within a group.
Only meaningful when documents are in a defined order.
Distinct from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/firstN/#mongodb-expression-exp.-firstN">
<code class="leafygreen-ui-1l06pbn">
$firstN
</code>
</a>
array operator.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
:
<!-- -->
Available in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stages.
</p>
</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/last/#mongodb-group-grp.-last">
<code class="leafygreen-ui-1l06pbn">
$last
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the result of an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
</span>
</a>
for the last document in a group.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/lastN/#mongodb-group-grp.-lastN">
<code class="leafygreen-ui-1l06pbn">
$lastN
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an aggregation of the last
<code class="leafygreen-ui-1l06pbn">
n
</code>
elements within a group.
Only meaningful when documents are in a defined order.
Distinct from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lastN/#mongodb-expression-exp.-lastN">
<code class="leafygreen-ui-1l06pbn">
$lastN
</code>
</a>
array operator.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
:
<!-- -->
Available in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stages.
</p>
</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/max/#mongodb-group-grp.-max">
<code class="leafygreen-ui-1l06pbn">
$max
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the highest expression value for each group.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/maxN/#mongodb-group-grp.-maxN">
<code class="leafygreen-ui-1l06pbn">
$maxN
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an aggregation of the
<code class="leafygreen-ui-1l06pbn">
n
</code>
maximum valued elements in
a group.
Distinct from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/maxN-array-element/#mongodb-expression-exp.-maxN">
<code class="leafygreen-ui-1l06pbn">
$maxN
</code>
</a>
array operator.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Available in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</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>
and as an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
<!-- -->
.
</span>
</a>
</p>
</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/median/#mongodb-group-grp.-median">
<code class="leafygreen-ui-1l06pbn">
$median
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an approximation of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-median">
median
</a>
, the 50th
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-percentile">
percentile
</a>
, as a scalar value.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
7.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
This operator is available as an accumulator in these 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/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">
<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>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
It is also available as an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
aggregation expression
<!-- -->
.
</span>
</a>
</p>
</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/mergeObjects/#mongodb-expression-exp.-mergeObjects">
<code class="leafygreen-ui-1l06pbn">
$mergeObjects
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a document created by combining the input documents
for each group.
</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/min/#mongodb-group-grp.-min">
<code class="leafygreen-ui-1l06pbn">
$min
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the lowest expression value for each group.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/minN/#mongodb-group-grp.-minN">
<code class="leafygreen-ui-1l06pbn">
$minN
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an aggregation of the
<code class="leafygreen-ui-1l06pbn">
n
</code>
minimum valued elements
in a group.
Distinct from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/minN-array-element/#mongodb-expression-exp.-minN">
<code class="leafygreen-ui-1l06pbn">
$minN
</code>
</a>
array operator.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Available in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</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>
and as an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
<!-- -->
.
</span>
</a>
</p>
</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/percentile/#mongodb-group-grp.-percentile">
<code class="leafygreen-ui-1l06pbn">
$percentile
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an array of scalar values that correspond to specified
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-percentile">
percentile
</a>
values.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
7.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
This operator is available as an accumulator in these 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/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">
<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>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
It is also available as an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
aggregation expression
<!-- -->
.
</span>
</a>
</p>
</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/push/#mongodb-group-grp.-push">
<code class="leafygreen-ui-1l06pbn">
$push
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an array of expression values for documents in each
group.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/stdDevPop/#mongodb-group-grp.-stdDevPop">
<code class="leafygreen-ui-1l06pbn">
$stdDevPop
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the population standard deviation of the input values.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/stdDevSamp/#mongodb-group-grp.-stdDevSamp">
<code class="leafygreen-ui-1l06pbn">
$stdDevSamp
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the sample standard deviation of the input values.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/sum/#mongodb-group-grp.-sum">
<code class="leafygreen-ui-1l06pbn">
$sum
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns a sum of numerical values. Ignores non-numeric values.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/top/#mongodb-group-grp.-top">
<code class="leafygreen-ui-1l06pbn">
$top
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the top element within a group according to the specified
sort order.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Available in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stages.
</p>
</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/topN/#mongodb-group-grp.-topN">
<code class="leafygreen-ui-1l06pbn">
$topN
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an aggregation of the top
<code class="leafygreen-ui-1l06pbn">
n
</code>
fields within a group,
according to the specified sort order.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Available in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stages.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Accumulators (in Other Stages)
<a class="headerlink leafygreen-ui-1kepx47" href="#accumulators--in-other-stages-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="accumulators--in-other-stages-">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Some operators that are available as accumulators for the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
stage are also available for use in other stages but
not as accumulators. When used in these other stages, these operators
do not maintain their state and can take as input either a single
argument or multiple arguments. For details, refer to the specific
operator page.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
The following accumulator operators are also available in 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>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/addFields/#mongodb-pipeline-pipe.-addFields">
<code class="leafygreen-ui-1l06pbn">
$addFields
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/set/#mongodb-pipeline-pipe.-set">
<code class="leafygreen-ui-1l06pbn">
$set
</code>
</a>
, and,
starting in MongoDB 5.0, 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>
stages.
</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>
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="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/avg/#mongodb-group-grp.-avg">
<code class="leafygreen-ui-1l06pbn">
$avg
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns an average of the specified expression or list of
expressions for each document. Ignores non-numeric values.
</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/first/#mongodb-group-grp.-first">
<code class="leafygreen-ui-1l06pbn">
$first
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the result of an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
</span>
</a>
for the first document in a group.
</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/last/#mongodb-group-grp.-last">
<code class="leafygreen-ui-1l06pbn">
$last
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the result of an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
</span>
</a>
for the last document in a group.
</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/max/#mongodb-group-grp.-max">
<code class="leafygreen-ui-1l06pbn">
$max
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the maximum of the specified expression or list of
expressions for each document
</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/median/#mongodb-group-grp.-median">
<code class="leafygreen-ui-1l06pbn">
$median
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an approximation of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-median">
median
</a>
, the 50th
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-percentile">
percentile
</a>
, as a scalar value.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
7.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
This operator is available as an accumulator in these 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/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">
<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>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
It is also available as an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
aggregation expression
<!-- -->
.
</span>
</a>
</p>
</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/min/#mongodb-group-grp.-min">
<code class="leafygreen-ui-1l06pbn">
$min
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the minimum of the specified expression or list of
expressions for each document
</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/percentile/#mongodb-group-grp.-percentile">
<code class="leafygreen-ui-1l06pbn">
$percentile
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an array of scalar values that correspond to specified
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-percentile">
percentile
</a>
values.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
7.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
This operator is available as an accumulator in these 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/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">
<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>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
It is also available as an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
aggregation expression
<!-- -->
.
</span>
</a>
</p>
</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/stdDevPop/#mongodb-group-grp.-stdDevPop">
<code class="leafygreen-ui-1l06pbn">
$stdDevPop
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the population standard deviation of the input values.
</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/stdDevSamp/#mongodb-group-grp.-stdDevSamp">
<code class="leafygreen-ui-1l06pbn">
$stdDevSamp
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns the sample standard deviation of the input values.
</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/sum/#mongodb-group-grp.-sum">
<code class="leafygreen-ui-1l06pbn">
$sum
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns a sum of numerical values. Ignores non-numeric values.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Variable Expression Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#variable-expression-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="variable-expression-operators">
</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>
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/aggregation/let/#mongodb-expression-exp.-let">
<code class="leafygreen-ui-1l06pbn">
$let
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Defines variables for use within the scope of a subexpression
and returns the result of the subexpression. Accepts named
parameters.
</p>
<p class="leafygreen-ui-1kp3ins">
Accepts any number of argument expressions.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Window Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#window-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="window-operators">
</div>
</a>
</h3>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Window operators return values from a defined span of documents from
a collection, known as a
<em>
window
</em>
. A
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-window">
window
</a>
is defined 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, available starting in MongoDB
5.0.
</p>
<p class="leafygreen-ui-1kp3ins">
The following window operators are 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>
<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>
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="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/addToSet/#mongodb-group-grp.-addToSet">
<code class="leafygreen-ui-1l06pbn">
$addToSet
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an array of all unique values that results from
applying an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
</span>
</a>
to
each document.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/avg/#mongodb-group-grp.-avg">
<code class="leafygreen-ui-1l06pbn">
$avg
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the average for the specified
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
</span>
</a>
. Ignores non-numeric values.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/bottom/#mongodb-group-grp.-bottom">
<code class="leafygreen-ui-1l06pbn">
$bottom
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the bottom element within a group according to the specified
sort order.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Available in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stages.
</p>
</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/bottomN/#mongodb-group-grp.-bottomN">
<code class="leafygreen-ui-1l06pbn">
$bottomN
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an aggregation of the bottom
<code class="leafygreen-ui-1l06pbn">
n
</code>
fields within a group,
according to the specified sort order.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Available in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stages.
</p>
</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/count-accumulator/#mongodb-group-grp.-count">
<code class="leafygreen-ui-1l06pbn">
$count
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the number of documents in the group or window.
</p>
<p class="leafygreen-ui-1kp3ins">
Distinct from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/count/#mongodb-pipeline-pipe.-count">
<code class="leafygreen-ui-1l06pbn">
$count
</code>
</a>
pipeline stage.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</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/covariancePop/#mongodb-group-grp.-covariancePop">
<code class="leafygreen-ui-1l06pbn">
$covariancePop
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the population covariance of two numeric
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expressions
<!-- -->
.
</span>
</a>
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</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/covarianceSamp/#mongodb-group-grp.-covarianceSamp">
<code class="leafygreen-ui-1l06pbn">
$covarianceSamp
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the sample covariance of two numeric
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expressions
<!-- -->
.
</span>
</a>
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</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/denseRank/#mongodb-group-grp.-denseRank">
<code class="leafygreen-ui-1l06pbn">
$denseRank
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the document position (known as the rank) relative to
other documents 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
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-partitionBy">
partition
</a>
. There are no
gaps in the ranks. Ties receive the same rank.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</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/derivative/#mongodb-group-grp.-derivative">
<code class="leafygreen-ui-1l06pbn">
$derivative
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the average rate of change within the specified
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-window">
window
<!-- -->
.
</a>
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</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/documentNumber/#mongodb-group-grp.-documentNumber">
<code class="leafygreen-ui-1l06pbn">
$documentNumber
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the position of a document (known as the document
number) 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
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-partitionBy">
partition
</a>
. Ties result in
different adjacent document numbers.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</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/expMovingAvg/#mongodb-group-grp.-expMovingAvg">
<code class="leafygreen-ui-1l06pbn">
$expMovingAvg
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the exponential moving average for the numeric
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
<!-- -->
.
</span>
</a>
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</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/first/#mongodb-group-grp.-first">
<code class="leafygreen-ui-1l06pbn">
$first
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the result of an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
</span>
</a>
for the first document in a group
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-window">
window
<!-- -->
.
</a>
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/integral/#mongodb-group-grp.-integral">
<code class="leafygreen-ui-1l06pbn">
$integral
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the approximation of the area under a curve.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</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/last/#mongodb-group-grp.-last">
<code class="leafygreen-ui-1l06pbn">
$last
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the result of an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
</span>
</a>
for the last document in a group
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-window">
window
<!-- -->
.
</a>
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/linearFill/#mongodb-group-grp.-linearFill">
<code class="leafygreen-ui-1l06pbn">
$linearFill
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Fills
<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>
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 surrounding field values.
</p>
<p class="leafygreen-ui-1kp3ins">
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>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.3
</em>
.
</p>
</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/locf/#mongodb-group-grp.-locf">
<code class="leafygreen-ui-1l06pbn">
$locf
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<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">
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>
<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>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/max/#mongodb-group-grp.-max">
<code class="leafygreen-ui-1l06pbn">
$max
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the maximum value that results from applying an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
</span>
</a>
to each document.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/min/#mongodb-group-grp.-min">
<code class="leafygreen-ui-1l06pbn">
$min
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the minimum value that results from applying an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
</span>
</a>
to each document.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/minN/#mongodb-group-grp.-minN">
<code class="leafygreen-ui-1l06pbn">
$minN
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an aggregation of the
<code class="leafygreen-ui-1l06pbn">
n
</code>
minimum valued elements in a
group.
Distinct from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/minN-array-element/#mongodb-expression-exp.-minN">
<code class="leafygreen-ui-1l06pbn">
$minN
</code>
</a>
array operator.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Available in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</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>
and as an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
<!-- -->
.
</span>
</a>
</p>
</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/push/#mongodb-group-grp.-push">
<code class="leafygreen-ui-1l06pbn">
$push
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an array of values that result from applying an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
</span>
</a>
to each document.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/rank/#mongodb-group-grp.-rank">
<code class="leafygreen-ui-1l06pbn">
$rank
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the document position (known as the rank) relative to
other documents 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
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-partitionBy">
partition
<!-- -->
.
</a>
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</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/shift/#mongodb-group-grp.-shift">
<code class="leafygreen-ui-1l06pbn">
$shift
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the value from an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
</span>
</a>
applied to a document in a
specified position relative to the current document 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
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-partitionBy">
partition
<!-- -->
.
</a>
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</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/stdDevPop/#mongodb-group-grp.-stdDevPop">
<code class="leafygreen-ui-1l06pbn">
$stdDevPop
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the population standard deviation that results from
applying a numeric
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
</span>
</a>
to each document.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/stdDevSamp/#mongodb-group-grp.-stdDevSamp">
<code class="leafygreen-ui-1l06pbn">
$stdDevSamp
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the sample standard deviation that results from
applying a numeric
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
</span>
</a>
to each document.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/sum/#mongodb-group-grp.-sum">
<code class="leafygreen-ui-1l06pbn">
$sum
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the sum that results from applying a numeric
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-aggregation-expressions" target="_self">
<span>
expression
</span>
</a>
to each document.
</p>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
5.0
</em>
:
<!-- -->
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>
</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/top/#mongodb-group-grp.-top">
<code class="leafygreen-ui-1l06pbn">
$top
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns the top element within a group according to the specified
sort order.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Available in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stages.
</p>
</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/topN/#mongodb-group-grp.-topN">
<code class="leafygreen-ui-1l06pbn">
$topN
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Returns an aggregation of the top
<code class="leafygreen-ui-1l06pbn">
n
</code>
fields within a group,
according to the specified sort order.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Available in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/group/#mongodb-pipeline-pipe.-group">
<code class="leafygreen-ui-1l06pbn">
$group
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
<code class="leafygreen-ui-1l06pbn">
$setWindowFields
</code>
</a>
stages.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
For the pipeline stages, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation-pipeline/">
Aggregation Stages
<!-- -->
.
</a>
</p>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/change-events/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Change Events
<a class="headerlink leafygreen-ui-1kepx47" href="#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="change-events">
</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="#operation-types" target="_self">
<span>
Operation Types
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#resume-token" target="_self">
<span>
Resume Token
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#expanded-events" target="_self">
<span>
Expanded Events
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
Change streams watch collections, databases, or deployments for changes.
</p>
<p class="leafygreen-ui-1kp3ins">
When a change occurs on a watched resource, the change stream returns a
change event notification document, with information on the operation and
the changes it made.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-change-event-operation-types">
</span>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Operation Types
<a class="headerlink leafygreen-ui-1kepx47" href="#operation-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="operation-types">
</div>
</a>
</h2>
<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>
Event
</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>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/create/#mongodb-data-create">
<code class="leafygreen-ui-1l06pbn">
create
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Occurs on the creation of a collection.
</p>
<p class="leafygreen-ui-1kp3ins">
Requires that you set the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-streams-expanded-events" target="_self">
<span>
showExpandedEvents
</span>
</a>
option to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</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/change-events/createIndexes/#mongodb-data-createIndexes">
<code class="leafygreen-ui-1l06pbn">
createIndexes
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Occurs on the creation of indexes on the collection.
</p>
<p class="leafygreen-ui-1kp3ins">
Requires that you set the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-streams-expanded-events" target="_self">
<span>
showExpandedEvents
</span>
</a>
option to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</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/change-events/delete/#mongodb-data-delete">
<code class="leafygreen-ui-1l06pbn">
delete
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Occurs when a document is removed from the collection.
</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/change-events/drop/#mongodb-data-drop">
<code class="leafygreen-ui-1l06pbn">
drop
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Occurs when a collection is dropped from a database.
</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/change-events/dropDatabase/#mongodb-data-dropDatabase">
<code class="leafygreen-ui-1l06pbn">
dropDatabase
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Occurs when a database is dropped.
</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/change-events/dropIndexes/#mongodb-data-dropIndexes">
<code class="leafygreen-ui-1l06pbn">
dropIndexes
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Occurs when an index is dropped from the collection.
</p>
<p class="leafygreen-ui-1kp3ins">
Requires that you set the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-streams-expanded-events" target="_self">
<span>
showExpandedEvents
</span>
</a>
option to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</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/change-events/insert/#mongodb-data-insert">
<code class="leafygreen-ui-1l06pbn">
insert
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Occurs when an operation adds documents to a collection.
</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/change-events/invalidate/#mongodb-data-invalidate">
<code class="leafygreen-ui-1l06pbn">
invalidate
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Occurs when an operation renders the change stream
invalid.
</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/change-events/modify/#mongodb-data-modify">
<code class="leafygreen-ui-1l06pbn">
modify
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Occurs when a collection is modified.
</p>
<p class="leafygreen-ui-1kp3ins">
Requires that you set the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-streams-expanded-events" target="_self">
<span>
showExpandedEvents
</span>
</a>
option to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</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/change-events/refineCollectionShardKey/#mongodb-data-refineCollectionShardKey">
<code class="leafygreen-ui-1l06pbn">
refineCollectionShardKey
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Occurs when a shard key is modified.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.1
</em>
.
</p>
</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/change-events/rename/#mongodb-data-rename">
<code class="leafygreen-ui-1l06pbn">
rename
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Occurs when a collection is renamed.
</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/change-events/replace/#mongodb-data-replace">
<code class="leafygreen-ui-1l06pbn">
replace
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Occurs when an update operation removes a document from
a collection and replaces it with a new document.
</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/change-events/reshardCollection/#mongodb-data-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">
Occurs when the shard key for a collection and the distribution
of data changes.
</p>
<p class="leafygreen-ui-1kp3ins">
Requires that you set the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-streams-expanded-events" target="_self">
<span>
showExpandedEvents
</span>
</a>
option to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.1
</em>
:
<!-- -->
<em>
(Also available in 6.0.14)
</em>
</p>
</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/change-events/shardCollection/#mongodb-data-shardCollection">
<code class="leafygreen-ui-1l06pbn">
shardCollection
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Occurs when a collection is sharded.
</p>
<p class="leafygreen-ui-1kp3ins">
Requires that you set the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-change-streams-expanded-events" target="_self">
<span>
showExpandedEvents
</span>
</a>
option to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</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/change-events/update/#mongodb-data-update">
<code class="leafygreen-ui-1l06pbn">
update
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Occurs when an operation updates a document in a collection.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-change-stream-event-id">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Resume Token
<a class="headerlink leafygreen-ui-1kepx47" href="#resume-token" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-token">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Each change event includes an
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field, which contain a document. This
document can serve as the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/changeStreams/#std-label-change-stream-resume">
resume token
</a>
when
starting a change stream.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-change-streams-expanded-events">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Expanded Events
<a class="headerlink leafygreen-ui-1kepx47" href="#expanded-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="expanded-events">
</div>
</a>
</h2>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0, change streams support change notifications for DDL
events, like the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/createIndexes/#std-label-change-event-createIndexes">
createIndexes
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/dropIndexes/#std-label-change-event-dropIndexes">
dropIndexes
</a>
events. To include expanded
events in a change stream, create the change stream cursor using the
<code class="leafygreen-ui-1l06pbn">
showExpandedEvents
</code>
option.
</p>
<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":"let cur = db.names.aggregate( [ {\n $changeStream: {\n showExpandedEvents: true\n }\n }\n ] )\n\ncur.next()","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">let</span> cur <!-- -->= <!-- -->db.<span class="lg-highlight-property">names</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">$changeStream</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">showExpandedEvents</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><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">cur.<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>
|
https://www.mongodb.com/docs/manual/reference/method/Binary.createFromBase64/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Binary.createFromBase64()
<a class="headerlink leafygreen-ui-1kepx47" href="#binary.createfrombase64--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.createfrombase64--">
</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 base64 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.createFromBase64( <base64String> [ , <subType> ] )
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-Binary.createFromBase64" 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.createFromBase64">
</div>
</a>
</dt>
<dd>
</dd>
</dl>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Method Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#method-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="method-fields">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The method accepts 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">
base64String
</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>
Specifies a string that contains a base64 value. For example,
<code class="leafygreen-ui-1l06pbn">
"SGVsbG8gV29ybGQhCg=="
</code>
.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
subType
</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>
<p class="leafygreen-ui-1kp3ins">
Optional. Specifies a binary subtype.
</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>
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>
0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Generic binary subtype
</div>
</div>
</td>
</tr>
<tr class="">
<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>
Function
</div>
</div>
</td>
</tr>
<tr class="">
<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>
Byte array (deprecated)
</div>
</div>
</td>
</tr>
<tr class="">
<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>
Old UUID (deprecated)
</div>
</div>
</td>
</tr>
<tr class="">
<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>
UUID
</div>
</div>
</td>
</tr>
<tr class="">
<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>
MD5
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
128 through 255
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
User defined
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</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>
<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.createFromBase64()
</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">
binaryObjectsFromBase64
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.binaryObjectsFromBase64.insertOne( {\n _id: 0,\n binaryObject: Binary.createFromBase64( \"SGVsbG8gV29ybGQhCg==\" )\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">binaryObjectsFromBase64</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">createFromBase64</span>( <span class="lg-highlight-string">"SGVsbG8gV29ybGQhCg=="</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.createFromBase64()
</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.binaryObjectsFromBase64.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">binaryObjectsFromBase64</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 objects 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, starting in
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
2.0.0:
</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(\"SGVsbG8gV29ybGQhCg==\")\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">_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">"SGVsbG8gV29ybGQhCg=="</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.removeUser/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.removeUser()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.removeuser--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.removeuser--">
</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>
<div>
<p>
<em>
Deprecated
<!-- -->
since version
<!-- -->
2.6
</em>
:
<!-- -->
Use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.dropUser/#mongodb-method-db.dropUser">
<code class="leafygreen-ui-1l06pbn">
db.dropUser()
</code>
</a>
instead
of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.removeUser/#mongodb-method-db.removeUser">
<code class="leafygreen-ui-1l06pbn">
db.removeUser()
</code>
</a>
</p>
</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.removeUser(username)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.removeUser" 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.removeUser">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Removes the specified username from the database.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.removeUser" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.removeUser()
</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">
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 database username.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</dd>
</dl>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/configure-a-non-voting-replica-set-member/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Configure a Non-Voting Self-Managed Replica Set Member
<a class="headerlink leafygreen-ui-1kepx47" href="#configure-a-non-voting-self-managed-replica-set-member" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-a-non-voting-self-managed-replica-set-member">
</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>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#related-documents" target="_self">
<span>
Related Documents
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
Non-voting members allow you to add additional members for read
distribution beyond the maximum seven voting members.
</p>
<p class="leafygreen-ui-1kp3ins">
To configure a member as non-voting, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetReconfig/#mongodb-dbcommand-dbcmd.replSetReconfig">
<code class="leafygreen-ui-1l06pbn">
replSetReconfig
</code>
</a>
command
<em>
or
</em>
its
<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>
helper method
<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 set its
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-.votes">
<code class="leafygreen-ui-1l06pbn">
members[n].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">
members[n].priority
</code>
</a>
values to
<code class="leafygreen-ui-1l06pbn">
0
</code>
. Non-voting replica set members
<em>
must
</em>
have a
<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>
of
<code class="leafygreen-ui-1l06pbn">
0
</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">
Replica reconfiguration can add or remove no more than
<em>
one
</em>
voting replica
set member at a time. To modify the votes of multiple members, issue a series
of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetReconfig/#mongodb-dbcommand-dbcmd.replSetReconfig">
<code class="leafygreen-ui-1l06pbn">
replSetReconfig
</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>
operations to
modify 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>
</div>
</div>
<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 procedure converts configures a single
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary">
secondary
</a>
replica set member to be non-voting. To convert the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
member to be non-voting, you must first successfully step the primary
down using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetStepDown/#mongodb-dbcommand-dbcmd.replSetStepDown">
<code class="leafygreen-ui-1l06pbn">
replSetStepDown
</code>
</a>
or its shell helper
<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>
before performing this procedure.
</p>
<dl>
<dt>
1) Connect to the Replica Set Primary
</dt>
<dd class="css-1ufxuu7">
<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 replica set
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
<!-- -->
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongosh --host \"<hostname>:<port>\"","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 --host <span class="lg-highlight-string">"<hostname>:<port>"</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">
Replace the
<code class="leafygreen-ui-1l06pbn">
<hostname>
</code>
and
<code class="leafygreen-ui-1l06pbn">
<port>
</code>
with the hostname and
port of the replica set primary. Include any other parameters
required for your deployment.
</p>
</dd>
<dt>
2) Retrieve the Replica Configuration
</dt>
<dd class="css-1ufxuu7">
<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 in the shell and assign the
result to a 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>
<p class="leafygreen-ui-1kp3ins">
The returned
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#std-label-replSetGetConfig-output">
document
</a>
contains a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members">
<code class="leafygreen-ui-1l06pbn">
members
</code>
</a>
array, where each element in the array contains
the configuration for a single replica set member.
</p>
</dd>
<dt>
3) Configure the Member to be Non-Voting
</dt>
<dd class="css-1ufxuu7">
<p class="leafygreen-ui-1kp3ins">
For the replica member to change to be non-voting,
set 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>
to
<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":"cfg.members[n].votes = 0;\ncfg.members[n].priority = 0;","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.<span class="lg-highlight-property">members</span>[<!-- -->n].<span class="lg-highlight-property">votes</span> <!-- -->= <span class="lg-highlight-number">0</span>;</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">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">
Replace
<code class="leafygreen-ui-1l06pbn">
n
</code>
with the array index position of the member
to modify. 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 is
<em>
zero-indexed
</em>
,
where the first element in the array has an index position of
<code class="leafygreen-ui-1l06pbn">
0
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
The array index position of a 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 is
<em>
distinct
</em>
from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-._id">
<code class="leafygreen-ui-1l06pbn">
members[n]._id
</code>
</a>
of a specific member. Do
<em>
not
</em>
use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-._id">
<code class="leafygreen-ui-1l06pbn">
_id
</code>
</a>
to reference the array
index position of any any member in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members">
<code class="leafygreen-ui-1l06pbn">
members
</code>
.
</a>
</p>
</dd>
<dt>
4) Reconfigure the Replica Set with the New Configuration
</dt>
<dd class="css-1ufxuu7">
<p class="leafygreen-ui-1kp3ins">
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>
method to reconfigure the replica set
with the updated replica set configuration document.
</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>
</dd>
</dl>
<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>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Related Documents
<a class="headerlink leafygreen-ui-1kepx47" href="#related-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="related-documents">
</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/replica-configuration/#mongodb-rsconf-rsconf.members-n-.votes">
<code class="leafygreen-ui-1l06pbn">
members[n].votes
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.reconfig/#std-label-replica-set-reconfiguration-usage">
Replica Set Reconfiguration
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-elections/">
Replica Set Elections
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/aggregation-pipeline-limits/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Aggregation Pipeline Limits
<a class="headerlink leafygreen-ui-1kepx47" href="#aggregation-pipeline-limits" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-pipeline-limits">
</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="#result-size-restrictions" target="_self">
<span>
Result Size Restrictions
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#number-of-stages-restrictions" target="_self">
<span>
Number of Stages Restrictions
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#memory-restrictions" target="_self">
<span>
Memory Restrictions
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
Aggregation operations with 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 have the
following limitations.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Result Size Restrictions
<a class="headerlink leafygreen-ui-1kepx47" href="#result-size-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="result-size-restrictions">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
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 can either return a cursor or store
the results in a collection. Each document in the result set is subject
to the 16 megabyte
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/limits/#mongodb-limit-BSON-Document-Size">
BSON Document Size limit
</a>
. If any single document exceeds the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/limits/#mongodb-limit-BSON-Document-Size">
BSON Document Size
limit
</a>
, the aggregation produces an error. The
limit only applies to the returned documents. During the pipeline
processing, the documents may exceed this size. 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 returns a cursor by default.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-stage-restrictions">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Number of Stages Restrictions
<a class="headerlink leafygreen-ui-1kepx47" href="#number-of-stages-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="number-of-stages-restrictions">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
MongoDB limits the number of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation-pipeline/#std-label-aggregation-pipeline-operator-reference">
aggregation pipeline stages
</a>
allowed in a single
pipeline to 1000.
</p>
<p class="leafygreen-ui-1kp3ins">
If an aggregation pipeline exceeds the stage limit before or after being parsed,
you receive an error.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-agg-memory-restrictions">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Memory Restrictions
<a class="headerlink leafygreen-ui-1kepx47" href="#memory-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="memory-restrictions">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.allowDiskUseByDefault">
<code class="leafygreen-ui-1l06pbn">
allowDiskUseByDefault
</code>
</a>
parameter controls whether pipeline stages that require more than 100
megabytes of memory to execute write temporary files to disk by
default.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.allowDiskUseByDefault">
<code class="leafygreen-ui-1l06pbn">
allowDiskUseByDefault
</code>
</a>
is set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
, pipeline
stages that require more than 100 megabytes of memory to execute
write temporary files to disk by default. You can disable writing
temporary files to disk for specific
<code class="leafygreen-ui-1l06pbn">
find
</code>
or
<code class="leafygreen-ui-1l06pbn">
aggregate
</code>
commands using the
<code class="leafygreen-ui-1l06pbn">
{ allowDiskUse: false }
</code>
option.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.allowDiskUseByDefault">
<code class="leafygreen-ui-1l06pbn">
allowDiskUseByDefault
</code>
</a>
is set to
<code class="leafygreen-ui-1l06pbn">
false
</code>
, pipeline
stages that require more than 100 megabytes of memory to execute
raise an error by default. You can enable writing temporary files to
disk for specific
<code class="leafygreen-ui-1l06pbn">
find
</code>
or
<code class="leafygreen-ui-1l06pbn">
aggregate
</code>
using
the
<code class="leafygreen-ui-1l06pbn">
{ allowDiskUse: true }
</code>
option.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/atlas-search/aggregation-stages/search/#mongodb-pipeline-pipe.-search" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$search
</code>
</span>
</a>
aggregation stage is not restricted to
100 megabytes of RAM because it runs in a separate process.
</p>
<p class="leafygreen-ui-1kp3ins">
Examples of stages that can write temporary files to disk when
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#std-label-aggregate-cmd-allowDiskUse">
allowDiskUse
</a>
is
<code class="leafygreen-ui-1l06pbn">
true
</code>
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/aggregation/bucket/#mongodb-pipeline-pipe.-bucket">
<code class="leafygreen-ui-1l06pbn">
$bucket
</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/bucketAuto/#mongodb-pipeline-pipe.-bucketAuto">
<code class="leafygreen-ui-1l06pbn">
$bucketAuto
</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/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">
<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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sort/#mongodb-pipeline-pipe.-sort">
<code class="leafygreen-ui-1l06pbn">
$sort
</code>
</a>
when the sort operation is not supported by an
index
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sortByCount/#mongodb-pipeline-pipe.-sortByCount">
<code class="leafygreen-ui-1l06pbn">
$sortByCount
</code>
</a>
</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">
Pipeline stages operate on streams of documents with each pipeline
stage taking in documents, processing them, and then outputting the
resulting documents.
</p>
<p class="leafygreen-ui-1kp3ins">
Some stages can't output any documents until they have processed all
incoming documents. These pipeline stages must keep their stage
output in RAM until all incoming documents are processed. As a
result, these pipeline stages may require more space than the 100 MB
limit.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If the results of one of your
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sort/#mongodb-pipeline-pipe.-sort">
<code class="leafygreen-ui-1l06pbn">
$sort
</code>
</a>
pipeline stages exceed
the limit, consider
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sort/#std-label-sort-limit-sequence">
adding a $limit stage
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-the-database-profiler/#std-label-database-profiler">
profiler log messages
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/log-messages/#std-label-log-messages-ref">
diagnostic log
messages
</a>
includes a
<code class="leafygreen-ui-1l06pbn">
usedDisk
</code>
indicator if any aggregation stage wrote data to temporary files due
to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-agg-memory-restrictions" target="_self">
<span>
memory restrictions
<!-- -->
.
</span>
</a>
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/rs.printReplicationInfo/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
rs.printReplicationInfo()
<a class="headerlink leafygreen-ui-1kepx47" href="#rs.printreplicationinfo--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.printreplicationinfo--">
</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">
rs.printReplicationInfo()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-rs.printReplicationInfo" 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.printReplicationInfo">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Prints a formatted report of the replica set member's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-oplog">
oplog
</a>
.
The displayed report formats the data returned by
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getReplicationInfo/#mongodb-method-db.getReplicationInfo">
<code class="leafygreen-ui-1l06pbn">
db.getReplicationInfo()
</code>
</a>
. The output of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-rs.printReplicationInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
rs.printReplicationInfo()
</code>
</span>
</a>
is identical to that of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.printReplicationInfo/#mongodb-method-db.printReplicationInfo">
<code class="leafygreen-ui-1l06pbn">
db.printReplicationInfo()
</code>
.
</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
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-rs.printReplicationInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
rs.printReplicationInfo()
</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.printReplicationInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
rs.printReplicationInfo()
</code>
</span>
</a>
for manual inspection, and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getReplicationInfo/#mongodb-method-db.getReplicationInfo">
<code class="leafygreen-ui-1l06pbn">
db.getReplicationInfo()
</code>
</a>
in scripts.
</p>
</div>
</div>
</dd>
</dl>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" 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>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following example is a sample output from the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-rs.printReplicationInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
rs.printReplicationInfo()
</code>
</span>
</a>
method run on the primary:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"configured oplog size: 192MB\nlog length start to end: 65422secs (18.17hrs)\noplog first event time: Mon Jun 23 2014 17:47:18 GMT-0400 (EDT)\noplog last event time: Tue Jun 24 2014 11:57:40 GMT-0400 (EDT)\nnow: Thu Jun 26 2014 14:24:39 GMT-0400 (EDT)"}
</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">configured oplog size: 192MB</td></tr><tr class=""><td class="leafygreen-ui-7razhx">log length start to end: 65422secs (18.17hrs)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">oplog first event time: Mon Jun 23 2014 17:47:18 GMT-0400 (EDT)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">oplog last event time: Tue Jun 24 2014 11:57:40 GMT-0400 (EDT)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">now: Thu Jun 26 2014 14:24:39 GMT-0400 (EDT)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
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>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-rs.printReplicationInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
rs.printReplicationInfo()
</code>
</span>
</a>
formats and prints the data returned by
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getReplicationInfo/#mongodb-method-db.getReplicationInfo">
<code class="leafygreen-ui-1l06pbn">
db.getReplicationInfo()
</code>
:
</a>
</p>
<dl>
<dt>
configured oplog size
</dt>
<dd class="css-1ufxuu7">
Displays the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getReplicationInfo/#mongodb-data-db.getReplicationInfo--.logSizeMB">
<code class="leafygreen-ui-1l06pbn">
db.getReplicationInfo().logSizeMB
</code>
</a>
value.
</dd>
<dt>
log length start to end
</dt>
<dd class="css-1ufxuu7">
Displays the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getReplicationInfo/#mongodb-data-db.getReplicationInfo--.timeDiff">
<code class="leafygreen-ui-1l06pbn">
db.getReplicationInfo().timeDiff
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getReplicationInfo/#mongodb-data-db.getReplicationInfo--.timeDiffHours">
<code class="leafygreen-ui-1l06pbn">
db.getReplicationInfo().timeDiffHours
</code>
</a>
values.
</dd>
<dt>
oplog first event time
</dt>
<dd class="css-1ufxuu7">
Displays the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getReplicationInfo/#mongodb-data-db.getReplicationInfo--.tFirst">
<code class="leafygreen-ui-1l06pbn">
db.getReplicationInfo().tFirst
</code>
.
</a>
</dd>
<dt>
oplog last event time
</dt>
<dd class="css-1ufxuu7">
Displays the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getReplicationInfo/#mongodb-data-db.getReplicationInfo--.tLast">
<code class="leafygreen-ui-1l06pbn">
db.getReplicationInfo().tLast
</code>
.
</a>
</dd>
<dt>
now
</dt>
<dd class="css-1ufxuu7">
Displays the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getReplicationInfo/#mongodb-data-db.getReplicationInfo--.now">
<code class="leafygreen-ui-1l06pbn">
db.getReplicationInfo().now
</code>
.
</a>
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getReplicationInfo/#mongodb-method-db.getReplicationInfo">
<code class="leafygreen-ui-1l06pbn">
db.getReplicationInfo()
</code>
</a>
for description of the data.
</p>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/timeseries/timeseries-secondary-index/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Add Secondary Indexes to Time Series Collections
<a class="headerlink leafygreen-ui-1kepx47" href="#add-secondary-indexes-to-time-series-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="add-secondary-indexes-to-time-series-collections">
</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="#use-secondary-indexes-to-improve-sort-performance" target="_self">
<span>
Use Secondary Indexes to Improve Sort Performance
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#last-point-queries-on-time-series-collections" target="_self">
<span>
Last Point Queries on Time Series Collections
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#specify-index-hints-for-time-series-collections" target="_self">
<span>
Specify Index Hints for Time Series Collections
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#create-2dsphere-indexes" target="_self">
<span>
Create 2dsphere Indexes
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
To improve query performance for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-time-series-collection">
time series collections
</a>
, add one or more
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary-index">
secondary indexes
</a>
to support common time series query patterns. Starting in
MongoDB 6.3, MongoDB automatically creates a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-compound/#std-label-index-type-compound">
compound index
</a>
on the
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
and
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
fields for
new collections.
</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 supported. For a list of unsupported index
types, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries/timeseries-limitations/#std-label-timeseries-limitations-secondary-indexes">
Limitations for Secondary Indexes on Time Series
Collections
<!-- -->
.
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
You may wish to create additional secondary indexes. Consider a weather
data collection with the configuration:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.createCollection(\n\"weather\",\n{\n timeseries: {\n timeField: \"timestamp\",\n metaField: \"metadata\"\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">createCollection</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">"weather"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timeseries</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timeField</span>: <span class="lg-highlight-string">"timestamp"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">metaField</span>: <span class="lg-highlight-string">"metadata"</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 each weather data document, the
<code class="leafygreen-ui-1l06pbn">
metadata
</code>
field value is a
subdocument with fields for the weather sensor ID and type:
</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\": {\n \"sensorId\": 5578,\n \"type\": \"temperature\"\n },\n \"temp\": 12\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">"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-string">"metadata"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"sensorId"</span>: <span class="lg-highlight-number">5578</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"temperature"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"temp"</span>: <span class="lg-highlight-number">12</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 default compound index for the collection indexes the entire
<code class="leafygreen-ui-1l06pbn">
metadata
</code>
subdocument, so the index is only used with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/eq/#mongodb-expression-exp.-eq">
<code class="leafygreen-ui-1l06pbn">
$eq
</code>
</a>
queries. By indexing specific
<code class="leafygreen-ui-1l06pbn">
metadata
</code>
fields, you
improve query performance for other query types.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, this
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/in/#mongodb-expression-exp.-in">
<code class="leafygreen-ui-1l06pbn">
$in
</code>
</a>
query benefits from a
secondary index on
<code class="leafygreen-ui-1l06pbn">
metadata.type
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ metadata.type:{ $in: [\"temperature\", \"pressure\"] }}","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">{ <!-- -->metadata.<span class="lg-highlight-property">type</span>:<!-- -->{ <span class="lg-highlight-attr">$in</span>: <!-- -->[<span class="lg-highlight-string">"temperature"</span>, <span class="lg-highlight-string">"pressure"</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:
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.createIndex/#mongodb-method-db.collection.createIndex">
<code class="leafygreen-ui-1l06pbn">
db.collection.createIndex()
</code>
</a>
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-timeseries-secondary-index-sort-performance">
</span>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Use Secondary Indexes to Improve Sort Performance
<a class="headerlink leafygreen-ui-1kepx47" href="#use-secondary-indexes-to-improve-sort-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="use-secondary-indexes-to-improve-sort-performance">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Sort operations on time series collections can use secondary indexes
on the
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
field. Under certain conditions, sort operations can also use compound secondary indexes on the
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
and
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
fields.
</p>
<p class="leafygreen-ui-1kp3ins">
The aggregation pipeline stages
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sort/#mongodb-pipeline-pipe.-sort">
<code class="leafygreen-ui-1l06pbn">
$sort
</code>
</a>
determine which indexes a time series collection can
use. An index can be used in the following scenarios:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Sort on
<code class="leafygreen-ui-1l06pbn">
{ <timeField>: ±1 }
</code>
uses a secondary index on
<code class="leafygreen-ui-1l06pbn">
<timeField>
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Sort on
<code class="leafygreen-ui-1l06pbn">
{ <metaField>: ±1, timeField: ±1 }
</code>
uses the default
compound index on
<code class="leafygreen-ui-1l06pbn">
{ <metaField>: ±1, timeField: ±1 }
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Sort on
<code class="leafygreen-ui-1l06pbn">
{ <timeField>: ±1 }
</code>
uses a secondary index on
<code class="leafygreen-ui-1l06pbn">
{ metaField: ±1, timeField: ±1 }
</code>
when there is a point predicate
on
<code class="leafygreen-ui-1l06pbn">
<metaField>
</code>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For example, the following
<code class="leafygreen-ui-1l06pbn">
sensorData
</code>
collection contains
measurements from weather sensors:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sensorData.insertMany( [ {\n \"metadata\": {\n \"sensorId\": 5578,\n \"type\": \"omni\",\n \"location\": {\n type: \"Point\",\n coordinates: [-77.40711, 39.03335]\n }\n },\n \"timestamp\": ISODate(\"2022-01-15T00:00:00.000Z\"),\n \"currentConditions\": {\n \"windDirection\": 127.0,\n \"tempF\": 71.0,\n \"windSpeed\": 2.0,\n \"cloudCover\": null,\n \"precip\": 0.1,\n \"humidity\": 94.0,\n }\n },\n {\n \"metadata\": {\n \"sensorId\": 5578,\n \"type\": \"omni\",\n \"location\": {\n type: \"Point\",\n coordinates: [-77.40711, 39.03335]\n }\n },\n \"timestamp\": ISODate(\"2022-01-15T00:01:00.000Z\"),\n \"currentConditions\": {\n \"windDirection\": 128.0,\n \"tempF\": 69.8,\n \"windSpeed\": 2.2,\n \"cloudCover\": null,\n \"precip\": 0.1,\n \"humidity\": 94.3,\n }\n },\n {\n \"metadata\": {\n \"sensorId\": 5579,\n \"type\": \"omni\",\n \"location\": {\n type: \"Point\",\n coordinates: [-80.19773, 25.77481]\n }\n },\n \"timestamp\": ISODate(\"2022-01-15T00:01:00.000Z\"),\n \"currentConditions\": {\n \"windDirection\": 115.0,\n \"tempF\": 88.0,\n \"windSpeed\": 1.0,\n \"cloudCover\": null,\n \"precip\": 0.0,\n \"humidity\": 99.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">sensorData</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">"metadata"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"sensorId"</span>: <span class="lg-highlight-number">5578</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"omni"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"location"</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">77.40711</span>, <span class="lg-highlight-number">39.03335</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">"timestamp"</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2022-01-15T00:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"currentConditions"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"windDirection"</span>: <span class="lg-highlight-number">127.0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tempF"</span>: <span class="lg-highlight-number">71.0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"windSpeed"</span>: <span class="lg-highlight-number">2.0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"cloudCover"</span>: <span class="lg-highlight-literal">null</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"precip"</span>: <span class="lg-highlight-number">0.1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"humidity"</span>: <span class="lg-highlight-number">94.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">"metadata"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"sensorId"</span>: <span class="lg-highlight-number">5578</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"omni"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"location"</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">77.40711</span>, <span class="lg-highlight-number">39.03335</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">"timestamp"</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2022-01-15T00:01:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"currentConditions"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"windDirection"</span>: <span class="lg-highlight-number">128.0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tempF"</span>: <span class="lg-highlight-number">69.8</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"windSpeed"</span>: <span class="lg-highlight-number">2.2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"cloudCover"</span>: <span class="lg-highlight-literal">null</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"precip"</span>: <span class="lg-highlight-number">0.1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"humidity"</span>: <span class="lg-highlight-number">94.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"> <span class="lg-highlight-string">"metadata"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"sensorId"</span>: <span class="lg-highlight-number">5579</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"omni"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"location"</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">80.19773</span>, <span class="lg-highlight-number">25.77481</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">"timestamp"</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2022-01-15T00:01:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"currentConditions"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"windDirection"</span>: <span class="lg-highlight-number">115.0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tempF"</span>: <span class="lg-highlight-number">88.0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"windSpeed"</span>: <span class="lg-highlight-number">1.0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"cloudCover"</span>: <span class="lg-highlight-literal">null</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"precip"</span>: <span class="lg-highlight-number">0.0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"humidity"</span>: <span class="lg-highlight-number">99.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">
Create a secondary single-field index on the
<code class="leafygreen-ui-1l06pbn">
timestamp
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sensorData.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">sensorData</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <span class="lg-highlight-string">"timestamp"</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 following sort operation on the
<code class="leafygreen-ui-1l06pbn">
timestamp
</code>
field uses the
Secondary Index to improve performance:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sensorData.aggregate( [\n { $match: { \"timestamp\" : { $gte: ISODate(\"2022-01-15T00:00:00.000Z\") } } },\n { $sort: { \"timestamp\": 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">sensorData</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">"timestamp"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2022-01-15T00:00:00.000Z"</span>) } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$sort</span>: <!-- -->{ <span class="lg-highlight-string">"timestamp"</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">
To confirm that the sort operation used the Secondary Index, run the
operation again with the
<code class="leafygreen-ui-1l06pbn">
.explain( "executionStats" )
</code>
option:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sensorData.explain( \"executionStats\" ).aggregate( [\n { $match: { \"timestamp\": { $gte: ISODate(\"2022-01-15T00:00:00.000Z\") } } },\n { $sort: { \"timestamp\": 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">sensorData</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">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$match</span>: <!-- -->{ <span class="lg-highlight-string">"timestamp"</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2022-01-15T00:00:00.000Z"</span>) } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$sort</span>: <!-- -->{ <span class="lg-highlight-string">"timestamp"</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>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Last Point Queries on Time Series Collections
<a class="headerlink leafygreen-ui-1kepx47" href="#last-point-queries-on-time-series-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="last-point-queries-on-time-series-collections">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
In time series data, a last point query returns the data point with the
latest timestamp for a given field. For time series collections, a last
point query fetches the latest measurement for each unique metadata
value. For example, you may want to get the latest temperature reading
from all sensors. Improve performance on last point queries by creating
any of the following indexes:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"metadata.sensorId\": 1, \"timestamp\": 1 }\n{ \"metadata.sensorId\": 1, \"timestamp\": -1 }\n{ \"metadata.sensorId\": -1, \"timestamp\": 1 }\n{ \"metadata.sensorId\": -1, \"timestamp\": -1 }","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">"metadata.sensorId"</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"timestamp"</span>: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"metadata.sensorId"</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"timestamp"</span>: <!-- -->-<span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"metadata.sensorId"</span>: <!-- -->-<span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"timestamp"</span>: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"metadata.sensorId"</span>: <!-- -->-<span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"timestamp"</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 class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Last point queries are most performant when they use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#std-label-explain-results">
DISTINCT_SCAN
optimization
</a>
. This optimization is only available when an
index on
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
is descending.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following command creates a compound secondary index on
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
(ascending) and
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
(descending):
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sensorData.createIndex( { \"metadata.sensorId\": 1, \"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">sensorData</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <span class="lg-highlight-string">"metadata.sensorId"</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"timestamp"</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 following last point query example uses the descending
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
compound secondary index created above:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sensorData.aggregate( [\n {\n $sort: { \"metadata.sensorId\": 1, \"timestamp\": -1 }\n },\n {\n $group: {\n _id: \"$metadata.sensorId\",\n ts: { $first: \"$timestamp\" },\n temperatureF: { $first: \"$currentConditions.tempF\" }\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">sensorData</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">$sort</span>: <!-- -->{ <span class="lg-highlight-string">"metadata.sensorId"</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"timestamp"</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">$group</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$metadata.sensorId"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ts</span>: <!-- -->{ <span class="lg-highlight-attr">$first</span>: <span class="lg-highlight-string">"$timestamp"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">temperatureF</span>: <!-- -->{ <span class="lg-highlight-attr">$first</span>: <span class="lg-highlight-string">"$currentConditions.tempF"</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">
To confirm that the last point query used the secondary index, run the operation
again using
<code class="leafygreen-ui-1l06pbn">
.explain( "executionStats" )
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getCollection( 'sensorData' ).explain( \"executionStats\" ).aggregate( [\n {\n $sort: { \"metadata.sensorId\": 1, \"timestamp\": -1 }\n },\n {\n $group: {\n _id: \"$metadata.sensorId\",\n ts: { $first: \"$timestamp\" },\n temperatureF: { $first: \"$currentConditions.tempF\" }\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">getCollection</span>( <span class="lg-highlight-string">'sensorData'</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">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">$sort</span>: <!-- -->{ <span class="lg-highlight-string">"metadata.sensorId"</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"timestamp"</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">$group</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"$metadata.sensorId"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ts</span>: <!-- -->{ <span class="lg-highlight-attr">$first</span>: <span class="lg-highlight-string">"$timestamp"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">temperatureF</span>: <!-- -->{ <span class="lg-highlight-attr">$first</span>: <span class="lg-highlight-string">"$currentConditions.tempF"</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
<code class="leafygreen-ui-1l06pbn">
winningPlan.queryPlan.inputStage.stage
</code>
is
<code class="leafygreen-ui-1l06pbn">
DISTINCT_SCAN
</code>
, which
indicates that the index was used. For more information on the explain plan
output, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#std-label-explain-results">
Explain Results
<!-- -->
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Specify Index Hints for Time Series Collections
<a class="headerlink leafygreen-ui-1kepx47" href="#specify-index-hints-for-time-series-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="specify-index-hints-for-time-series-collections">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Index hints cause MongoDB to use a specific index for a query. Some
operations on time series collections can only take advantage of an
index if that index is specified in a hint.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, the following query causes MongoDB to use the
<code class="leafygreen-ui-1l06pbn">
timestamp_1_metadata.sensorId_1
</code>
index:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sensorData.find( { \"metadata.sensorId\": 5578 } ).hint( \"timestamp_1_metadata.sensorId_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">sensorData</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-string">"metadata.sensorId"</span>: <span class="lg-highlight-number">5578</span> } ).<span class="lg-highlight-title lg-highlight-function">hint</span>( <span class="lg-highlight-string">"timestamp_1_metadata.sensorId_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">
On a time series collection, you can specify hints using either the
index name or the index key pattern. To get the names of the indexes on
a collection, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.getIndexes/#mongodb-method-db.collection.getIndexes">
<code class="leafygreen-ui-1l06pbn">
db.collection.getIndexes()
</code>
</a>
method.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Create 2dsphere Indexes
<a class="headerlink leafygreen-ui-1kepx47" href="#create-2dsphere-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="create-2dsphere-indexes">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in version 6.0 you can create 2dsphere indexes on the
<code class="leafygreen-ui-1l06pbn">
timeField
</code>
,
<code class="leafygreen-ui-1l06pbn">
metaField
</code>
, or measurement fields.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, the following operation creates a 2dsphere index on the
<code class="leafygreen-ui-1l06pbn">
location
</code>
field:
</p>
<div class="leafygreen-ui-vg7qwp" role="note">
<h2 class="leafygreen-ui-2xicti">
Example
</h2>
<div class="leafygreen-ui-9mycqc">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sensorData.createIndex({ \"metadata.location\": \"2dsphere\" })","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">sensorData</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>(<!-- -->{ <span class="lg-highlight-string">"metadata.location"</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>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Additionally, the following operation creates a 2dsphere index on measurement
fields:
</p>
<div class="leafygreen-ui-vg7qwp" role="note">
<h2 class="leafygreen-ui-2xicti">
Example
</h2>
<div class="leafygreen-ui-9mycqc">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sensorData.createIndex({ \"currentConditions.tempF\": \"2dsphere\" })","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">sensorData</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>(<!-- -->{ <span class="lg-highlight-string">"currentConditions.tempF"</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>
</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
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary-index">
secondary indexes
</a>
on
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries-collections/#std-label-manual-timeseries-collection">
time
series collections
</a>
and you need to
downgrade the feature compatibility version (fCV), you must first drop
any secondary indexes that are incompatible with the downgraded fCV.
For more information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setFeatureCompatibilityVersion/#mongodb-dbcommand-dbcmd.setFeatureCompatibilityVersion">
<code class="leafygreen-ui-1l06pbn">
setFeatureCompatibilityVersion
</code>
.
</a>
</p>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/query/minDistance/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$minDistance
<a class="headerlink leafygreen-ui-1kepx47" href="#-mindistance" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-mindistance">
</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="#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">
$minDistance
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-query-op.-minDistance" 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.-minDistance">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Filters the results of a 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>
query to those documents that are
<em>
at least
</em>
the
specified distance from the center point.
</p>
<p class="leafygreen-ui-1kp3ins">
If
<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>
query specifies the center
point as a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/geojson/#std-label-geojson-point">
GeoJSON point
</a>
, specify the distance
as a non-negative number in
<em>
meters
</em>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/nearSphere/#mongodb-query-op.-nearSphere">
<code class="leafygreen-ui-1l06pbn">
$nearSphere
</code>
</a>
query specifies the center point as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-legacy-coordinate-pairs">
legacy coordinate pair
</a>
, specify
the distance as a non-negative number in
<em>
radians
</em>
.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/near/#mongodb-query-op.-near">
<code class="leafygreen-ui-1l06pbn">
$near
</code>
</a>
can only use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2dsphere/#std-label-2dsphere-index">
2dsphere
</a>
index if the query
specifies the center point as a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/geojson/#std-label-geojson-point">
GeoJSON point
<!-- -->
.
</a>
</p>
</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>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Use with
<code class="leafygreen-ui-1l06pbn">
$near
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#use-with--near" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-with--near">
</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">
If specifying latitude and longitude 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>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Consider a collection
<code class="leafygreen-ui-1l06pbn">
places
</code>
that has a
<code class="leafygreen-ui-1l06pbn">
2dsphere
</code>
index.
</p>
<p class="leafygreen-ui-1kp3ins">
The following example returns documents that are at least
<code class="leafygreen-ui-1l06pbn">
1000
</code>
meters from and at most
<code class="leafygreen-ui-1l06pbn">
5000
</code>
meters from the specified GeoJSON
point, sorted from nearest to farthest:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.places.find(\n {\n location:\n { $near :\n {\n $geometry: { type: \"Point\", coordinates: [ -73.9667, 40.78 ] },\n $minDistance: 1000,\n $maxDistance: 5000\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">location</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->$near<!-- --> <!-- -->:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$geometry</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.9667</span>, <span class="lg-highlight-number">40.78</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$minDistance</span>: <span class="lg-highlight-number">1000</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$maxDistance</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><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">
Use with
<code class="leafygreen-ui-1l06pbn">
$nearSphere
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#use-with--nearsphere" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-with--nearsphere">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Consider a collection
<code class="leafygreen-ui-1l06pbn">
places
</code>
that contains documents with a
<code class="leafygreen-ui-1l06pbn">
location
</code>
field and has a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2dsphere/#std-label-2dsphere-index">
2dsphere
</a>
index.
</p>
<p class="leafygreen-ui-1kp3ins">
Then, the following example returns whose
<code class="leafygreen-ui-1l06pbn">
location
</code>
is at least
<code class="leafygreen-ui-1l06pbn">
1000
</code>
meters from and at most
<code class="leafygreen-ui-1l06pbn">
5000
</code>
meters from the specified
point, ordered from nearest to farthest:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.places.find(\n {\n location: {\n $nearSphere: {\n $geometry: {\n type : \"Point\",\n coordinates : [ -73.9667, 40.78 ]\n },\n $minDistance: 1000,\n $maxDistance: 5000\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"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">location</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$nearSphere</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">"Point"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->coordinates <!-- -->: <!-- -->[ <!-- -->-<span class="lg-highlight-number">73.9667</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"> <span class="lg-highlight-attr">$minDistance</span>: <span class="lg-highlight-number">1000</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$maxDistance</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><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 an example that specifies the center point as legacy coordinate
pair, see
<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>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/isArray/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$isArray (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-isarray--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="-isarray--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">
$isArray
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-isArray" 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.-isArray">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Determines if the operand is an array. Returns a boolean.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-isArray" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$isArray
</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":"{ $isArray: [ <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">$isArray</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>
</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
<code class="leafygreen-ui-1l06pbn">
<expression>
</code>
can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
. 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">
<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>
<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>
<code class="leafygreen-ui-1l06pbn">
{ $isArray: "hello" }
</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>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
"hello"
</code>
is a string, passed as a string.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $isArray: [ "hello" ] }
</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>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
"hello"
</code>
is a string, passed as part of an argument array.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $isArray: [ [ "hello" ] ] }
</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>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
[ "hello" ]
</code>
is an array, passed as part of an argument array.
</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">
Aggregation expressions accept a variable number of arguments. These
arguments are normally passed as an array. However, when the argument
is a single value, you can simplify your code by passing the argument
directly without wrapping it in an array.
</p>
</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 a collection named
<code class="leafygreen-ui-1l06pbn">
warehouses
</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.warehouses.insertMany( [\n { _id : 1, instock: [ \"chocolate\" ], ordered: [ \"butter\", \"apples\" ] },\n { _id : 2, instock: [ \"apples\", \"pudding\", \"pie\" ] },\n { _id : 3, instock: [ \"pears\", \"pecans\" ], ordered: [ \"cherries\" ] },\n { _id : 4, instock: [ \"ice cream\" ], ordered: [ ] }\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">warehouses</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>, <span class="lg-highlight-attr">instock</span>: <!-- -->[ <span class="lg-highlight-string">"chocolate"</span> ]<!-- -->, <span class="lg-highlight-attr">ordered</span>: <!-- -->[ <span class="lg-highlight-string">"butter"</span>, <span class="lg-highlight-string">"apples"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">instock</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"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">instock</span>: <!-- -->[ <span class="lg-highlight-string">"pears"</span>, <span class="lg-highlight-string">"pecans"</span> ]<!-- -->, <span class="lg-highlight-attr">ordered</span>: <!-- -->[ <span class="lg-highlight-string">"cherries"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-attr">instock</span>: <!-- -->[ <span class="lg-highlight-string">"ice cream"</span> ]<!-- -->, <span class="lg-highlight-attr">ordered</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">
Check if the
<code class="leafygreen-ui-1l06pbn">
instock
</code>
and the
<code class="leafygreen-ui-1l06pbn">
ordered
</code>
fields are arrays. If both
fields are arrays, concatenate them:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.warehouses.aggregate( [\n { $project:\n { items:\n { $cond:\n {\n if: { $and: [ { $isArray: \"$instock\" },\n { $isArray: \"$ordered\" }\n ] },\n then: { $concatArrays: [ \"$instock\", \"$ordered\" ] },\n else: \"One or more fields is not an array.\"\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">warehouses</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"> <!-- -->{ <span class="lg-highlight-attr">items</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$cond</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">if</span>: <!-- -->{ <span class="lg-highlight-attr">$and</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$isArray</span>: <span class="lg-highlight-string">"$instock"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$isArray</span>: <span class="lg-highlight-string">"$ordered"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">then</span>: <!-- -->{ <span class="lg-highlight-attr">$concatArrays</span>: <!-- -->[ <span class="lg-highlight-string">"$instock"</span>, <span class="lg-highlight-string">"$ordered"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">else</span>: <span class="lg-highlight-string">"One or more fields is not an 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"> }</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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ _id : 1, items : [ \"chocolate\", \"butter\", \"apples\" ] }\n{ _id : 2, items : \"One or more fields is not an array.\" }\n{ _id : 3, items : [ \"pears\", \"pecans\", \"cherries\" ] }\n{ _id : 4, items : [ \"ice cream\" ] }","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">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">1</span>, <!-- -->items <!-- -->: <!-- -->[ <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"butter"</span>, <span class="lg-highlight-string">"apples"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">2</span>, <!-- -->items <!-- -->: <span class="lg-highlight-string">"One or more fields is not an array."</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">3</span>, <!-- -->items <!-- -->: <!-- -->[ <span class="lg-highlight-string">"pears"</span>, <span class="lg-highlight-string">"pecans"</span>, <span class="lg-highlight-string">"cherries"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">4</span>, <!-- -->items <!-- -->: <!-- -->[ <span class="lg-highlight-string">"ice cream"</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">
<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/cond/#mongodb-expression-exp.-cond">
<code class="leafygreen-ui-1l06pbn">
$cond
</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/concatArrays/#mongodb-expression-exp.-concatArrays">
<code class="leafygreen-ui-1l06pbn">
$concatArrays
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/capped-collections/create-capped-collection/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Create a Capped Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#create-a-capped-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-capped-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="#steps" target="_self">
<span>
Steps
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#create-a-capped-collection-with-a-maximum-size" target="_self">
<span>
Create a Capped Collection with a Maximum Size
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#create-a-capped-collection-with-a-maximum-number-of-documents" target="_self">
<span>
Create a Capped Collection with a Maximum Number of Documents
</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 create a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/capped-collections/#std-label-manual-capped-collection">
capped collection
</a>
, specify
the
<code class="leafygreen-ui-1l06pbn">
capped
</code>
option to either the
<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>
method or the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/create/#mongodb-dbcommand-dbcmd.create">
<code class="leafygreen-ui-1l06pbn">
create
</code>
</a>
command.
</p>
<p class="leafygreen-ui-1kp3ins">
You must create capped collections explicitly. You cannot create a
capped collection implicitly by inserting data into a non-existing
collection.
</p>
<p class="leafygreen-ui-1kp3ins">
When you create a capped collection you must specify the maximum size of
the collection. MongoDB pre-allocates the specified storage for the
collection. The size of the capped collection includes a small amount of
space for internal overhead.
</p>
<p class="leafygreen-ui-1kp3ins">
You can optionally specify a maximum number of documents for the
collection. MongoDB removes older documents if the collection reaches
the maximum size limit before it reaches the maximum document count.
</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">
Generally,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-ttl/#std-label-index-feature-ttl">
TTL (Time To Live) indexes
</a>
offer
better performance and more flexibility than capped collections. TTL
indexes expire and remove data from normal collections based on the
value of a date-typed field and a TTL value for the index.
</p>
<p class="leafygreen-ui-1kp3ins">
Capped collections serialize write operations and therefore have worse
concurrent insert, update, and delete performance than non-capped
collections. Before you create a capped collection, consider if you
can use a TTL index instead.
</p>
</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>
<p class="leafygreen-ui-1kp3ins">
The following examples show you how to:
</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="#std-label-create-capped-collection-max-size" target="_self">
<span>
Create a Capped Collection with a Maximum Size
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-create-capped-collection-max-docs" target="_self">
<span>
Create a Capped Collection with a Maximum Number of Documents
</span>
</a>
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-create-capped-collection-max-size">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Create a Capped Collection with a Maximum Size
<a class="headerlink leafygreen-ui-1kepx47" href="#create-a-capped-collection-with-a-maximum-size" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-capped-collection-with-a-maximum-size">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Create a capped collection called
<code class="leafygreen-ui-1l06pbn">
log
</code>
that has a maximum size of
100,000 bytes:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.createCollection( \"log\", { capped: true, size: 100000 } )","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">"log"</span>, <!-- -->{ <span class="lg-highlight-attr">capped</span>: <span class="lg-highlight-literal">true</span>, <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-number">100000</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">
The value that you provide for the
<code class="leafygreen-ui-1l06pbn">
size
</code>
field
must be greater than
<code class="leafygreen-ui-1l06pbn">
0
</code>
and less than or equal to
<code class="leafygreen-ui-1l06pbn">
1024^5
</code>
(1
<span class="leafygreen-ui-vm4wms" tabindex="0">
PB
</span>
). MongoDB rounds the
<code class="leafygreen-ui-1l06pbn">
size
</code>
of all capped
collections up to the nearest integer multiple of 256, in bytes.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-create-capped-collection-max-docs">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Create a Capped Collection with a Maximum Number of Documents
<a class="headerlink leafygreen-ui-1kepx47" href="#create-a-capped-collection-with-a-maximum-number-of-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="create-a-capped-collection-with-a-maximum-number-of-documents">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Create a capped collection called
<code class="leafygreen-ui-1l06pbn">
log2
</code>
that has a maximum size of
5,242,880 bytes and can store a maximum of 5,000 documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.createCollection(\n \"log2\",\n {\n capped: true,\n size: 5242880,\n max: 5000\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">createCollection</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"log2"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">capped</span>: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">size</span>: <span class="lg-highlight-number">5242880</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">max</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>
<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">
size
</code>
field is always required, even when you specify the
<code class="leafygreen-ui-1l06pbn">
max
</code>
number of documents.
</p>
</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/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/core/capped-collections/query-capped-collection/#std-label-capped-collections-query">
Query a Capped Collection
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/capped-collections/check-if-collection-is-capped/#std-label-capped-collections-check">
Check if a Collection is Capped
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/Bulk.find.remove/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Bulk.find.remove()
<a class="headerlink leafygreen-ui-1kepx47" href="#bulk.find.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="bulk.find.remove--">
</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>
<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.remove()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-Bulk.find.remove" 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.remove">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Starting in
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
0.12.2,
<code class="leafygreen-ui-1l06pbn">
Bulk.find.remove()
</code>
is an alias
for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Bulk.find.delete/#mongodb-method-Bulk.find.delete">
<code class="leafygreen-ui-1l06pbn">
Bulk.find.delete()
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
In new code, use
<code class="leafygreen-ui-1l06pbn">
Bulk.find.delete()
</code>
instead of
<code class="leafygreen-ui-1l06pbn">
Bulk.find.remove()
</code>
.
</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 the
<code class="leafygreen-ui-1l06pbn">
music
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.music.insertMany( [\n { artist: \"DOA\", genre: \"punk\" },\n { artist: \"Rick Astley\", genre: \"pop\" },\n { artist: \"Black Flag\", genre: \"punk\" },\n { artist: \"Justin Bieber\", genre: \"pop\" }\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">music</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">artist</span>: <span class="lg-highlight-string">"DOA"</span>, <span class="lg-highlight-attr">genre</span>: <span class="lg-highlight-string">"punk"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">artist</span>: <span class="lg-highlight-string">"Rick Astley"</span>, <span class="lg-highlight-attr">genre</span>: <span class="lg-highlight-string">"pop"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">artist</span>: <span class="lg-highlight-string">"Black Flag"</span>, <span class="lg-highlight-attr">genre</span>: <span class="lg-highlight-string">"punk"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">artist</span>: <span class="lg-highlight-string">"Justin Bieber"</span>, <span class="lg-highlight-attr">genre</span>: <span class="lg-highlight-string">"pop"</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:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
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.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Searches for the genre
<code class="leafygreen-ui-1l06pbn">
pop
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Deletes
<code class="leafygreen-ui-1l06pbn">
pop
</code>
music from the collection.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var bulk = db.music.initializeOrderedBulkOp();\nbulk.find( { \"genre\": \"pop\" } ).remove();\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">music</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">find</span>( <!-- -->{ <span class="lg-highlight-string">"genre"</span>: <span class="lg-highlight-string">"pop"</span> } ).<span class="lg-highlight-title lg-highlight-function">remove</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>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/db.serverBuildInfo/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.serverBuildInfo()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.serverbuildinfo--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.serverbuildinfo--">
</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>
</ul>
</div>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
db.serverBuildInfo()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.serverBuildInfo" 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.serverBuildInfo">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Provides a wrapper around the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/buildInfo/#mongodb-dbcommand-dbcmd.buildInfo">
<code class="leafygreen-ui-1l06pbn">
buildInfo
</code>
</a>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-database-command">
database command
</a>
.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/buildInfo/#mongodb-dbcommand-dbcmd.buildInfo">
<code class="leafygreen-ui-1l06pbn">
buildInfo
</code>
</a>
returns a document that contains
an overview of parameters used to compile this
<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>
</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">
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>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/planCacheStats/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$planCacheStats
<a class="headerlink leafygreen-ui-1kepx47" href="#-plancachestats" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-plancachestats">
</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-x9koo9" href="#pipeline" target="_self">
<span>
Pipeline
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#restrictions" target="_self">
<span>
Restrictions
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#access-control" target="_self">
<span>
Access Control
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#redaction" target="_self">
<span>
Redaction
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#read-preference" target="_self">
<span>
Read Preference
</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>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#return-information-for-all-entries-in-the-query-cache" target="_self">
<span>
Return Information for All Entries in the Query Cache
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#find-cache-entry-details-for-a-query-hash" target="_self">
<span>
Find Cache Entry Details for a Query Hash
</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">
$planCacheStats
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-pipeline-pipe.-planCacheStats" 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.-planCacheStats">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-query-plans-query-optimization">
plan cache
</a>
information for a
collection. The stage returns a document for each plan cache entry.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
stage must be the first stage in the
pipeline. The stage has the following syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $planCacheStats: {\n allHosts: <boolean>\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">$planCacheStats</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">allHosts</span>: <<!-- -->boolean<!-- -->></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">
$planCacheStats
</code>
aggregation stage has the following options:
</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>
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">
allHosts
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Configures how the
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
aggregation stage targets
nodes in a sharded cluster.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
true
</code>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#std-program-mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
broadcasts the
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
aggregation stage to all nodes (primary and secondaries) for each
affected shard that contains one or more chunks from the target
collection.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
false
</code>
, the
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
aggregation stage follows the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#std-label-read-preference">
Read Preference
</a>
and only retrieves the plan cache from the
targeted replica set primary.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Replica sets and standalone servers return an error during pipeline
parsing if
<code class="leafygreen-ui-1l06pbn">
allHosts
</code>
is set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
. The option is only
available to sharded clusters.
</p>
<p class="leafygreen-ui-1kp3ins">
Default:
<code class="leafygreen-ui-1l06pbn">
false
</code>
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
7.1
</em>
.
</p>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</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/core/query-plans/#std-label-query-plans-query-optimization">
Query Plans
</a>
</p>
</div>
</div>
</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">
Pipeline
<a class="headerlink leafygreen-ui-1kepx47" href="#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="pipeline">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
must be the first stage in an aggregation pipeline.
</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>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
is not allowed in:
</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/transactions/#std-label-transactions">
transactions
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/facet/#mongodb-pipeline-pipe.-facet">
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
</a>
aggregation stage
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
requires read concern level
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-local/#mongodb-readconcern-readconcern.-local-">
<code class="leafygreen-ui-1l06pbn">
"local"
</code>
.
</a>
</p>
</li>
</ul>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" 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>
</h3>
<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-planCacheRead">
<code class="leafygreen-ui-1l06pbn">
planCacheRead
</code>
</a>
privilege for the collection.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-read-pref">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Redaction
<a class="headerlink leafygreen-ui-1kepx47" href="#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="redaction">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/queryable-encryption/#std-label-qe-manual-feature-qe">
Queryable Encryption
</a>
, the
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
stage omits operations against encrypted collections, even
though the operations are cached as normal.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
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>
</h3>
<p class="leafygreen-ui-1kp3ins">
When the
<code class="leafygreen-ui-1l06pbn">
allHosts
</code>
option is set to
<code class="leafygreen-ui-1l06pbn">
false
</code>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-planCacheStats" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
</span>
</a>
follows the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#std-label-read-preference">
read preference
</a>
in selecting the host(s) from which to return
the plan cache information.
</p>
<p class="leafygreen-ui-1kp3ins">
Applications may target different members of a replica set. As such,
each replica set member might receive different read commands and have
plan cache information that differs from other members. Nevertheless,
running
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
on a replica set or a sharded
cluster obeys the normal read preference rules. That is, on a replica
set, the operation gathers plan cache information from just one member
of replica set, and on a sharded cluster, the operation gathers plan
cache information from just one member of each shard replica set.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-planCacheStats-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>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
7.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
The output of
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
depends on the query engine used to
complete the query. The value of the
<code class="leafygreen-ui-1l06pbn">
version
</code>
field of the
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
indicates which query engine was used:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
1
</code>
indicates that the classic engine was used.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
2
</code>
indicates that the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/sbe/#std-label-sbe-landing">
slot-based query execution engine
</a>
was used.
</p>
</li>
</ul>
<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 classic-sample-output/sbe-sample-output" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_sdpvc271r" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_0pugiyqok" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Classic Execution Engine
</div>
</button>
<button aria-controls="_j7tki8dh2" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_6sy7smhmi" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Slot-Based Query Execution Engine
</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="_ittoffrnz" class="" id="_12quvj7ti" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
For queries that use the classic execution engine,
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
returns a document similar to the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"version\" : 1,\n \"createdFromQuery\" : <document>,\n \"planCacheShapeHash\" : <hexadecimal>,\n \"planCacheKey\" : <hexadecimal>,\n \"isActive\" : <boolean>,\n \"works\" : <numberlong>,\n \"cachedPlan\" : {\n \"stage\" : <stage1>,\n \"filter\" : <document>,\n \"inputStage\" : {\n \"stage\" : <stage2>,\n ...\n }\n },\n \"timeOfCreation\" : <date>,\n \"creationExecStats\" : [ // Exec Stats Document for each candidate plan\n {\n \"nReturned\" : <num>,\n \"executionTimeMillisEstimate\" : <num>,\n \"totalKeysExamined\" : <num>,\n \"totalDocsExamined\" :<num>,\n \"executionStages\" : {\n \"stage\" : <stage>,\n ...\n \"inputStage\" : {\n \"stage\" : <stage>,\n ...\n }\n }\n },\n ...\n ],\n \"candidatePlanScores\" : [\n <number>,\n ...\n ],\n \"indexFilterSet\" : <boolean>,\n \"estimatedSizeBytes\" : <num>,\n \"querySettings\" : <document>,\n \"host\" : <string>,\n \"shard\" : <string>\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">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "version" : 1,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "createdFromQuery" : <document>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "planCacheShapeHash" : <hexadecimal string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "planCacheKey" : <hexadecimal string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "isActive" : <boolean>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "works" : <NumberLong>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "cachedPlan" : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "stage" : <STAGE1>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "filter" : <document>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "inputStage" : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "stage" : <STAGE2>,</td></tr><tr 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"> "timeOfCreation" : <date>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "creationExecStats" : [ // Exec Stats Document for each candidate plan</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "nReturned" : <num>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "executionTimeMillisEstimate" : <num>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "totalKeysExamined" : <num>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "totalDocsExamined" :<num>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "executionStages" : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "stage" : <STAGE A>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "inputStage" : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "stage" : <STAGE B>,</td></tr><tr 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><tr class=""><td class="leafygreen-ui-7razhx"> "candidatePlanScores" : [</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <number>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ],</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "indexFilterSet" : <boolean>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "estimatedSizeBytes" : <num>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "querySettings" : <document>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "host" : <string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "shard" : <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">
Each document includes various query plan and execution stats,
including:
</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>
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">
version
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-planCacheStats-classic-version">
</span>
<p class="leafygreen-ui-1kp3ins">
A number that indicates the query engine used to complete the query.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
1
</code>
indicates that the classic engine was used.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
2
</code>
indicates that the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/sbe/#std-label-sbe-landing">
slot-based query execution engine
</a>
was used.
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
createdFromQuery
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-createdFromQuery">
</span>
<p class="leafygreen-ui-1kp3ins">
A document that contains the specific query that resulted in
this cache entry. For example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"query\" : <document>,\n \"sort\" : <document>,\n \"projection\" : <document>\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">"query"</span> <!-- -->: <<span class="lg-highlight-variable lg-highlight-language">document</span>><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"sort"</span> <!-- -->: <<span class="lg-highlight-variable lg-highlight-language">document</span>><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"projection"</span> <!-- -->: <<span class="lg-highlight-variable lg-highlight-language">document</span>></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</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>
<code class="leafygreen-ui-1l06pbn">
isActive
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
A boolean that indicates whether the entry is active or inactive.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If active, the query planner is currently using the entry to generate
query plans.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If inactive, the query planner is not currently using the entry to
generate query plans.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-cache-entry-state">
Plan Cache Entry State
<!-- -->
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-planCacheShapeHash">
</span>
<p class="leafygreen-ui-1kp3ins">
A hexadecimal string that represents the hash of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-query-shape">
query shape
</a>
. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.queryPlanner.planCacheShapeHash">
<code class="leafygreen-ui-1l06pbn">
explain.queryPlanner.planCacheShapeHash
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, the pre-existing
<code class="leafygreen-ui-1l06pbn">
queryHash
</code>
field is renamed
to
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
. If you're using an earlier MongoDB version,
you'll see
<code class="leafygreen-ui-1l06pbn">
queryHash
</code>
instead of
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
planCacheKey
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-planCacheKey">
</span>
<p class="leafygreen-ui-1kp3ins">
A hexadecimal string that represents the hash of the key used to find
the plan cache entry associated with this query. The plan cache key is a
function of both the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-plan-cache-query-shape">
plan cache query shape
</a>
and the currently available indexes for
that shape. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.queryPlanner.planCacheKey">
<code class="leafygreen-ui-1l06pbn">
explain.queryPlanner.planCacheKey
</code>
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
cachedPlan
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-cachedPlan">
</span>
<p class="leafygreen-ui-1kp3ins">
The details of the cached plan. The fields included in the
<code class="leafygreen-ui-1l06pbn">
cachedPlan
</code>
vary based on whether the query was completed using the
classic engine or the slot-based query execution engine. For more information on query plans, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.queryPlanner">
<code class="leafygreen-ui-1l06pbn">
explain.queryPlanner
</code>
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
works
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
The number of "work units" performed by the query execution plan during
the trial period when the query planner evaluates candidate plans. For
more information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.executionStats.executionStages.works">
<code class="leafygreen-ui-1l06pbn">
explain.executionStats.executionStages.works
</code>
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
timeOfCreation
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Time of creation for the entry.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
creationExecStats
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-creationExecStats">
</span>
<p class="leafygreen-ui-1kp3ins">
An array of execution stats documents. The array contains a
document for each candidate plan.
</p>
<p class="leafygreen-ui-1kp3ins">
For details on the execution stats, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.executionStats">
<code class="leafygreen-ui-1l06pbn">
explain.executionStats
</code>
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
candidatePlanScores
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-candidatePlanScores">
</span>
<p class="leafygreen-ui-1kp3ins">
An array of scores for the candidate plans listed in the
<code class="leafygreen-ui-1l06pbn">
creationExecStats
</code>
array.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
indexFilterSet
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
A boolean that indicates whether an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-index-filters">
index filter
</a>
exists for the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-plan-cache-query-shape">
plan cache query shape
<!-- -->
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
estimatedSizeBytes
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
The estimated size in bytes of a plan cache entry.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
querySettings
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-querySettings">
</span>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
8.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
A document that contains query settings previously set using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setQuerySettings/#mongodb-dbcommand-dbcmd.setQuerySettings">
<code class="leafygreen-ui-1l06pbn">
setQuerySettings
</code>
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"querySettings: {\n indexHints: [ {\n ns: { db: <string>, coll: <string> },\n allowedIndexes: <array>\n }, ... ],\n queryFramework: <string>\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"><span class="lg-highlight-attr">querySettings</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">indexHints</span>: <!-- -->[ <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ns</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <<!-- -->string<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">coll</span>: <<!-- -->string<!-- -->> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">allowedIndexes</span>: <<!-- -->array<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->, <!-- -->...<!-- --> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">queryFramework</span>: <<!-- -->string<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
querySettings
</code>
fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:5%"/>
<col style="width:35%"/>
<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="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
indexHints.ns
</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">
Namespace for index hints.
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:5%"/>
<col style="width:35%"/>
<col style="width:50%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
db
</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>
Name of the database for index hints.
</div>
</div>
</td>
</tr>
<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>
Name of the collection for index hints.
</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">
indexHints.allowedIndexes
</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>
Array of indexes for index hints. For more details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/indexes/#std-label-indexes">
Indexes
</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>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
queryFramework
</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">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-query-framework">
Query framework
</a>
string can be:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
classic
</code>
for the classic engine.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
sbe
</code>
for slot-based query execution engine. For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/sbe/#std-label-sbe-landing">
Slot-Based Query Execution Engine
<!-- -->
.
</a>
</p>
</li>
</ul>
</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">
host
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-host">
</span>
<p class="leafygreen-ui-1kp3ins">
The hostname and port of 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 which
the plan cache information was returned.
</p>
<p class="leafygreen-ui-1kp3ins">
When run on a sharded cluster, the operation returns plan cache entry
information from a single member in each shard replica set. This member
is identified with the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-plancachestats-shard" target="_self">
<span>
shard
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-plancachestats-host" target="_self">
<span>
host
</span>
</a>
fields. See also
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-plancachestats-read-pref" target="_self">
<span>
Redaction
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
shard
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-shard">
</span>
<p class="leafygreen-ui-1kp3ins">
The name of the shard from which
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-planCacheStats" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
</span>
</a>
retrieved the cache entry.
</p>
<p class="leafygreen-ui-1kp3ins">
Only available if run on a sharded cluster.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div>
<div aria-labelledby="_0330ktcz5" class="leafygreen-ui-38lglc" id="_k01vgrqhb" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
For queries that use the slot-based query execution engine,
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
returns a
document similar to the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"version\" : 2,\n \"planCacheShapeHash\" : <hexadecimal>,\n \"planCacheKey\" : <hexadecimal>,\n \"isActive\" : <boolean>,\n \"works\" : <numberlong>,\n \"cachedPlan\" : {\n \"slots\" : <string>,\n \"stages\": <string>\n },\n \"indexFilterSet\" : <boolean>,\n \"estimatedSizeBytes\" : <num>,\n \"querySettings\" : <document>,\n \"host\" : <string>\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">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "version" : 2,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "planCacheShapeHash" : <hexadecimal string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "planCacheKey" : <hexadecimal string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "isActive" : <boolean>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "works" : <NumberLong>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "cachedPlan" : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "slots" : <string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "stages": <string></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "indexFilterSet" : <boolean>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "estimatedSizeBytes" : <num>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "querySettings" : <document>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> "host" : <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">
Each document includes various query plan and execution stats,
including:
</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>
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">
version
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-planCacheStats-sbe-version">
</span>
<p class="leafygreen-ui-1kp3ins">
A number that indicates the query engine used to complete the query.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
1
</code>
indicates that the classic engine was used.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
2
</code>
indicates that the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/sbe/#std-label-sbe-landing">
slot-based query execution engine
</a>
was used.
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-sbe-planCacheShapeHash">
</span>
<p class="leafygreen-ui-1kp3ins">
A hexadecimal string that represents the hash of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-query-shape">
query shape
</a>
. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.queryPlanner.planCacheShapeHash">
<code class="leafygreen-ui-1l06pbn">
explain.queryPlanner.planCacheShapeHash
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, the pre-existing
<code class="leafygreen-ui-1l06pbn">
queryHash
</code>
field is renamed
to
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
. If you're using an earlier MongoDB version,
you'll see
<code class="leafygreen-ui-1l06pbn">
queryHash
</code>
instead of
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
planCacheKey
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-sbe-planCacheKey">
</span>
<p class="leafygreen-ui-1kp3ins">
A hexadecimal string that represents the hash of the key used to find
the plan cache entry associated with this query. The plan cache key is a
function of both the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-plan-cache-query-shape">
plan cache query shape
</a>
and the currently available indexes for
that shape. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.queryPlanner.planCacheKey">
<code class="leafygreen-ui-1l06pbn">
explain.queryPlanner.planCacheKey
</code>
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
isActive
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-sbe-isActive">
</span>
<p class="leafygreen-ui-1kp3ins">
A boolean that indicates whether the entry is active or inactive.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If active, the query planner is currently using the entry to generate
query plans.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If inactive, the query planner is not currently using the entry to
generate query plans.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-cache-entry-state">
Plan Cache Entry State
<!-- -->
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
works
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-sbe-works">
</span>
<p class="leafygreen-ui-1kp3ins">
The number of "work units" performed by the query execution plan during
the trial period when the query planner evaluates candidate plans. For
more information, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.executionStats.executionStages.works">
<code class="leafygreen-ui-1l06pbn">
explain.executionStats.executionStages.works
</code>
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
cachedPlan
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-sbe-cachedPlan">
</span>
<p class="leafygreen-ui-1kp3ins">
The details of the cached plan. The fields included in the
<code class="leafygreen-ui-1l06pbn">
cachedPlan
</code>
vary based on whether the query was completed using the
classic engine or the slot-based query execution engine. For more information on query plans, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/explain-results/#mongodb-data-explain.queryPlanner">
<code class="leafygreen-ui-1l06pbn">
explain.queryPlanner
</code>
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
indexFilterSet
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-sbe-indexFilterSet">
</span>
<p class="leafygreen-ui-1kp3ins">
A boolean that indicates whether an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-index-filters">
index filter
</a>
exists for the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-plan-cache-query-shape">
plan cache query shape
<!-- -->
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
estimatedSizeBytes
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-sbe-estimatedSizeBytes">
</span>
<p class="leafygreen-ui-1kp3ins">
The estimated size in bytes of a plan cache entry.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
querySettings
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-sbe-querySettings">
</span>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
8.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
A document that contains query settings previously set using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setQuerySettings/#mongodb-dbcommand-dbcmd.setQuerySettings">
<code class="leafygreen-ui-1l06pbn">
setQuerySettings
</code>
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"querySettings: {\n indexHints: [ {\n ns: { db: <string>, coll: <string> },\n allowedIndexes: <array>\n }, ... ],\n queryFramework: <string>\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"><span class="lg-highlight-attr">querySettings</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">indexHints</span>: <!-- -->[ <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">ns</span>: <!-- -->{ <span class="lg-highlight-attr">db</span>: <<!-- -->string<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">coll</span>: <<!-- -->string<!-- -->> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">allowedIndexes</span>: <<!-- -->array<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->, <!-- -->...<!-- --> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">queryFramework</span>: <<!-- -->string<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx">}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
querySettings
</code>
fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:5%"/>
<col style="width:35%"/>
<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="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
indexHints.ns
</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">
Namespace for index hints.
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:5%"/>
<col style="width:35%"/>
<col style="width:50%"/>
</colgroup>
<thead class="leafygreen-ui-a9ho40">
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
db
</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>
Name of the database for index hints.
</div>
</div>
</td>
</tr>
<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>
Name of the collection for index hints.
</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">
indexHints.allowedIndexes
</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>
Array of indexes for index hints. For more details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/indexes/#std-label-indexes">
Indexes
</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>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
queryFramework
</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">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-query-framework">
Query framework
</a>
string can be:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
classic
</code>
for the classic engine.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
sbe
</code>
for slot-based query execution engine. For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/sbe/#std-label-sbe-landing">
Slot-Based Query Execution Engine
<!-- -->
.
</a>
</p>
</li>
</ul>
</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">
host
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-plancachestats-sbe-host">
</span>
<p class="leafygreen-ui-1kp3ins">
The hostname and port of 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 which
the plan cache information was returned.
</p>
<p class="leafygreen-ui-1kp3ins">
When run on a sharded cluster, the operation returns plan cache entry
information from a single member in each shard replica set. This member
is identified with the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-plancachestats-shard" target="_self">
<span>
shard
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-plancachestats-host" target="_self">
<span>
host
</span>
</a>
fields. See also
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-plancachestats-read-pref" target="_self">
<span>
Redaction
<!-- -->
.
</span>
</a>
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-planCacheStats-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>
<p class="leafygreen-ui-1kp3ins">
The examples in this section use the following
<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.insertMany( [\n { \"_id\" : 1, \"item\" : \"abc\", \"price\" : NumberDecimal(\"12\"), \"quantity\" : 2, \"type\": \"apparel\" },\n { \"_id\" : 2, \"item\" : \"jkl\", \"price\" : NumberDecimal(\"20\"), \"quantity\" : 1, \"type\": \"electronics\" },\n { \"_id\" : 3, \"item\" : \"abc\", \"price\" : NumberDecimal(\"10\"), \"quantity\" : 5, \"type\": \"apparel\" },\n { \"_id\" : 4, \"item\" : \"abc\", \"price\" : NumberDecimal(\"8\"), \"quantity\" : 10, \"type\": \"apparel\" },\n { \"_id\" : 5, \"item\" : \"jkl\", \"price\" : NumberDecimal(\"15\"), \"quantity\" : 15, \"type\": \"electronics\" }\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">orders</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">"abc"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"12"</span>)<!-- -->, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"apparel"</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">"jkl"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"20"</span>)<!-- -->, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"electronics"</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">"abc"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"10"</span>)<!-- -->, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"apparel"</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">"abc"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"8"</span>)<!-- -->, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"apparel"</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">"jkl"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"15"</span>)<!-- -->, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">15</span>, <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"electronics"</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">
Create the following indexes on the collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.createIndex( { item: 1 } );\ndb.orders.createIndex( { item: 1, quantity: 1 } );\ndb.orders.createIndex( { quantity: 1 } );\ndb.orders.createIndex( { quantity: 1, type: 1 } );\ndb.orders.createIndex(\n { item: 1, price: 1 },\n { partialFilterExpression: { price: { $gte: NumberDecimal(\"10\")} } }\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">orders</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <!-- -->{ <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-number">1</span> } )<!-- -->;</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">item</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">1</span> } )<!-- -->;</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">quantity</span>: <span class="lg-highlight-number">1</span> } )<!-- -->;</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">quantity</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-number">1</span> } )<!-- -->;</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>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">partialFilterExpression</span>: <!-- -->{ <span class="lg-highlight-attr">price</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"10"</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-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Index
<code class="leafygreen-ui-1l06pbn">
{ item: 1, price: 1 }
</code>
is a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-partial/#std-label-index-type-partial">
partial index
</a>
and only indexes documents with
<code class="leafygreen-ui-1l06pbn">
price
</code>
field greater than or equal to
<code class="leafygreen-ui-1l06pbn">
NumberDecimal("10")
</code>
.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Run some queries against the collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.find( { item: \"abc\", price: { $gte: NumberDecimal(\"10\") } } )\ndb.orders.find( { item: \"abc\", price: { $gte: NumberDecimal(\"5\") } } )\ndb.orders.find( { quantity: { $gte: 20 } } )\ndb.orders.find( { quantity: { $gte: 5 }, type: \"apparel\" } )","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">orders</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"abc"</span>, <span class="lg-highlight-attr">price</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"10"</span>) } } )</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">find</span>( <!-- -->{ <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"abc"</span>, <span class="lg-highlight-attr">price</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"5"</span>) } } )</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">find</span>( <!-- -->{ <span class="lg-highlight-attr">quantity</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-number">20</span> } } )</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">find</span>( <!-- -->{ <span class="lg-highlight-attr">quantity</span>: <!-- -->{ <span class="lg-highlight-attr">$gte</span>: <span class="lg-highlight-number">5</span> }<!-- -->, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"apparel"</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 preceding queries are completed using the slot-based query execution engine.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Return Information for All Entries in the Query Cache
<a class="headerlink leafygreen-ui-1kepx47" href="#return-information-for-all-entries-in-the-query-cache" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="return-information-for-all-entries-in-the-query-cache">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following aggregation pipeline uses
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
to
return information on the plan cache entries for the collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.aggregate( [\n { $planCacheStats: { } }\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">orders</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">$planCacheStats</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">
Output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n { // Plan Cache Entry 1\n version: '2',\n planCacheShapeHash: '478AD696',\n planCacheKey: '21AE23AD',\n isActive: true,\n works: Long(\"7\"),\n timeOfCreation: ISODate(\"2023-05-22T20:33:49.031Z\"),\n cachedPlan: {\n ...\n },\n indexFilterSet: false,\n isPinned: false,\n estimatedSizeBytes: Long(\"8194\"),\n host: 'mongodb1.example.net:27018'\n },\n { // Plan Cache Entry 2\n version: '2',\n planCacheShapeHash: '3D8AFDC6',\n planCacheKey: '1C2C4360',\n isActive: true,\n works: Long(\"6\"),\n timeOfCreation: ISODate(\"2023-05-22T20:33:50.584Z\"),\n cachedPlan: {\n ...\n },\n indexFilterSet: false,\n isPinned: false,\n estimatedSizeBytes: Long(\"11547\"),\n host: 'mongodb1.example.net:27018'\n },\n { // Plan Cache Entry 3\n version: '2',\n planCacheShapeHash: '27285F9B',\n planCacheKey: '20BB9404',\n isActive: true,\n works: Long(\"1\"),\n timeOfCreation: ISODate(\"2023-05-22T20:33:49.051Z\"),\n cachedPlan: {\n ...\n },\n indexFilterSet: false,\n isPinned: false,\n estimatedSizeBytes: Long(\"7406\"),\n host: 'mongodb1.example.net:27018'\n },\n { // Plan Cache Entry 4\n version: '2',\n planCacheShapeHash: '478AD696',\n planCacheKey: 'B1435201',\n isActive: true,\n works: Long(\"5\"),\n timeOfCreation: ISODate(\"2023-05-22T20:33:49.009Z\"),\n cachedPlan: {\n ...\n },\n indexFilterSet: false,\n isPinned: false,\n estimatedSizeBytes: Long(\"7415\"),\n host: 'mongodb1.example.net:27018'\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-comment">//</span><span class="lg-highlight-comment"> Plan Cache Entry </span><span class="lg-highlight-comment">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">version</span>: <span class="lg-highlight-string">'2'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">planCacheShapeHash</span>: <span class="lg-highlight-string">'478AD696'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">planCacheKey</span>: <span class="lg-highlight-string">'21AE23AD'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">isActive</span>: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">works</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"7"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timeOfCreation</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2023-05-22T20:33:49.031Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cachedPlan</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">indexFilterSet</span>: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">isPinned</span>: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">estimatedSizeBytes</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"8194"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">host</span>: <span class="lg-highlight-string">'mongodb1.example.net:27018'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Plan Cache Entry </span><span class="lg-highlight-comment">2</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">version</span>: <span class="lg-highlight-string">'2'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">planCacheShapeHash</span>: <span class="lg-highlight-string">'3D8AFDC6'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">planCacheKey</span>: <span class="lg-highlight-string">'1C2C4360'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">isActive</span>: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">works</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"6"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timeOfCreation</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2023-05-22T20:33:50.584Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cachedPlan</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">indexFilterSet</span>: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">isPinned</span>: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">estimatedSizeBytes</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"11547"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">host</span>: <span class="lg-highlight-string">'mongodb1.example.net:27018'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Plan Cache Entry </span><span class="lg-highlight-comment">3</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">version</span>: <span class="lg-highlight-string">'2'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">planCacheShapeHash</span>: <span class="lg-highlight-string">'27285F9B'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">planCacheKey</span>: <span class="lg-highlight-string">'20BB9404'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">isActive</span>: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">works</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"1"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timeOfCreation</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2023-05-22T20:33:49.051Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cachedPlan</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">indexFilterSet</span>: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">isPinned</span>: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">estimatedSizeBytes</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"7406"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">host</span>: <span class="lg-highlight-string">'mongodb1.example.net:27018'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Plan Cache Entry </span><span class="lg-highlight-comment">4</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">version</span>: <span class="lg-highlight-string">'2'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">planCacheShapeHash</span>: <span class="lg-highlight-string">'478AD696'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">planCacheKey</span>: <span class="lg-highlight-string">'B1435201'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">isActive</span>: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">works</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"5"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timeOfCreation</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2023-05-22T20:33:49.009Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cachedPlan</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">indexFilterSet</span>: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">isPinned</span>: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">estimatedSizeBytes</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"7415"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">host</span>: <span class="lg-highlight-string">'mongodb1.example.net:27018'</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 class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, the pre-existing
<code class="leafygreen-ui-1l06pbn">
queryHash
</code>
field is renamed
to
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
. If you're using an earlier MongoDB version,
you'll see
<code class="leafygreen-ui-1l06pbn">
queryHash
</code>
instead of
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
See also
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-plan-cache-key">
planCacheKey
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-planCacheStats-cache-entry-for-query-hash">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Find Cache Entry Details for a Query Hash
<a class="headerlink leafygreen-ui-1kepx47" href="#find-cache-entry-details-for-a-query-hash" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-cache-entry-details-for-a-query-hash">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To return plan cache information for a particular query hash, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-planCacheStats" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
</span>
</a>
stage can be followed by a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
on the
<code class="leafygreen-ui-1l06pbn">
planCacheKey
</code>
field.
</p>
<p class="leafygreen-ui-1kp3ins">
The following aggregation pipeline uses
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-pipeline-pipe.-planCacheStats" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
</span>
</a>
followed by a
<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 to return specific information
for a particular query hash:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.orders.aggregate( [\n { $planCacheStats: { } },\n { $match: { planCacheKey: \"B1435201\"} }\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">orders</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">$planCacheStats</span>: <!-- -->{ <!-- -->} }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$match</span>: <!-- -->{ <span class="lg-highlight-attr">planCacheKey</span>: <span class="lg-highlight-string">"B1435201"</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">
Output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n version: '2',\n planCacheShapeHash: '478AD696',\n planCacheKey: 'B1435201',\n isActive: true,\n works: Long(\"5\"),\n timeOfCreation: ISODate(\"2023-05-22T20:33:49.009Z\"),\n cachedPlan: {\n slots: '$$RESULT=s11 env: { s3 = 1684787629009 (NOW), s6 = Nothing, s5 = Nothing, s1 = TimeZoneDatabase(Asia/Kuwait...Etc/UCT) (timeZoneDB), s10 = {\"item\" : 1, \"price\" : 1}, s2 = Nothing (SEARCH_META) }',\n stages: '[2] nlj inner [] [s4, s7, s8, s9, s10] \\n' +\n ' left \\n' +\n ' [1] cfilter {(exists(s5) && exists(s6))} \\n' +\n ' [1] ixseek s5 s6 s9 s4 s7 s8 [] @\"358822b7-c129-47b7-ad7f-40017a51b03c\" @\"item_1_price_1\" true \\n' +\n ' right \\n' +\n ' [2] limit 1 \\n' +\n ' [2] seek s4 s11 s12 s7 s8 s9 s10 none none [] @\"358822b7-c129-47b7-ad7f-40017a51b03c\" true false \\n'\n },\n indexFilterSet: false,\n isPinned: false,\n estimatedSizeBytes: Long(\"7415\"),\n host: 'mongodb1.example.net:27018'\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">version</span>: <span class="lg-highlight-string">'2'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">planCacheShapeHash</span>: <span class="lg-highlight-string">'478AD696'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">planCacheKey</span>: <span class="lg-highlight-string">'B1435201'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">isActive</span>: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">works</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"5"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timeOfCreation</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2023-05-22T20:33:49.009Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cachedPlan</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">slots</span>: <span class="lg-highlight-string">'$$RESULT=s11 env: { s3 = 1684787629009 (NOW), s6 = Nothing, s5 = Nothing, s1 = TimeZoneDatabase(Asia/Kuwait...Etc/UCT) (timeZoneDB), s10 = {"item" : 1, "price" : 1}, s2 = Nothing (SEARCH_META) }'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">stages</span>: <span class="lg-highlight-string">'[2] nlj inner [] [s4, s7, s8, s9, s10] </span><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">'</span> <!-- -->+</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">' left </span><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">'</span> <!-- -->+</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">' [1] cfilter {(exists(s5) && exists(s6))} </span><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">'</span> <!-- -->+</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">' [1] ixseek s5 s6 s9 s4 s7 s8 [] @"358822b7-c129-47b7-ad7f-40017a51b03c" @"item_1_price_1" true </span><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">'</span> <!-- -->+</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">' right </span><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">'</span> <!-- -->+</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">' [2] limit 1 </span><span class="lg-highlight-string">\n</span><span class="lg-highlight-string">'</span> <!-- -->+</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">' [2] seek s4 s11 s12 s7 s8 s9 s10 none none [] @"358822b7-c129-47b7-ad7f-40017a51b03c" true false </span><span class="lg-highlight-string">\n</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-attr">indexFilterSet</span>: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">isPinned</span>: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">estimatedSizeBytes</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>(<span class="lg-highlight-string">"7415"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">host</span>: <span class="lg-highlight-string">'mongodb1.example.net:27018'</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 class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0, the pre-existing
<code class="leafygreen-ui-1l06pbn">
queryHash
</code>
field is renamed
to
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
. If you're using an earlier MongoDB version,
you'll see
<code class="leafygreen-ui-1l06pbn">
queryHash
</code>
instead of
<code class="leafygreen-ui-1l06pbn">
planCacheShapeHash
</code>
.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
See also
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-plan-cache-key">
planCacheKey
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-query-hash">
planCacheShapeHash
<!-- -->
.
</a>
</p>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/db.collection.validate/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.collection.validate()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.collection.validate--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.validate--">
</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="#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>
<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.IMongoDatabase.RunCommand.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/usage-examples/command/" 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/Admin.html#validateCollection" 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://pymongo.readthedocs.io/en/4.10.1/api/pymongo/database.html#pymongo.database.Database.validate_collection" 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_client_read_command_with_opts.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_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://mongocxx.org/api/mongocxx-3.11.0/classmongocxx_1_1v__noabi_1_1database.html#a1e11c0874c945f8bb9ca39f1a30c9271" 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/" 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/write/run-command/" 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://www.mongodb.com/docs/drivers/kotlin/coroutine/current/usage-examples/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">
<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/-mongo-database/run-command.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">
<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/run-command/" 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.mongodb.com/docs/mongoid/master/api/Mongoid/Contextual/Command.html" 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/" 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/command/" 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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-validate-standalone-inconsistencies">
</span>
<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>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
6.2
</em>
.
</p>
</div>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
db.collection.validate(<documents>)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.collection.validate" 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.validate">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Validates a collection. The method scans a collection data and
indexes for correctness and returns the result. For details of the
output, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/validate/#std-label-validate-output">
Validate Output
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in version 5.0, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.validate" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.validate()
</code>
</span>
</a>
method can also fix inconsistencies in the collection.
</p>
<p class="leafygreen-ui-1kp3ins">
Index inconsistencies include:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
An index is
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-multikey/#std-label-index-type-multikey">
multikey
</a>
but there are
no multikey fields.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
An index has
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-multikey/#std-label-compound_multikey_indexes">
multikeyPaths
</a>
covering
fields that are not multikey.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
An index does not have
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-multikey/#std-label-compound_multikey_indexes">
multikeyPaths
</a>
but
there are multikey documents (for indexes built before 3.4).
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
If any inconsistencies are detected by the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.validate" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.validate()
</code>
</span>
</a>
command, a warning is returned
and the repair flag on the index is set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.validate" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.validate()
</code>
</span>
</a>
also validates any documents that
violate the collection's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/schema-validation/#std-label-schema-validation-document">
schema validation rules
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.validate" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.validate()
</code>
</span>
</a>
method is a wrapper around
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/validate/#mongodb-dbcommand-dbcmd.validate">
<code class="leafygreen-ui-1l06pbn">
validate
</code>
</a>
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 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 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
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.validate" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.validate()
</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.validate( {\n full: <boolean>, // Optional\n repair: <boolean>, // Optional, added in MongoDB 5.0\n checkBSONConformance: <boolean> // Optional, added in MongoDB 6.2\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">collection</span>.<span class="lg-highlight-title lg-highlight-function">validate</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">full</span>: <<!-- -->boolean<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-comment">// Optional</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">repair</span>: <<!-- -->boolean<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-comment">// Optional, added in MongoDB 5.0</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">checkBSONConformance</span>: <<!-- -->boolean<!-- -->> <span class="lg-highlight-comment">// Optional, added in MongoDB 6.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>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" 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>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.validate" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.validate()
</code>
</span>
</a>
method can take the
following optional document parameter with the fields:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:15%"/>
<col style="width:15%"/>
<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>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-method-validate-full" target="_self">
<span>
full
</span>
</a>
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-method-validate-full">
</span>
<p class="leafygreen-ui-1kp3ins">
<em>
Optional
</em>
. A flag that determines whether the command performs
a slower but more thorough check or a faster but less thorough
check.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
true
</code>
, performs a more thorough check with the following
exception:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Full validation on the
<code class="leafygreen-ui-1l06pbn">
oplog
</code>
for WiredTiger skips the more
thorough check.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
false
</code>
, omits some checks for a faster but less
thorough check.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The default is
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
For the WiredTiger storage engine, only the
<code class="leafygreen-ui-1l06pbn">
full
</code>
validation process will
force a checkpoint and flush all in-memory data to disk before verifying the
on-disk data.
</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-method-validate-repair" target="_self">
<span>
repair
</span>
</a>
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-method-validate-repair">
</span>
<p class="leafygreen-ui-1kp3ins">
<em>
Optional
</em>
. A flag that determines whether the command performs a
repair.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
true
</code>
, a repair is performed.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
false
</code>
, no repair is performed.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The default is
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
A repair can only be run on a standalone node.
</p>
<p class="leafygreen-ui-1kp3ins">
The repair fixes these issues:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If missing index entries are found, the missing keys are inserted into
the index.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If extra index entries are found, the extra keys are removed from the
index.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-multikey/#std-label-index-type-multikey">
multikey
</a>
documents are found for an
index that is not a multikey index, the index is changed to a multikey
index.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-multikey/#std-label-index-type-multikey">
multikey
</a>
documents are found that are
not specified by an index's multikey paths, the index's multikey paths
are updated.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If corrupt documents with invalid BSON data are found, the documents
are removed.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For more information, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--repair">
<code class="leafygreen-ui-1l06pbn">
--repair
</code>
</a>
option
for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</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="#std-label-method-validate-checkBSONConformance" target="_self">
<span>
checkBSONConformance
</span>
</a>
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-method-validate-checkBSONConformance">
</span>
<p class="leafygreen-ui-1kp3ins">
<em>
Optional
</em>
. If
<code class="leafygreen-ui-1l06pbn">
true
</code>
, the collection is checked to ensure the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/document/#std-label-bson-document-format">
BSON documents
</a>
conform to the BSON
specifications. The checks increase the time to complete the validation
operation. Any issues are returned as a warning.
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
checkBSONConformance
</code>
:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Default is
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Is enabled when
<code class="leafygreen-ui-1l06pbn">
full
</code>
is set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Cannot be used with:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
repair
</code>
set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
metadata
</code>
set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
</li>
</ul>
</li>
</ul>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
6.2
</em>
.
</p>
</div>
</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>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Performance
<a class="headerlink leafygreen-ui-1kepx47" href="#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="performance">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.validate" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.validate()
</code>
</span>
</a>
method is potentially resource
intensive and may impact the performance of your MongoDB instance,
particularly on larger data sets.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.validate" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.validate()
</code>
</span>
</a>
method obtains an exclusive lock
on the collection. This will block all reads and writes on the
collection until the operation finishes. When run on a secondary, the
operation can block all other operations on that secondary until it
finishes.
</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">
Validation has exclusive lock requirements that affect performance
on primaries and on secondaries that are servicing reads. Consider
only running
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.validate" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.validate()
</code>
</span>
</a>
on nodes that are
not servicing reads or writes.
</p>
<p class="leafygreen-ui-1kp3ins">
To minimize impact on the primary, the majority of the data-bearing
(non-arbiter), voting members in the cluster must be available and
must not have significant replication lag.
</p>
<p class="leafygreen-ui-1kp3ins">
To minimize the impact of the validation operation on client
applications, run
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.validate" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.validate()
</code>
</span>
</a>
on a secondary
node that is not servicing read requests. You can convert the
current primary node to a secondary node, by running the
<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>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
To completely isolate the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.validate" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.validate()
</code>
</span>
</a>
operation from client traffic, choose one of the following options:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Isolate a replica set member by following the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/perform-maintence-on-replica-set-members/#std-label-perform-maint-on-replica-set">
rolling
maintenance procedure
</a>
to
temporarily remove it from the cluster.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/configure-a-hidden-replica-set-member/#std-label-configure-hidden-replica-set-member">
Convert a secondary node
</a>
to a replica set
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-hidden-member/#std-label-replica-set-hidden-members">
hidden member
</a>
and perform the
validation on the hidden node.
</p>
</li>
</ul>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Data Throughput Metrics
<a class="headerlink leafygreen-ui-1kepx47" href="#data-throughput-metrics" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="data-throughput-metrics">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/currentOp/#mongodb-pipeline-pipe.-currentOp">
<code class="leafygreen-ui-1l06pbn">
$currentOp
</code>
</a>
and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/currentOp/#mongodb-dbcommand-dbcmd.currentOp">
<code class="leafygreen-ui-1l06pbn">
currentOp
</code>
</a>
command
include
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/currentOp/#mongodb-data--currentOp.dataThroughputAverage">
<code class="leafygreen-ui-1l06pbn">
dataThroughputAverage
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/currentOp/#mongodb-data--currentOp.dataThroughputLastSecond">
<code class="leafygreen-ui-1l06pbn">
dataThroughputLastSecond
</code>
</a>
information for
validate operations in progress.
</p>
<p class="leafygreen-ui-1kp3ins">
The log messages for validate operations include
<code class="leafygreen-ui-1l06pbn">
dataThroughputAverage
</code>
and
<code class="leafygreen-ui-1l06pbn">
dataThroughputLastSecond
</code>
information.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Collection Validation Improvements
<a class="headerlink leafygreen-ui-1kepx47" href="#collection-validation-improvements" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="collection-validation-improvements">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.2, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/validate/#mongodb-dbcommand-dbcmd.validate">
<code class="leafygreen-ui-1l06pbn">
validate
</code>
</a>
command and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.validate" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.validate()
</code>
</span>
</a>
method:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Check collections to ensure the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/document/#std-label-bson-document-format">
BSON documents
</a>
conform to the BSON
specifications.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Check
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries-collections/#std-label-manual-timeseries-collection">
time series collections
</a>
for internal data inconsistencies.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Have a new option
<code class="leafygreen-ui-1l06pbn">
checkBSONConformance
</code>
that enables comprehensive
BSON checks.
</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>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To validate a collection
<code class="leafygreen-ui-1l06pbn">
myCollection
</code>
using the default validation
setting (specifically,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-method-validate-full" target="_self">
<span>
full: false
</span>
</a>
):
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.myCollection.validate()\n\ndb.myCollection.validate({ })\n\ndb.myCollection.validate( { full: false } )","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">myCollection</span>.<span class="lg-highlight-title lg-highlight-function">validate</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-property">myCollection</span>.<span class="lg-highlight-title lg-highlight-function">validate</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-property">myCollection</span>.<span class="lg-highlight-title lg-highlight-function">validate</span>( <!-- -->{ <span class="lg-highlight-attr">full</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>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To perform a full validation of collection
<code class="leafygreen-ui-1l06pbn">
myCollection
</code>
, specify
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-method-validate-full" target="_self">
<span>
full: true
<!-- -->
:
</span>
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.myCollection.validate( { full: 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">db.<span class="lg-highlight-property">myCollection</span>.<span class="lg-highlight-title lg-highlight-function">validate</span>( <!-- -->{ <span class="lg-highlight-attr">full</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">
To repair collection
<code class="leafygreen-ui-1l06pbn">
myCollection
</code>
, specify
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-method-validate-repair" target="_self">
<span>
repair: true
<!-- -->
:
</span>
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.myCollection.validate( { repair: 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">db.<span class="lg-highlight-property">myCollection</span>.<span class="lg-highlight-title lg-highlight-function">validate</span>( <!-- -->{ <span class="lg-highlight-attr">repair</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">
To perform additional BSON conformance checks in
<code class="leafygreen-ui-1l06pbn">
myCollection
</code>
,
specify
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-method-validate-checkBSONConformance" target="_self">
<span>
checkBSONConformance: true
<!-- -->
:
</span>
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.myCollection.validate( { checkBSONConformance: 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">myCollection</span>.<span class="lg-highlight-title lg-highlight-function">validate</span>( <!-- -->{ <span class="lg-highlight-attr">checkBSONConformance</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>
</ul>
<p class="leafygreen-ui-1kp3ins">
For details of the output, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/validate/#std-label-validate-output">
Validate Output
<!-- -->
.
</a>
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/release-notes/7.0-compatibility/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Compatibility Changes in MongoDB 7.0
<a class="headerlink leafygreen-ui-1kepx47" href="#compatibility-changes-in-mongodb-7.0" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-changes-in-mongodb-7.0">
</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="#deprecations" target="_self">
<span>
Deprecations
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#free-monitoring-decommissioned" target="_self">
<span>
Free Monitoring Decommissioned
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#queryable-encryption-general-availability" target="_self">
<span>
Queryable Encryption General Availability
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#backward-incompatible-features" target="_self">
<span>
Backward-Incompatible Features
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#intra-node-authentication" target="_self">
<span>
Intra-Node Authentication
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#time-series" target="_self">
<span>
Time-Series
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#cluster-configuration" target="_self">
<span>
Cluster Configuration
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#encrypted-multi-map--emm-" target="_self">
<span>
Encrypted Multi-Map (EMM)
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#wildcard-indexes" target="_self">
<span>
Wildcard Indexes
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
This page describes changes introduced in MongoDB 7.0 that can affect
compatibility with older versions of MongoDB.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB 7.0 is a
<!-- -->
Major
<!-- -->
Release, which means that it is supported for
both MongoDB Atlas and on-premises deployments. MongoDB 7.0 includes
changes introduced in MongoDB Rapid Releases 6.1, 6.2, and 6.3. This
page describes compatibility changes introduced in those Rapid Releases
and MongoDB 7.0.
</p>
<p class="leafygreen-ui-1kp3ins">
To learn more about the differences between
<!-- -->
Major
<!-- -->
and Rapid Releases,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/versioning/#std-label-release-version-numbers">
MongoDB Versioning
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
For detail on downgrading from MongoDB 7.0, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/release-notes/7.0-downgrade/#std-label-7.0-downgrade">
Downgrade
<!-- -->
7.0
<!-- -->
to
<!-- -->
6.0
<!-- -->
.
</a>
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Deprecations
<a class="headerlink leafygreen-ui-1kepx47" href="#deprecations" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="deprecations">
</div>
</a>
</h2>
<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>
Deprecated
</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">
storageDetails
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<em>
Deprecated in version 7.0.
</em>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Free Monitoring Decommissioned
<a class="headerlink leafygreen-ui-1kepx47" href="#free-monitoring-decommissioned" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="free-monitoring-decommissioned">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 7.0,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/free-monitoring/#std-label-free-monitoring-mongodb">
free monitoring
</a>
is decommissioned. The following aren't
available:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
cloud.monitoring.free
</code>
configuration option
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
--enableFreeMonitoring
</code>
server parameter
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
db.enableFreeMonitoring()
</code>
command
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
db.disableFreeMonitoring()
</code>
command
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
setFreeMonitoring
</code>
command
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Queryable Encryption General Availability
<a class="headerlink leafygreen-ui-1kepx47" href="#queryable-encryption-general-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="queryable-encryption-general-availability">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 7.0, Queryable Encryption with equality queries is generally
available (GA). Improvements in the GA make it
incompatible with the Queryable Encryption Public Preview, which should not be used now that
the feature is GA.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Server and Drivers Compatibility Between Public Preview and GA
<a class="headerlink leafygreen-ui-1kepx47" href="#server-and-drivers-compatibility-between-public-preview-and-ga" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="server-and-drivers-compatibility-between-public-preview-and-ga">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To use Queryable Encryption with equality queries, MongoDB server must be version 7.0 or
later and drivers must be 7.0 compatible. If you are still using the
Queryable Encryption Public Preview included with MongoDB 6.x, the server must remain on
version 6.x and the drivers must be 6.x compatible. You cannot use
MongoDB 6.x compatible drivers with a 7.0 server, or 7.0 compatible
drivers with a 6.x server. Attempting to do so results in an error.
</p>
<p class="leafygreen-ui-1kp3ins">
To make upgrading easier, MongoDB 7.0 drivers can decrypt data
created with MongoDB 6.x drivers. For upgrade options, see the following sections.
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Start Fresh (Preferred)
<a class="headerlink leafygreen-ui-1kepx47" href="#start-fresh--preferred-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-fresh--preferred-">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
If possible, create new collections rather than migrating those produced
with the Queryable Encryption Public Preview in MongoDB 6.x:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Upgrade MongoDB server and drivers to 7.0.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Configure a new encrypted collection with a different name from the
previous collection.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Insert new data, or an unencrypted version of the existing data if
you have a local copy.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Drop the previous version 6.x collection.
</p>
</li>
</ol>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Migration
<a class="headerlink leafygreen-ui-1kepx47" href="#migration" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="migration">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
If you cannot use new data or do not have an unencrypted version of the
existing data:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Upgrade MongoDB server and drivers to 7.0
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Using a 7.0 compatible driver, query the encrypted collection to decrypt it.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Save the output locally.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Configure a new encrypted collection and ingest the data.
</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="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongoexport/#mongodb-binary-bin.mongoexport" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongoexport
</code>
</span>
</a>
and
<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>
operations do not decrypt the collection. You must query the
collection from a driver to output the decrypted data.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
MongoDB 7.0 compatible drivers can't query encrypted fields on
data encrypted with MongoDB 6.x compatible drivers. To decrypt
data, either query an unencrypted field or query the entire collection.
</p>
</li>
</ul>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-7.0-downgrade-incompatible">
</span>
</section>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Backward-Incompatible Features
<a class="headerlink leafygreen-ui-1kepx47" href="#backward-incompatible-features" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="backward-incompatible-features">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The following 7.0 features are not compatible with earlier versions of
MongoDB. To downgrade from MongoDB 7.0 to an earlier version, you must
remove data that uses any of the following features:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Collections with
<code class="leafygreen-ui-1l06pbn">
encryptedFields
</code>
with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/ranged-sharding/#std-label-sharding-ranged">
range indexes
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Compound wildcard indexes require fCV 7.0 or greater. A pre-7.0
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
does not start if you are using one or more compound
wildcard indexes.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Config servers that have collections with
<code class="leafygreen-ui-1l06pbn">
changeStreamPreAndPostImages
</code>
enabled
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Secondary
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-ttl/#std-label-index-feature-ttl">
TTL indexes
</a>
with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-partial/#std-label-index-type-partial">
partial
filters
</a>
on
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries-collections/#std-label-manual-timeseries-collection">
time series collections
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Time series collections with custom bucketing parameters
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Intra-Node Authentication
<a class="headerlink leafygreen-ui-1kepx47" href="#intra-node-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="intra-node-authentication">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 7.0, you can configure servers to authenticate
X.509 certificates as cluster members identified by attributes or extension
values. You can also override this configuration to accept alternate
certificates during a rolling upgrade.
</p>
<p class="leafygreen-ui-1kp3ins">
To downgrade to MongoDB 6.0, perform the certificate rotation procedure to unset
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.tls.clusterAuthX509">
<code class="leafygreen-ui-1l06pbn">
net.tls.clusterAuthX509
</code>
</a>
setting and rotate the cluster
membership certificates to ones with matching DC, O, and OU attributes.
</p>
<p class="leafygreen-ui-1kp3ins">
Once this is complete, you can downgrade the cluster.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Time-Series
<a class="headerlink leafygreen-ui-1kepx47" href="#time-series" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="time-series">
</div>
</a>
</h2>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Remove partial TTL indexes from time series collections.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Remove or modify collections using new index parameters. In some cases, you
may use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collMod/#mongodb-dbcommand-dbcmd.collMod">
<code class="leafygreen-ui-1l06pbn">
collMod
</code>
</a>
to switch to legacy granularity settings, if not
you need to drop the collection before downgrading.
</p>
</li>
</ul>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Cluster Configuration
<a class="headerlink leafygreen-ui-1kepx47" href="#cluster-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="cluster-configuration">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Active
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setClusterParameter/#mongodb-dbcommand-dbcmd.setClusterParameter">
<code class="leafygreen-ui-1l06pbn">
setClusterParameter
</code>
</a>
parameter operations prevent
feature Compatibility Version (fCV) from successful completion.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Encrypted Multi-Map (EMM)
<a class="headerlink leafygreen-ui-1kepx47" href="#encrypted-multi-map--emm-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-multi-map--emm-">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Remove collections that use the
<code class="leafygreen-ui-1l06pbn">
encryptedFields
</code>
collection option
before downgrading.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Wildcard Indexes
<a class="headerlink leafygreen-ui-1kepx47" href="#wildcard-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="wildcard-indexes">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Compound wildcard indexes require fCV 7.0 or greater. A pre-7.0
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
does not start if you are using one or more compound
wildcard indexes.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
OIDC Access Token Authentication Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#oidc-access-token-authentication-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="oidc-access-token-authentication-behavior">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 7.0, only one
<code class="leafygreen-ui-1l06pbn">
audience
</code>
oidcIdentityProviders
field can be specified for OIDC access tokens.
<code class="leafygreen-ui-1l06pbn">
audience
</code>
fields
with empty arrays or arrays of multiple strings are invalid.
</p>
<p class="leafygreen-ui-1kp3ins">
For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#std-label-oidcidentityproviders-fields">
oidcIdentityProviders Fields
<!-- -->
.
</a>
</p>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/administration/free-monitoring/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Free Monitoring
<a class="headerlink leafygreen-ui-1kepx47" href="#free-monitoring" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="free-monitoring">
</div>
</a>
</h1>
<div class="leafygreen-ui-1sjx7ru" role="note">
<h2 class="leafygreen-ui-29m78u">
Warning
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Free monitoring is decommissioned.
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Free monitoring was decommissioned in August 2023.
</p>
<p class="leafygreen-ui-1kp3ins">
Beginning in April 2023, you cannot enable free monitoring on MongoDB
Community instances.
</p>
<p class="leafygreen-ui-1kp3ins">
The following list shows monitoring options for your deployment:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<strong>
Deploy a MongoDB Atlas dedicated cluster
</strong>
.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/migration-live-atlas-managed/#std-label-live-migration-pre60" target="_self">
<span>
Migrate your
data
</span>
</a>
to a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/getting-started/#std-label-atlas-getting-started" target="_self">
<span>
MongoDB Atlas
</span>
</a>
dedicated cluster sized M10 or greater,
which includes several advanced monitoring and alerting features:
</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/tutorial/profile-database/#std-label-query-profiler" target="_self">
<span>
Query Profiler
</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/docs/atlas/performance-advisor/#std-label-performance-advisor" target="_self">
<span>
Performance Advisor
</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/docs/atlas/real-time-performance-panel/#std-label-real-time-metrics-status-tab" target="_self">
<span>
Real-Time Performance Panel
</span>
</a>
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<strong>
Deploy a MongoDB Atlas free cluster
</strong>
. A free Atlas cluster includes
basic monitoring and alerting capabilities. After you
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas/tutorial/deploy-free-tier-cluster/" target="_self">
<span>
create a free cluster
</span>
</a>
, use
<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>
to manually create a backup of your database
and import your data from your MongoDB Community instance.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<strong>
Use MongoDB Cloud Manager
</strong>
. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://docs.cloudmanager.mongodb.com/" target="_self">
<span>
MongoDB Cloud Manager
</span>
</a>
free tier includes basic
monitoring capabilities.
</p>
</li>
</ul>
</div>
</div>
</section>
|
https://www.mongodb.com/docs/manual/reference/security/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Security Reference for Self-Managed Deployments
<a class="headerlink leafygreen-ui-1kepx47" href="#security-reference-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="security-reference-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="#security-methods-in-mongosh" target="_self">
<span>
Security Methods in
<code class="css-1kekw28 e1wawog0">
mongosh
</code>
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#security-reference-documentation" target="_self">
<span>
Security Reference Documentation
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
The following lists the security related methods available 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>
as well as additional
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-security-reference-materials" target="_self">
<span>
security reference material
<!-- -->
.
</span>
</a>
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Security Methods in
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#security-methods-in-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="security-methods-in-mongosh">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
User Management and Authentication Methods
<a class="headerlink leafygreen-ui-1kepx47" href="#user-management-and-authentication-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="user-management-and-authentication-methods">
</div>
</a>
</h3>
<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="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<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>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Authenticates a user to a database.
</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/method/db.changeUserPassword/#mongodb-method-db.changeUserPassword">
<code class="leafygreen-ui-1l06pbn">
db.changeUserPassword()
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Changes an existing user's password.
</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/method/db.createUser/#mongodb-method-db.createUser">
<code class="leafygreen-ui-1l06pbn">
db.createUser()
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Creates a new user.
</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/method/db.dropUser/#mongodb-method-db.dropUser">
<code class="leafygreen-ui-1l06pbn">
db.dropUser()
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Removes a single user.
</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/method/db.dropAllUsers/#mongodb-method-db.dropAllUsers">
<code class="leafygreen-ui-1l06pbn">
db.dropAllUsers()
</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>
</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/method/db.getUser/#mongodb-method-db.getUser">
<code class="leafygreen-ui-1l06pbn">
db.getUser()
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns information about the specified user.
</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/method/db.getUsers/#mongodb-method-db.getUsers">
<code class="leafygreen-ui-1l06pbn">
db.getUsers()
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns information about all users associated with a database.
</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/method/db.grantRolesToUser/#mongodb-method-db.grantRolesToUser">
<code class="leafygreen-ui-1l06pbn">
db.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>
</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/method/db.removeUser/#mongodb-method-db.removeUser">
<code class="leafygreen-ui-1l06pbn">
db.removeUser()
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Deprecated. Removes a user from a database.
</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/method/db.revokeRolesFromUser/#mongodb-method-db.revokeRolesFromUser">
<code class="leafygreen-ui-1l06pbn">
db.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>
</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/method/db.updateUser/#mongodb-method-db.updateUser">
<code class="leafygreen-ui-1l06pbn">
db.updateUser()
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Updates user data.
</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/method/passwordPrompt/#mongodb-method-passwordPrompt">
<code class="leafygreen-ui-1l06pbn">
passwordPrompt()
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Prompts for the password as an alternative to specifying passwords
directly in various
<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>
user
authentication/management methods.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Role Management Methods
<a class="headerlink leafygreen-ui-1kepx47" href="#role-management-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="role-management-methods">
</div>
</a>
</h3>
<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="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.createRole/#mongodb-method-db.createRole">
<code class="leafygreen-ui-1l06pbn">
db.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>
</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/method/db.dropRole/#mongodb-method-db.dropRole">
<code class="leafygreen-ui-1l06pbn">
db.dropRole()
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Deletes a user-defined role.
</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/method/db.dropAllRoles/#mongodb-method-db.dropAllRoles">
<code class="leafygreen-ui-1l06pbn">
db.dropAllRoles()
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Deletes all user-defined roles associated with a database.
</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/method/db.getRole/#mongodb-method-db.getRole">
<code class="leafygreen-ui-1l06pbn">
db.getRole()
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns information for the specified role.
</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/method/db.getRoles/#mongodb-method-db.getRoles">
<code class="leafygreen-ui-1l06pbn">
db.getRoles()
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns information for all the user-defined roles in a database.
</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/method/db.grantPrivilegesToRole/#mongodb-method-db.grantPrivilegesToRole">
<code class="leafygreen-ui-1l06pbn">
db.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>
</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/method/db.revokePrivilegesFromRole/#mongodb-method-db.revokePrivilegesFromRole">
<code class="leafygreen-ui-1l06pbn">
db.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>
</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/method/db.grantRolesToRole/#mongodb-method-db.grantRolesToRole">
<code class="leafygreen-ui-1l06pbn">
db.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>
</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/method/db.revokeRolesFromRole/#mongodb-method-db.revokeRolesFromRole">
<code class="leafygreen-ui-1l06pbn">
db.revokeRolesFromRole()
</code>
</a>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Removes inherited roles from a role.
</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/method/db.updateRole/#mongodb-method-db.updateRole">
<code class="leafygreen-ui-1l06pbn">
db.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>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-security-reference-materials">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Security Reference Documentation
<a class="headerlink leafygreen-ui-1kepx47" href="#security-reference-documentation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-reference-documentation">
</div>
</a>
</h2>
<dl>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-roles-collection/">
<code class="leafygreen-ui-1l06pbn">
system.roles
</code>
Collection in Self-Managed Deployments
</a>
</dt>
<dd class="css-1ufxuu7">
Describes the content of the collection that stores user-defined
roles.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-users-collection/">
<code class="leafygreen-ui-1l06pbn">
system.users
</code>
Collection in Self-Managed Deployments
</a>
</dt>
<dd class="css-1ufxuu7">
Describes the content of the collection that stores users'
credentials and role assignments.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/resource-document/">
Resource Document on Self-Managed Deployments
</a>
</dt>
<dd class="css-1ufxuu7">
Describes the resource document for roles.
</dd>
<dt>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/">
Privilege Actions for Self-Managed Deployments
</a>
</dt>
<dd class="css-1ufxuu7">
List of the actions available for privileges.
</dd>
</dl>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/query/nor/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$nor
<a class="headerlink leafygreen-ui-1kepx47" href="#-nor" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-nor">
</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="#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">
$nor
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-query-op.-nor" 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.-nor">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-nor" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$nor
</code>
</span>
</a>
performs a logical
<code class="leafygreen-ui-1l06pbn">
NOR
</code>
operation on an array of one
or more query predicates and selects the documents that
<strong>
fail
</strong>
all
the query predicates in the array. The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-nor" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$nor
</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":"{ $nor: [ { <expression1> }, { <expression2> }, ... { <expressionn> } ] }","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">$nor</span>: <!-- -->[ <!-- -->{ <<!-- -->expression1<!-- -->> <!-- -->}<!-- -->, <!-- -->{ <<!-- -->expression2<!-- -->> <!-- -->}<!-- -->, <!-- -->...<!-- --> <!-- -->{ <<!-- -->expressionN<!-- -->> <!-- -->} ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
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">
$nor
</code>
Query with Two Expressions
<a class="headerlink leafygreen-ui-1kepx47" href="#-nor-query-with-two-expressions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-nor-query-with-two-expressions">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Consider the following query which uses only the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-nor" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$nor
</code>
</span>
</a>
operator:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.find( { $nor: [ { price: 1.99 }, { sale: 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">inventory</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">$nor</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">1.99</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">sale</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">
This query will return all documents that:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
contain the
<code class="leafygreen-ui-1l06pbn">
price
</code>
field whose value is
<em>
not
</em>
equal to
<code class="leafygreen-ui-1l06pbn">
1.99
</code>
and contain the
<code class="leafygreen-ui-1l06pbn">
sale
</code>
field whose value
<em>
is not
</em>
equal to
<code class="leafygreen-ui-1l06pbn">
true
</code>
<strong>
or
</strong>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
contain the
<code class="leafygreen-ui-1l06pbn">
price
</code>
field whose value is
<em>
not
</em>
equal to
<code class="leafygreen-ui-1l06pbn">
1.99
</code>
<em>
but
</em>
do
<em>
not
</em>
contain the
<code class="leafygreen-ui-1l06pbn">
sale
</code>
field
<strong>
or
</strong>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
do
<em>
not
</em>
contain the
<code class="leafygreen-ui-1l06pbn">
price
</code>
field
<em>
but
</em>
contain the
<code class="leafygreen-ui-1l06pbn">
sale
</code>
field whose value
<em>
is not
</em>
equal to
<code class="leafygreen-ui-1l06pbn">
true
</code>
<strong>
or
</strong>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
do
<em>
not
</em>
contain the
<code class="leafygreen-ui-1l06pbn">
price
</code>
field
<em>
and
</em>
do
<em>
not
</em>
contain the
<code class="leafygreen-ui-1l06pbn">
sale
</code>
field
</p>
</li>
</ul>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
$nor
</code>
and Additional Comparisons
<a class="headerlink leafygreen-ui-1kepx47" href="#-nor-and-additional-comparisons" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-nor-and-additional-comparisons">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Consider the following query:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.find( { $nor: [ { price: 1.99 }, { qty: { $lt: 20 } }, { sale: 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">inventory</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">$nor</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">1.99</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">qty</span>: <!-- -->{ <span class="lg-highlight-attr">$lt</span>: <span class="lg-highlight-number">20</span> } }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">sale</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">
This query will select all documents in the
<code class="leafygreen-ui-1l06pbn">
inventory
</code>
collection
where:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the
<code class="leafygreen-ui-1l06pbn">
price
</code>
field value does
<em>
not
</em>
equal
<code class="leafygreen-ui-1l06pbn">
1.99
</code>
<strong>
and
</strong>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the
<code class="leafygreen-ui-1l06pbn">
qty
</code>
field value is
<em>
not
</em>
less than
<code class="leafygreen-ui-1l06pbn">
20
</code>
<strong>
and
</strong>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the
<code class="leafygreen-ui-1l06pbn">
sale
</code>
field value is
<em>
not
</em>
equal to
<code class="leafygreen-ui-1l06pbn">
true
</code>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
including those documents that do not contain these field(s).
</p>
<p class="leafygreen-ui-1kp3ins">
The exception in returning documents that do not contain the field
in the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-nor" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$nor
</code>
</span>
</a>
expression is when the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-nor" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$nor
</code>
</span>
</a>
operator is
used with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/exists/#mongodb-query-op.-exists">
<code class="leafygreen-ui-1l06pbn">
$exists
</code>
</a>
operator.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
$nor
</code>
and
<code class="leafygreen-ui-1l06pbn">
$exists
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#-nor-and--exists" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-nor-and--exists">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Compare that with the following query which uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-nor" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$nor
</code>
</span>
</a>
operator with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/exists/#mongodb-query-op.-exists">
<code class="leafygreen-ui-1l06pbn">
$exists
</code>
</a>
operator:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.find( { $nor: [ { price: 1.99 }, { price: { $exists: false } },\n { sale: true }, { sale: { $exists: false } } ] } )","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>( <!-- -->{ <span class="lg-highlight-attr">$nor</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">1.99</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">price</span>: <!-- -->{ <span class="lg-highlight-attr">$exists</span>: <span class="lg-highlight-literal">false</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">sale</span>: <span class="lg-highlight-literal">true</span> }<!-- -->, <!-- -->{ <span class="lg-highlight-attr">sale</span>: <!-- -->{ <span class="lg-highlight-attr">$exists</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>
<p class="leafygreen-ui-1kp3ins">
This query will return all documents that:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
contain the
<code class="leafygreen-ui-1l06pbn">
price
</code>
field whose value is
<em>
not
</em>
equal to
<code class="leafygreen-ui-1l06pbn">
1.99
</code>
and contain the
<code class="leafygreen-ui-1l06pbn">
sale
</code>
field whose value
<em>
is not
</em>
equal to
<code class="leafygreen-ui-1l06pbn">
true
</code>
</p>
</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.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/operator/query/or/#mongodb-query-op.-or">
<code class="leafygreen-ui-1l06pbn">
$or
</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>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/exists/#mongodb-query-op.-exists">
<code class="leafygreen-ui-1l06pbn">
$exists
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/db.watch/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.watch()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.watch--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.watch--">
</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="#availability" target="_self">
<span>
Availability
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#deployment" target="_self">
<span>
Deployment
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#storage-engine" target="_self">
<span>
Storage Engine
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#read-concern-majority-support" target="_self">
<span>
Read Concern
<code class="css-1kekw28 e1wawog0">
majority
</code>
Support
</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-x9koo9" href="#resumability" target="_self">
<span>
Resumability
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#full-document-lookup-of-update-operations" target="_self">
<span>
Full Document Lookup of Update Operations
</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="#cursor-iteration" target="_self">
<span>
Cursor Iteration
</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.watch( pipeline, options )
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.watch" 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.watch">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
<em>
For replica sets and sharded clusters only
</em>
</p>
<p class="leafygreen-ui-1kp3ins">
Opens a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/changeStreams/#std-label-changeStreams">
change stream cursor
</a>
for a database
to report on all its non-
<code class="leafygreen-ui-1l06pbn">
system
</code>
collections.
</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">
pipeline
</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. An
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
Aggregation Pipeline
</a>
consisting
of one or more of 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>
</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">
Specify a pipeline to filter/modify the change events output.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 4.2, change streams will throw an exception if
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/changeStreams/#std-label-change-stream-modify-output">
change stream aggregation pipeline
</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>
</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. Additional options that modify the behavior of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.watch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.watch()
</code>
.
</span>
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
options
</code>
document can contain the following fields and values:
</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">
resumeAfter
</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. Specifies a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/changeStreams/#std-label-change-stream-resume">
resume token
</a>
as the
logical starting point for the change stream. Cannot be used to resume
the change stream after an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/invalidate/#mongodb-data-invalidate">
<code class="leafygreen-ui-1l06pbn">
invalidate
</code>
</a>
event.
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
is mutually exclusive with
<code class="leafygreen-ui-1l06pbn">
startAfter
</code>
and
<code class="leafygreen-ui-1l06pbn">
startAtOperationTime
</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">
startAfter
</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. Specifies a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/changeStreams/#std-label-change-stream-resume">
resume token
</a>
as the
logical starting point for the change stream. Unlike
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
,
<code class="leafygreen-ui-1l06pbn">
startAfter
</code>
can resume notifications after an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/invalidate/#mongodb-data-invalidate">
<code class="leafygreen-ui-1l06pbn">
invalidate
</code>
</a>
event by creating a new change stream.
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
startAfter
</code>
is mutually exclusive with
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
and
<code class="leafygreen-ui-1l06pbn">
startAtOperationTime
</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">
fullDocument
</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. By default,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.watch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.watch()
</code>
</span>
</a>
returns the delta of
those fields modified by an update operation, instead of the entire
updated document.
</p>
<p class="leafygreen-ui-1kp3ins">
Set
<code class="leafygreen-ui-1l06pbn">
fullDocument
</code>
to
<code class="leafygreen-ui-1l06pbn">
"updateLookup"
</code>
to direct
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.watch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.watch()
</code>
</span>
</a>
to look up the most current
majority-committed version of the updated document.
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.watch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.watch()
</code>
</span>
</a>
returns a
<code class="leafygreen-ui-1l06pbn">
fullDocument
</code>
field with
the document lookup in addition to the
<code class="leafygreen-ui-1l06pbn">
updateDescription
</code>
delta.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0, you can set
<code class="leafygreen-ui-1l06pbn">
fullDocument
</code>
to:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"whenAvailable"
</code>
to output the document post-image, if available,
after the document was inserted, replaced, or updated.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"required"
</code>
to output the document post-image after the document
was inserted, replaced, or updated. Raises an error if the post-image
is not available.
</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">
fullDocumentBeforeChange
</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. Default is
<code class="leafygreen-ui-1l06pbn">
"off"
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 6.0, you can use the new
<code class="leafygreen-ui-1l06pbn">
fullDocumentBeforeChange
</code>
field and set it to:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"whenAvailable"
</code>
to output the document pre-image, if available,
before the document was replaced, updated, or deleted.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"required"
</code>
to output the document pre-image before the document
was replaced, updated, or deleted. Raises an error if the pre-image
is not available.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
"off"
</code>
to suppress the document pre-image.
<code class="leafygreen-ui-1l06pbn">
"off"
</code>
is the
default.
</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">
batchSize
</code>
</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>
<p class="leafygreen-ui-1kp3ins">
Optional. Specifies the maximum number of change events to return in each
batch of the response from the MongoDB cluster.
</p>
<p class="leafygreen-ui-1kp3ins">
Has the same functionality as
<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>
</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">
maxAwaitTimeMS
</code>
</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>
<p class="leafygreen-ui-1kp3ins">
Optional. The maximum amount of time in milliseconds the server waits for new
data changes to report to the change stream cursor before returning an
empty batch.
</p>
<p class="leafygreen-ui-1kp3ins">
Defaults to
<code class="leafygreen-ui-1l06pbn">
1000
</code>
milliseconds.
</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">
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>
<p class="leafygreen-ui-1kp3ins">
Optional. Pass a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation-document-fields">
collation document
</a>
to specify a collation for the change stream cursor.
</p>
<p class="leafygreen-ui-1kp3ins">
If omitted, defaults to
<code class="leafygreen-ui-1l06pbn">
simple
</code>
binary comparison.
</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">
startAtOperationTime
</code>
</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>
<p class="leafygreen-ui-1kp3ins">
Optional. The starting point for the change stream. If the specified starting
point is in the past, it must be in the time range of the oplog. To
check the time range of the oplog, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.printReplicationInfo/#mongodb-method-rs.printReplicationInfo">
<code class="leafygreen-ui-1l06pbn">
rs.printReplicationInfo()
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
startAtOperationTime
</code>
is mutually exclusive with
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
and
<code class="leafygreen-ui-1l06pbn">
startAfter
</code>
.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</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>
A
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-cursor">
cursor
</a>
over the change event documents.
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/">
Change Events
</a>
for examples of change
event documents.
</td>
</tr>
</tbody>
</table>
<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.watch/#mongodb-method-db.collection.watch">
<code class="leafygreen-ui-1l06pbn">
db.collection.watch()
</code>
</a>
and
<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>
</dd>
</dl>
</section>
<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>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Deployment
<a class="headerlink leafygreen-ui-1kepx47" href="#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="deployment">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.watch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.watch()
</code>
</span>
</a>
is available for replica sets and sharded
clusters:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For a replica set, you can issue
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.watch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.watch()
</code>
</span>
</a>
on any
data-bearing member.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For a sharded cluster, you must issue
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.watch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.watch()
</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.
</p>
</li>
</ul>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Storage Engine
<a class="headerlink leafygreen-ui-1kepx47" href="#storage-engine" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="storage-engine">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can only use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.watch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.watch()
</code>
</span>
</a>
with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
Wired Tiger
storage engine
<!-- -->
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Read Concern
<code class="leafygreen-ui-1l06pbn">
majority
</code>
Support
<a class="headerlink leafygreen-ui-1kepx47" href="#read-concern-majority-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="read-concern-majority-support">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/changeStreams/#std-label-changeStreams">
Change streams
</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>
</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>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
You cannot run
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.watch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.watch()
</code>
</span>
</a>
on the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
,
<code class="leafygreen-ui-1l06pbn">
local
</code>
, or
<code class="leafygreen-ui-1l06pbn">
config
</code>
database.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.watch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.watch()
</code>
</span>
</a>
only notifies on data changes that have
persisted to a majority of data-bearing members.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/changeStreams/#std-label-changeStreams">
change stream cursor
</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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
You can run
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.watch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.watch()
</code>
</span>
</a>
for a database that does not exist.
However, once the database is created and you drop the database, the
change stream cursor closes.
</p>
</li>
</ul>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Resumability
<a class="headerlink leafygreen-ui-1kepx47" href="#resumability" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="resumability">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Unlike the MongoDB
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
Drivers
</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>
does
not automatically attempt to resume a change stream cursor after an
error. The MongoDB drivers make
<em>
one
</em>
attempt to automatically resume
a change stream cursor after certain errors.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.watch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.watch()
</code>
</span>
</a>
uses information stored in the oplog to produce the
change event description and generate a resume token associated to
that operation. If the operation identified by the resume token
passed to the
<code class="leafygreen-ui-1l06pbn">
resumeAfter
</code>
or
<code class="leafygreen-ui-1l06pbn">
startAfter
</code>
option has already
dropped off the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-oplog">
oplog
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.watch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.watch()
</code>
</span>
</a>
cannot resume the
change stream.
</p>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/changeStreams/#std-label-change-stream-resume">
Resume a Change Stream
</a>
for more information on
resuming a change stream.
</p>
<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">
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="leafygreen-ui-tqgtui" href="/docs/manual/changeStreams/#std-label-change-stream-start-after">
startAfter
</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>
<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>
</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">
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="leafygreen-ui-tqgtui" href="/docs/manual/changeStreams/#std-label-change-stream-start-after">
startAfter
</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>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Full Document Lookup of Update Operations
<a class="headerlink leafygreen-ui-1kepx47" href="#full-document-lookup-of-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="full-document-lookup-of-update-operations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
By default, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/changeStreams/#std-label-changeStreams">
change stream cursor
</a>
returns
specific field changes/deltas for update operations. You can also
configure the change stream to look up and return the current
majority-committed version of the changed document. Depending on other
write operations that may have occurred between the update and the
lookup, the returned document may differ significantly from the
document at the time of the update.
</p>
<p class="leafygreen-ui-1kp3ins">
Depending on the number of changes applied during the update operation
and the size of the full document, there is a risk that the size of the
change event document for an update operation is greater than the 16MB
BSON document limit. If this occurs, the server closes the change stream
cursor and returns an error.
</p>
</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">
When running with access control, the user must have the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-find">
<code class="leafygreen-ui-1l06pbn">
find
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/privilege-actions/#mongodb-authaction-changeStream">
<code class="leafygreen-ui-1l06pbn">
changeStream
</code>
</a>
privilege actions on
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/resource-document/#std-label-resource-document">
database resource
</a>
. That is, a user must
have a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-roles">
role
</a>
that grants the following
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-privileges">
privilege
<!-- -->
:
</a>
</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>
<p class="leafygreen-ui-1kp3ins">
The built-in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-read">
<code class="leafygreen-ui-1l06pbn">
read
</code>
</a>
role provides the appropriate
privileges.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Cursor Iteration
<a class="headerlink leafygreen-ui-1kepx47" href="#cursor-iteration" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-iteration">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
MongoDB provides multiple ways to iterate on a cursor.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.hasNext/#mongodb-method-cursor.hasNext">
<code class="leafygreen-ui-1l06pbn">
cursor.hasNext()
</code>
</a>
method blocks and waits for the next
event. To monitor the
<code class="leafygreen-ui-1l06pbn">
watchCursor
</code>
cursor and iterate over the
events, use
<code class="leafygreen-ui-1l06pbn">
hasNext()
</code>
like this:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"while (!watchCursor.isClosed()) {\n if (watchCursor.hasNext()) {\n firstChange = watchCursor.next();\n break;\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">while</span> <!-- -->(<!-- -->!<!-- -->watchCursor.<span class="lg-highlight-title lg-highlight-function">isClosed</span>(<!-- -->)) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">if</span> <!-- -->(<!-- -->watchCursor.<span class="lg-highlight-title lg-highlight-function">hasNext</span>(<!-- -->)) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->firstChange <!-- -->= <!-- -->watchCursor.<span class="lg-highlight-title lg-highlight-function">next</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"> <!-- -->}</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/cursor.tryNext/#mongodb-method-cursor.tryNext">
<code class="leafygreen-ui-1l06pbn">
cursor.tryNext()
</code>
</a>
method is non-blocking. To monitor
the
<code class="leafygreen-ui-1l06pbn">
watchCursor
</code>
cursor and iterate over the events, use
<code class="leafygreen-ui-1l06pbn">
tryNext()
</code>
like this:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"while (!watchCursor.isClosed()) {\n let next = watchCursor.tryNext()\n while (next !== null) {\n printjson(next);\n next = watchCursor.tryNext()\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">while</span> <!-- -->(<!-- -->!<!-- -->watchCursor.<span class="lg-highlight-title lg-highlight-function">isClosed</span>(<!-- -->)) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">let</span> next <!-- -->= <!-- -->watchCursor.<span class="lg-highlight-title lg-highlight-function">tryNext</span>(<!-- -->)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">while</span> <!-- -->(<!-- -->next <!-- -->!<!-- -->== <span class="lg-highlight-literal">null</span>) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">printjson</span>(<!-- -->next)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->next <!-- -->= <!-- -->watchCursor.<span class="lg-highlight-title lg-highlight-function">tryNext</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-db.watch-example">
</span>
</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 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>
opens a
change stream cursor on the
<code class="leafygreen-ui-1l06pbn">
hr
</code>
database. The returned cursor
reports on data changes to all the non-
<code class="leafygreen-ui-1l06pbn">
system
</code>
collections in that
database.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"watchCursor = db.getSiblingDB(\"hr\").watch()","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">watchCursor <!-- -->= <!-- -->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">watch</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">
Iterate the cursor to check for new events. Use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.isClosed/#mongodb-method-cursor.isClosed">
<code class="leafygreen-ui-1l06pbn">
cursor.isClosed()
</code>
</a>
method with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.tryNext/#mongodb-method-cursor.tryNext">
<code class="leafygreen-ui-1l06pbn">
cursor.tryNext()
</code>
</a>
method to ensure the loop only exits if the change stream cursor is
closed
<em>
and
</em>
there are no objects remaining in the latest batch:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"while (!watchCursor.isClosed()) {\n let next = watchCursor.tryNext()\n while (next !== null) {\n printjson(next);\n next = watchCursor.tryNext()\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">while</span> <!-- -->(<!-- -->!<!-- -->watchCursor.<span class="lg-highlight-title lg-highlight-function">isClosed</span>(<!-- -->)) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">let</span> next <!-- -->= <!-- -->watchCursor.<span class="lg-highlight-title lg-highlight-function">tryNext</span>(<!-- -->)</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">while</span> <!-- -->(<!-- -->next <!-- -->!<!-- -->== <span class="lg-highlight-literal">null</span>) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">printjson</span>(<!-- -->next)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->next <!-- -->= <!-- -->watchCursor.<span class="lg-highlight-title lg-highlight-function">tryNext</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">
For complete documentation on change stream output, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/change-events/#std-label-change-stream-output">
Change Events
<!-- -->
.
</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">
You cannot use
<code class="leafygreen-ui-1l06pbn">
isExhausted()
</code>
with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/changeStreams/#std-label-changeStreams">
change streams
<!-- -->
.
</a>
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/journaling/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Journaling
<a class="headerlink leafygreen-ui-1kepx47" href="#journaling" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="journaling">
</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="#journaling-and-the-wiredtiger-storage-engine" target="_self">
<span>
Journaling and the WiredTiger Storage Engine
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#journaling-and-the-in-memory-storage-engine" target="_self">
<span>
Journaling and the In-Memory Storage Engine
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
To provide durability in the event of a failure, MongoDB uses
<em>
write
ahead logging
</em>
to on-disk
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-journal">
journal
</a>
files.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-journaling-wiredTiger">
</span>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Journaling and the WiredTiger Storage Engine
<a class="headerlink leafygreen-ui-1kepx47" href="#journaling-and-the-wiredtiger-storage-engine" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="journaling-and-the-wiredtiger-storage-engine">
</div>
</a>
</h2>
<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
<em>
log
</em>
mentioned in this section refers to the WiredTiger
write-ahead log (i.e. the journal) and not the MongoDB log file.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
</a>
uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger-checkpoints">
checkpoints
</a>
to provide a consistent view of data
on disk and allow MongoDB to recover from the last checkpoint. However,
if MongoDB exits unexpectedly in between checkpoints, journaling is
required to recover information that occurred after the last checkpoint.
</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.1, journaling is always enabled. As a result, MongoDB
removes the
<code class="leafygreen-ui-1l06pbn">
storage.journal.enabled
</code>
option and the corresponding
<code class="leafygreen-ui-1l06pbn">
--journal
</code>
and
<code class="leafygreen-ui-1l06pbn">
--nojournal
</code>
command-line options.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
With journaling, the recovery process:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Looks in the data files to find the identifier of the last
checkpoint.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Searches in the journal files for the record that
matches the identifier of the last checkpoint.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Apply the operations in the journal files since the last checkpoint.
</p>
</li>
</ol>
<span class="leafygreen-ui-1qnjd6d" id="std-label-journal-process">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Journaling Process
<a class="headerlink leafygreen-ui-1kepx47" href="#journaling-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="journaling-process">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
With journaling, WiredTiger creates one journal record for each client
initiated write operation. The journal record includes any internal
write operations caused by the initial write. For example, an update to
a document in a collection may result in modifications to the indexes;
WiredTiger creates a single journal record that includes both the
update operation and its associated index modifications.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB configures WiredTiger to use in-memory buffering for storing
the journal records. Threads coordinate to allocate and copy
into their portion of the buffer. All journal records up to 128
kB are buffered.
</p>
<p class="leafygreen-ui-1kp3ins">
WiredTiger syncs the buffered journal records to disk upon any of
the following conditions:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For replica set members (primary and secondary members):
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If a write operation includes or implies a write concern of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.j">
<code class="leafygreen-ui-1l06pbn">
j: true
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Additionally for secondary members, after every batch
application of the oplog entries.
</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">
Write concern
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
implies
<code class="leafygreen-ui-1l06pbn">
j: true
</code>
if
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.writeConcernMajorityJournalDefault">
<code class="leafygreen-ui-1l06pbn">
writeConcernMajorityJournalDefault
</code>
</a>
is true.
</p>
</div>
</div>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
At every 100 milliseconds (See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-storage.journal.commitIntervalMs">
<code class="leafygreen-ui-1l06pbn">
storage.journal.commitIntervalMs
</code>
</a>
).
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
When WiredTiger creates a new journal file. Because MongoDB uses a
journal file size limit of 100 MB, WiredTiger creates a new
journal file approximately every 100 MB of data.
</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">
In between write operations, while the journal records
remain in the WiredTiger buffers, updates can be lost following a
hard shutdown of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
.
</a>
</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">
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/serverStatus/#mongodb-dbcommand-dbcmd.serverStatus">
<code class="leafygreen-ui-1l06pbn">
serverStatus
</code>
</a>
command returns information on the
WiredTiger journal statistics in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/serverStatus/#mongodb-serverstatus-serverstatus.wiredTiger.log">
<code class="leafygreen-ui-1l06pbn">
wiredTiger.log
</code>
</a>
field.
</p>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Journal Files
<a class="headerlink leafygreen-ui-1kepx47" href="#journal-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="journal-files">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
For the journal files, MongoDB creates a subdirectory named
<code class="leafygreen-ui-1l06pbn">
journal
</code>
under the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-storage.dbPath">
<code class="leafygreen-ui-1l06pbn">
dbPath
</code>
</a>
directory. WiredTiger journal
files have names with the following format
<code class="leafygreen-ui-1l06pbn">
WiredTigerLog.<sequence>
</code>
where
<code class="leafygreen-ui-1l06pbn">
<sequence>
</code>
is a zero-padded number starting from
<code class="leafygreen-ui-1l06pbn">
0000000001
</code>
.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-wt-jouraling-record">
</span>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Journal Records
<a class="headerlink leafygreen-ui-1kepx47" href="#journal-records" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="journal-records">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Journal files contain a record per each client initiated write operation
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The journal record includes any internal write operations caused by
the initial write. For example, an update to a document in a
collection may result in modifications to the indexes; WiredTiger
creates a single journal record that includes both the update
operation and its associated index modifications.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Each record has a unique identifier.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The minimum journal record size for WiredTiger is 128 bytes.
</p>
</li>
</ul>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Compression
<a class="headerlink leafygreen-ui-1kepx47" href="#compression" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="compression">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
By default, MongoDB configures WiredTiger to use snappy compression for
its journaling data. To specify a different compression algorithm or no
compression, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-storage.wiredTiger.engineConfig.journalCompressor">
<code class="leafygreen-ui-1l06pbn">
storage.wiredTiger.engineConfig.journalCompressor
</code>
</a>
setting.
For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-journaling/#std-label-manage-journaling-change-wt-journal-compressor">
Change WiredTiger Journal Compressor
<!-- -->
.
</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">
If a log record less than or equal to 128 bytes (the mininum
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-wt-jouraling-record" target="_self">
<span>
log record size for WiredTiger
</span>
</a>
),
WiredTiger does not compress that record.
</p>
</div>
</div>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Journal File Size Limit
<a class="headerlink leafygreen-ui-1kepx47" href="#journal-file-size-limit" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="journal-file-size-limit">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
WiredTiger journal files have a maximum size limit of approximately 100 MB.
Once the file exceeds that limit, WiredTiger creates a new journal file.
</p>
<p class="leafygreen-ui-1kp3ins">
WiredTiger automatically removes old journal files and maintains only
the files needed to recover from the last checkpoint. To determine how much
disk space to set aside for journal files, consider the following:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The default maximum size for a checkpoint is 2 GB
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Additional space may be required for MongoDB to write new journal
files while recovering from a checkpoint
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
MongoDB compresses journal files
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The time it takes to restore a checkpoint is specific to your use case
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If you override the maximum checkpoint size or disable compression, your
calculations may be significantly different
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For these reasons, it is difficult to calculate exactly how much additional
space you need. Over-estimating disk space is always a safer approach.
</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">
If you do not set aside enough disk space for your journal
files, the MongoDB server will crash.
</p>
</div>
</div>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Pre-Allocation
<a class="headerlink leafygreen-ui-1kepx47" href="#pre-allocation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="pre-allocation">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
WiredTiger pre-allocates journal files.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-journaling-inMemory">
</span>
</section>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Journaling and the In-Memory Storage Engine
<a class="headerlink leafygreen-ui-1kepx47" href="#journaling-and-the-in-memory-storage-engine" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="journaling-and-the-in-memory-storage-engine">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
In MongoDB Enterprise, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/inmemory/">
In-Memory Storage Engine
</a>
is
part of general availability (GA). Because its data is kept in memory, there is
no separate journal. Write operations with a write concern of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.j">
<code class="leafygreen-ui-1l06pbn">
j: true
</code>
</a>
are immediately acknowledged.
</p>
<p class="leafygreen-ui-1kp3ins">
If any voting member of a replica set uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/inmemory/#std-label-storage-inmemory">
in-memory
storage engine
</a>
, you must set
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.writeConcernMajorityJournalDefault">
<code class="leafygreen-ui-1l06pbn">
writeConcernMajorityJournalDefault
</code>
</a>
to
<code class="leafygreen-ui-1l06pbn">
false
</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">
Starting in version 4.2 (and 4.0.13 and 3.6.14 ), if a replica set
member uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/inmemory/#std-label-storage-inmemory">
in-memory storage engine
</a>
(voting or non-voting) but the replica set has
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.writeConcernMajorityJournalDefault">
<code class="leafygreen-ui-1l06pbn">
writeConcernMajorityJournalDefault
</code>
</a>
set to true, the
replica set member logs a startup warning.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
With
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.writeConcernMajorityJournalDefault">
<code class="leafygreen-ui-1l06pbn">
writeConcernMajorityJournalDefault
</code>
</a>
set to
<code class="leafygreen-ui-1l06pbn">
false
</code>
,
MongoDB does not wait for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
w: "majority"
</code>
</a>
writes to be written to the on-disk journal before acknowledging the
writes. As such,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
write operations could
possibly roll back in the event of a transient loss (e.g. crash and
restart) of a majority of nodes in a given replica set.
</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/inmemory/#std-label-inmemory-durability">
In-Memory Storage Engine: Durability
</a>
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/index-unique/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" 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>
</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-a-unique-index" target="_self">
<span>
Create a Unique Index
</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>
<p class="leafygreen-ui-1kp3ins">
A unique index ensures that the indexed fields do not store duplicate
values. A unique index on a single field ensures that a value appears
at most once for a given field. A unique compound index ensures that
any given
<em>
combination
</em>
of the index key values only appears at most
once. By default, MongoDB creates a unique index on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/document/#std-label-document-id-field">
_id
</a>
field during the creation of a collection.
</p>
<a class="css-53dzkk" href="https://www.mongodb.com/docs/atlas/atlas-ui/indexes/">
<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/indexes/" target="_self">
<span>
create and manage unique indexes 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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-index-unique-create">
</span>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Create a Unique Index
<a class="headerlink leafygreen-ui-1kepx47" href="#create-a-unique-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-unique-index">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To create a unique index, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.createIndex/#mongodb-method-db.collection.createIndex">
<code class="leafygreen-ui-1l06pbn">
db.collection.createIndex()
</code>
</a>
method with the
<code class="leafygreen-ui-1l06pbn">
unique
</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":"db.collection.createIndex( <key>, { 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">collection</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( <span class="lg-highlight-tag lg-highlight-xml"><</span><span class="lg-highlight-name lg-highlight-tag lg-highlight-xml">key</span><span class="lg-highlight-tag lg-highlight-xml"> </span><span class="lg-highlight-attr lg-highlight-tag lg-highlight-xml">and</span><span class="lg-highlight-tag lg-highlight-xml"> </span><span class="lg-highlight-attr lg-highlight-tag lg-highlight-xml">index</span><span class="lg-highlight-tag lg-highlight-xml"> </span><span class="lg-highlight-attr lg-highlight-tag lg-highlight-xml">type</span><span class="lg-highlight-tag lg-highlight-xml"> </span><span class="lg-highlight-attr lg-highlight-tag lg-highlight-xml">specification</span><span class="lg-highlight-tag lg-highlight-xml">></span><span class="lg-highlight-xml">, { unique: 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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-index-unique-index">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Unique Index on a Single Field
<a class="headerlink leafygreen-ui-1kepx47" href="#unique-index-on-a-single-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="unique-index-on-a-single-field">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
For example, to create a unique index on the
<code class="leafygreen-ui-1l06pbn">
user_id
</code>
field of the
<code class="leafygreen-ui-1l06pbn">
members
</code>
collection, use the following operation 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.members.createIndex( { \"user_id\": 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">members</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( { <span class="lg-highlight-string">"user_id"</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-index-unique-compound-index">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Unique Compound Index
<a class="headerlink leafygreen-ui-1kepx47" href="#unique-compound-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="unique-compound-index">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can also enforce a unique constraint on
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-compound/#std-label-index-type-compound">
compound indexes
</a>
. A unique
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-compound/#std-label-index-type-compound">
compound index
</a>
enforces uniqueness on the
<em>
combination
</em>
of the
index key values.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, to create a unique index on
<code class="leafygreen-ui-1l06pbn">
groupNumber
</code>
,
<code class="leafygreen-ui-1l06pbn">
lastname
</code>
,
and
<code class="leafygreen-ui-1l06pbn">
firstname
</code>
fields of the
<code class="leafygreen-ui-1l06pbn">
members
</code>
collection, use the
following operation 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.members.createIndex( { groupNumber: 1, lastname: 1, firstname: 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">members</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( { <span class="lg-highlight-attr">groupNumber</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">lastname</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">firstname</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 created index enforces uniqueness for the
<em>
combination
</em>
of
<code class="leafygreen-ui-1l06pbn">
groupNumber
</code>
,
<code class="leafygreen-ui-1l06pbn">
lastname
</code>
, and
<code class="leafygreen-ui-1l06pbn">
firstname
</code>
values.
</p>
<p class="leafygreen-ui-1kp3ins">
For another example, consider a collection with the following document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ _id: 1, a: [ { loc: \"A\", qty: 5 }, { qty: 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">{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">a</span>: [ { <span class="lg-highlight-attr">loc</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">5</span> }, { <span class="lg-highlight-attr">qty</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">
Create a unique compound
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-multikey/#std-label-index-type-multikey">
multikey
</a>
index
on
<code class="leafygreen-ui-1l06pbn">
a.loc
</code>
and
<code class="leafygreen-ui-1l06pbn">
a.qty
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.createIndex( { \"a.loc\": 1, \"a.qty\": 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">collection</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( { <span class="lg-highlight-string">"a.loc"</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"a.qty"</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 unique index permits the insertion of the following documents into
the collection since the index enforces uniqueness for the
<em>
combination
</em>
of
<code class="leafygreen-ui-1l06pbn">
a.loc
</code>
and
<code class="leafygreen-ui-1l06pbn">
a.qty
</code>
values:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.insertMany( [\n { _id: 2, a: [ { loc: \"A\" }, { qty: 5 } ] },\n { _id: 3, a: [ { loc: \"A\", qty: 10 } ] }\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">insertMany</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">a</span>: [ { <span class="lg-highlight-attr">loc</span>: <span class="lg-highlight-string">"A"</span> }, { <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">5</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">a</span>: [ { <span class="lg-highlight-attr">loc</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">10</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="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-unique-separate-documents" target="_self">
<span>
Unique Constraint Across Separate Documents
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-unique-index-and-missing-field" target="_self">
<span>
Missing Document Field in a Unique Single-Field Index
</span>
</a>
</p>
</li>
</ul>
</div>
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-unique-index-restrictions">
</span>
<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">
MongoDB cannot create a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-index-type-unique" target="_self">
<span>
unique index
</span>
</a>
on the
specified index field(s) if the collection already contains data that
would violate the unique constraint for the index.
</p>
<p class="leafygreen-ui-1kp3ins">
You may not specify a unique constraint on a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-hashed/#std-label-index-type-hashed">
hashed
index.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Building Unique Index on Replica Sets and Sharded Clusters
<a class="headerlink leafygreen-ui-1kepx47" href="#building-unique-index-on-replica-sets-and-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="building-unique-index-on-replica-sets-and-sharded-clusters">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
For replica sets and sharded clusters, using a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/build-indexes-on-replica-sets/#std-label-index-build-on-replica-sets">
rolling procedure
</a>
to create a unique index
requires that you stop all writes to the collection during the
procedure. If you cannot stop all writes to the collection during the
procedure, do not use the rolling procedure. Instead, to build your
unique index on the collection you must either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Run
<code class="leafygreen-ui-1l06pbn">
db.collection.createIndex()
</code>
on the primary for a
replica set
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Run
<code class="leafygreen-ui-1l06pbn">
db.collection.createIndex()
</code>
on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
for a sharded cluster
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-unique-separate-documents">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Unique Constraint Across Separate Documents
<a class="headerlink leafygreen-ui-1kepx47" href="#unique-constraint-across-separate-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="unique-constraint-across-separate-documents">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The unique constraint applies to separate documents in the collection.
That is, the unique index prevents
<em>
separate
</em>
documents from having the
same value for the indexed key.
</p>
<p class="leafygreen-ui-1kp3ins">
Because the constraint applies to separate documents, for a unique
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-multikey/#std-label-index-type-multikey">
multikey
</a>
index, a document may have array
elements that result in repeating index key values as long as the index
key values for that document do not duplicate those of another
document. In this case, the repeated index entry is inserted into the
index only once.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, consider a 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, a: [ { loc: \"A\", qty: 5 }, { qty: 10 } ] }\n{ _id: 2, a: [ { loc: \"A\" }, { qty: 5 } ] }\n{ _id: 3, a: [ { loc: \"A\", qty: 10 } ] }","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">1</span>, <span class="lg-highlight-attr">a</span>: [ { <span class="lg-highlight-attr">loc</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">5</span> }, { <span class="lg-highlight-attr">qty</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-number">2</span>, <span class="lg-highlight-attr">a</span>: [ { <span class="lg-highlight-attr">loc</span>: <span class="lg-highlight-string">"A"</span> }, { <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">5</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">a</span>: [ { <span class="lg-highlight-attr">loc</span>: <span class="lg-highlight-string">"A"</span>, <span class="lg-highlight-attr">qty</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">
Create a unique compound multikey index on
<code class="leafygreen-ui-1l06pbn">
a.loc
</code>
and
<code class="leafygreen-ui-1l06pbn">
a.qty
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.createIndex( { \"a.loc\": 1, \"a.qty\": 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">collection</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( { <span class="lg-highlight-string">"a.loc"</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"a.qty"</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 unique index permits the insertion of the following document into
the collection if no other document in the collection has an index key
value of
<code class="leafygreen-ui-1l06pbn">
{ "a.loc": "B", "a.qty": null }
</code>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.insertOne( { _id: 4, a: [ { loc: \"B\" }, { loc: \"B\" } ] } )","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">insertOne</span>( { <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-attr">a</span>: [ { <span class="lg-highlight-attr">loc</span>: <span class="lg-highlight-string">"B"</span> }, { <span class="lg-highlight-attr">loc</span>: <span class="lg-highlight-string">"B"</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-unique-index-and-missing-field">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Missing Document Field in a Unique Single-Field Index
<a class="headerlink leafygreen-ui-1kepx47" href="#missing-document-field-in-a-unique-single-field-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="missing-document-field-in-a-unique-single-field-index">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If a document has a
<code class="leafygreen-ui-1l06pbn">
null
</code>
or missing value for the indexed field in a unique
single-field index, the index stores a
<code class="leafygreen-ui-1l06pbn">
null
</code>
value for that document.
Because of the unique constraint, a single-field unique index can only
contain one document that contains a
<code class="leafygreen-ui-1l06pbn">
null
</code>
value in its index entry. If there is
more than one document with a
<code class="leafygreen-ui-1l06pbn">
null
</code>
value in its index entry, the index
build fails with a duplicate key error.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, a collection has a unique single-field index on
<code class="leafygreen-ui-1l06pbn">
x
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.createIndex( { \"x\": 1 }, { unique: 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">db.<span class="lg-highlight-property">collection</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( { <span class="lg-highlight-string">"x"</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 unique index allows the insertion of a document without the field
<code class="leafygreen-ui-1l06pbn">
x
</code>
if the collection does not already contain a document missing the
field
<code class="leafygreen-ui-1l06pbn">
x
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.insertOne( { y: 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">insertOne</span>( { <span class="lg-highlight-attr">y</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">
However, you cannot insert a document without the field
<code class="leafygreen-ui-1l06pbn">
x
</code>
if the
collection already contains a document missing the field
<code class="leafygreen-ui-1l06pbn">
x
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.insertOne( { z: 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">insertOne</span>( { <span class="lg-highlight-attr">z</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 operation fails to insert the document because of the violation of
the unique constraint on the value of the field
<code class="leafygreen-ui-1l06pbn">
x
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"WriteResult({\n \"nInserted\" : 0,\n \"writeError\" : {\n \"code\" : 11000,\n \"errmsg\" : \"E11000 duplicate key error index: test.collection.$a.b_1 dup key: { : null }\"\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-title lg-highlight-class">WriteResult</span>({</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nInserted"</span> : <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"writeError"</span> : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"code"</span> : <span class="lg-highlight-number">11000</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"errmsg"</span> : <span class="lg-highlight-string">"E11000 duplicate key error index: test.collection.$a.b_1 dup key: { : 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 class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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-unique-partial-indexes">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Missing Document Fields in a Unique Compound Index
<a class="headerlink leafygreen-ui-1kepx47" href="#missing-document-fields-in-a-unique-compound-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="missing-document-fields-in-a-unique-compound-index">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If a document has a
<code class="leafygreen-ui-1l06pbn">
null
</code>
or missing value for one or more indexed
fields in a unique compound index, the index stores a null value for
each
<code class="leafygreen-ui-1l06pbn">
null
</code>
or missing field in the document's index entry. Because of
the unique constraint, a unique compound index only permits one document
that has a
<code class="leafygreen-ui-1l06pbn">
null
</code>
value for all indexed fields in an index entry. If
there is more than one index entry with a
<code class="leafygreen-ui-1l06pbn">
null
</code>
value for all indexed
fields, the index build fails with a duplicate key error. MongoDB
permits multiple documents with missing fields in unique compound
indexes as long as each index entry is unique.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, a collection
<code class="leafygreen-ui-1l06pbn">
students
</code>
has a unique compound index on fields
<code class="leafygreen-ui-1l06pbn">
name
</code>
,
<code class="leafygreen-ui-1l06pbn">
age
</code>
, and
<code class="leafygreen-ui-1l06pbn">
grade
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students.createIndex(\n {\n \"name\": 1,\n \"age\": -1,\n \"grade\": 1\n },\n { unique: 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">students</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</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">"age"</span>: -<span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"grade"</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">unique</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">
If the collection does not already contain identical documents, the
unique compound index allows the insertion of the following documents
that are all missing the
<code class="leafygreen-ui-1l06pbn">
grade
</code>
field.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students.insertMany(\n { \"name\": \"Meredith\", \"age\": 12 },\n { \"name\": \"Olivia\", \"age\": 11 },\n { \"name\": \"Benjamin\" }\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">"name"</span>: <span class="lg-highlight-string">"Meredith"</span>, <span class="lg-highlight-string">"age"</span>: <span class="lg-highlight-number">12</span> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> { <span class="lg-highlight-string">"name"</span>: <span class="lg-highlight-string">"Olivia"</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">"name"</span>: <span class="lg-highlight-string">"Benjamin"</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">
However, you cannot insert a document that has the same index key (value
for
<code class="leafygreen-ui-1l06pbn">
name
</code>
,
<code class="leafygreen-ui-1l06pbn">
age
</code>
, and
<code class="leafygreen-ui-1l06pbn">
grade
</code>
) as another document in the
collection.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students.insertOne( { name: \"Meredith\", age: 12 } )","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</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>( { <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Meredith"</span>, <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">12</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 fails to insert the document because of the violation of
the unique constraint on the values of the fields
<code class="leafygreen-ui-1l06pbn">
name
</code>
,
<code class="leafygreen-ui-1l06pbn">
age
</code>
, and
<code class="leafygreen-ui-1l06pbn">
grade
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"WriteResult({\n \"nInserted\" : 0,\n \"writeError\" : {\n \"code\" : 11000,\n \"errmsg\" :\n \"E11000 duplicate key error collection: test.students\n index: name_1_age_-1_grade_1\n dup key: { name: \"Meredith\", age: 12, grade: null }\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-title lg-highlight-class">WriteResult</span>({</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nInserted"</span> : <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"writeError"</span> : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"code"</span> : <span class="lg-highlight-number">11000</span>,</td></tr><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">"E11000 duplicate key error collection: test.students</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> index: name_1_age_-1_grade_1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> dup key: { name: "</span><span class="lg-highlight-title lg-highlight-class">Meredith</span><span class="lg-highlight-string">", age: 12, grade: null }</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></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 also cannot insert a document that is unique but shares an index
key with an existing index entry.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.students.insertOne( { name: \"Olivia\", \"age\": 11, \"favorite color\": \"red\"} )","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">students</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>( { <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Olivia"</span>, <span class="lg-highlight-string">"age"</span>: <span class="lg-highlight-number">11</span>, <span class="lg-highlight-string">"favorite color"</span>: <span class="lg-highlight-string">"red"</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 fails to insert the document because of the violation of
the unique constraint on the values of the fields
<code class="leafygreen-ui-1l06pbn">
name
</code>
,
<code class="leafygreen-ui-1l06pbn">
age
</code>
, and
<code class="leafygreen-ui-1l06pbn">
grade
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"WriteResult({\n \"nInserted\" : 0,\n \"writeError\" : {\n \"code\" : 11000,\n \"errmsg\" :\n \"E11000 duplicate key error collection: test.students\n index: name_1_age_-1_grade_1\n dup key: { name: \"Olivia\", age: 11, grade: null }\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-title lg-highlight-class">WriteResult</span>({</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nInserted"</span> : <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"writeError"</span> : {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"code"</span> : <span class="lg-highlight-number">11000</span>,</td></tr><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">"E11000 duplicate key error collection: test.students</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> index: name_1_age_-1_grade_1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string"> dup key: { name: "</span><span class="lg-highlight-title lg-highlight-class">Olivia</span><span class="lg-highlight-string">", age: 11, grade: null }</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></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Unique Partial Indexes
<a class="headerlink leafygreen-ui-1kepx47" href="#unique-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="unique-partial-indexes">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Partial indexes only index the documents in a collection that meet a
specified filter expression. If you specify both the
<code class="leafygreen-ui-1l06pbn">
partialFilterExpression
</code>
and a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-index-type-unique" target="_self">
<span>
unique constraint
</span>
</a>
, the unique constraint only applies to the
documents that meet the filter expression.
</p>
<p class="leafygreen-ui-1kp3ins">
A partial index with a unique constraint does not prevent the insertion
of documents that do not meet the unique constraint if the documents do
not meet the filter criteria. For an example, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-partial/#std-label-partial-index-with-unique-constraints">
Partial Index with Unique Constraint.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sharded-clusters-unique-indexes">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Sharded Clusters and Unique Indexes
<a class="headerlink leafygreen-ui-1kepx47" href="#sharded-clusters-and-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="sharded-clusters-and-unique-indexes">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You cannot specify a unique constraint on a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-hashed/#std-label-index-type-hashed">
hashed index.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
For a ranged sharded collection, only the following indexes can be
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-index-type-unique" target="_self">
<span>
unique:
</span>
</a>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the index on the shard key
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-compound-index">
compound index
</a>
where the shard key is a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/index-compound/#std-label-compound-index-prefix">
prefix
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the default
<code class="leafygreen-ui-1l06pbn">
_id
</code>
index;
<strong>
however
</strong>
, the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
index only
enforces the uniqueness constraint per shard
<strong>
if
</strong>
the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field
is
<strong>
not
</strong>
the shard key or the prefix of the shard key.
</p>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Uniqueness and the _id Index
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field is not the shard key or the prefix of the
shard key,
<code class="leafygreen-ui-1l06pbn">
_id
</code>
index only enforces the uniqueness constraint
per shard and
<strong>
not
</strong>
across shards.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, consider a sharded collection (with shard key
<code class="leafygreen-ui-1l06pbn">
{x:
1}
</code>
) that spans two shards A and B. Because the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
key is
not part of the shard key, the collection could have a document
with
<code class="leafygreen-ui-1l06pbn">
_id
</code>
value
<code class="leafygreen-ui-1l06pbn">
1
</code>
in shard A and another document with
<code class="leafygreen-ui-1l06pbn">
_id
</code>
value
<code class="leafygreen-ui-1l06pbn">
1
</code>
in shard B.
</p>
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field is not the shard key nor the prefix of the
shard key, MongoDB expects applications to enforce the uniqueness
of the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
values across the shards.
</p>
</div>
</div>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The unique index constraints mean that:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For a to-be-sharded collection, you cannot shard the collection if
the collection has other unique indexes.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For an already-sharded collection, you cannot create unique indexes
on other fields.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
To maintain uniqueness on a field that is not your shard key,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/unique-constraints-on-arbitrary-fields/#std-label-shard-key-arbitrary-uniqueness">
Unique Constraints on Arbitrary Fields.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Sparse and Non-Sparse Unique Indexes
<a class="headerlink leafygreen-ui-1kepx47" href="#sparse-and-non-sparse-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="sparse-and-non-sparse-unique-indexes">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.0,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-sparse/#std-label-sparse-unique-index">
unique sparse
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-properties/#std-label-unique-index">
unique non-sparse
</a>
indexes with the same
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.createIndexes/#std-label-key_patterns">
key pattern
</a>
can exist on a single collection.
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Unique and Sparse Index Creation
<a class="headerlink leafygreen-ui-1kepx47" href="#unique-and-sparse-index-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="unique-and-sparse-index-creation">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
This example creates multiple indexes with the same key pattern and
different
<code class="leafygreen-ui-1l06pbn">
sparse
</code>
options:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.scoreHistory.createIndex( { score : 1 }, { name: \"unique_index\", unique: true } )\ndb.scoreHistory.createIndex( { score : 1 }, { name: \"unique_sparse_index\", unique: true, sparse: 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">db.<span class="lg-highlight-property">scoreHistory</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( { score : <span class="lg-highlight-number">1</span> }, { <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"unique_index"</span>, <span class="lg-highlight-attr">unique</span>: <span class="lg-highlight-literal">true</span> } )</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">scoreHistory</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( { score : <span class="lg-highlight-number">1</span> }, { <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"unique_sparse_index"</span>, <span class="lg-highlight-attr">unique</span>: <span class="lg-highlight-literal">true</span>, <span class="lg-highlight-attr">sparse</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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Basic and Sparse Index Creation
<a class="headerlink leafygreen-ui-1kepx47" href="#basic-and-sparse-index-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="basic-and-sparse-index-creation">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
You can also create basic indexes with the same key pattern with and
without the sparse option:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.scoreHistory.createIndex( { score : 1 }, { name: \"sparse_index\", sparse: true } )\ndb.scoreHistory.createIndex( { score : 1 }, { name: \"basic_index\" } )","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">scoreHistory</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( { score : <span class="lg-highlight-number">1</span> }, { <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"sparse_index"</span>, <span class="lg-highlight-attr">sparse</span>: <span class="lg-highlight-literal">true</span> } )</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">scoreHistory</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( { score : <span class="lg-highlight-number">1</span> }, { <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"basic_index"</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>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Basic and Unique Indexes With Duplicate Key Patterns
<a class="headerlink leafygreen-ui-1kepx47" href="#basic-and-unique-indexes-with-duplicate-key-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="basic-and-unique-indexes-with-duplicate-key-patterns">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 5.0, basic and unique indexes can exist with the
same
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.createIndexes/#std-label-key_patterns">
key pattern.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
This duplication in key patterns allows for adding a unique index to
already indexed fields.
</p>
<p class="leafygreen-ui-1kp3ins">
In this example:
</p>
<p class="leafygreen-ui-1kp3ins">
Create a basic index with the key pattern
<code class="leafygreen-ui-1l06pbn">
{ score : 1 }
</code>
and insert
three documents.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.scoreHistory.createIndex( { score : 1 }, { name: \"basic_index\" } )\ndb.scoreHistory.insert( { score : 1 } )\ndb.scoreHistory.insert( { score : 2 } )\ndb.scoreHistory.insert( { score : 3 } )","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">scoreHistory</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( { score : <span class="lg-highlight-number">1</span> }, { <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"basic_index"</span> } )</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">scoreHistory</span>.<span class="lg-highlight-title lg-highlight-function">insert</span>( { score : <span class="lg-highlight-number">1</span> } )</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">scoreHistory</span>.<span class="lg-highlight-title lg-highlight-function">insert</span>( { score : <span class="lg-highlight-number">2</span> } )</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">scoreHistory</span>.<span class="lg-highlight-title lg-highlight-function">insert</span>( { score : <span class="lg-highlight-number">3</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">
Create a unique index with the same key pattern
<code class="leafygreen-ui-1l06pbn">
{ score : 1 }
</code>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.scoreHistory.createIndex( { score : 1 }, { name: \"unique_index\", 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">scoreHistory</span>.<span class="lg-highlight-title lg-highlight-function">createIndex</span>( { score : <span class="lg-highlight-number">1</span> }, { <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"unique_index"</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">
Try to insert a duplicate
<code class="leafygreen-ui-1l06pbn">
score
</code>
document that fails because of
the unique index.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.scoreHistory.insert( { score : 3 } )","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">scoreHistory</span>.<span class="lg-highlight-title lg-highlight-function">insert</span>( { score : <span class="lg-highlight-number">3</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/core/defragment-sharded-collections/monitor-defragmentation-sharded-collection/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Monitor Defragmentation of a Sharded Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#monitor-defragmentation-of-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="monitor-defragmentation-of-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 monitor defragmentation of a sharded collection, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/balancerCollectionStatus/#mongodb-dbcommand-dbcmd.balancerCollectionStatus">
<code class="leafygreen-ui-1l06pbn">
balancerCollectionStatus
</code>
</a>
command.
</p>
<p class="leafygreen-ui-1kp3ins">
You can see the current defragmentation state and the number of
remaining chunks to process. This shows you the defragmentation
progress.
</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">
Defragmentation uses the following phases to reduce the number of chunks
in a collection and improve performance:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Merge chunks on the same shard that can be merged.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Migrate smaller chunks to other shards. A small chunk is one that
contains data less than 25% of the
<code class="leafygreen-ui-1l06pbn">
chunkSize
</code>
setting.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Merge remaining chunks on the same shard that can be merged.
</p>
</li>
</ol>
<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>
<p class="leafygreen-ui-1kp3ins">
In the procedure for this task, you monitor the phases and see the
defragmentation progress.
</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>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Start defragmenting a sharded collection. For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/defragment-sharded-collections/start-defragmenting-sharded-collection/#std-label-start-defragmenting-sharded-collection">
Start Defragmenting a Sharded Collection
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<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>
</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":"Run:","name":"Monitor defragmentation"},{"@type":"HowToStep","text":"The previous command returns a document with information about\ndefragmentation status, current phase, and the defragmentation\nwork remaining. For example:The following table describes the document fields.FieldTypeDescriptionbalancerCompliantBooleanfalse if collection chunks must be moved. Otherwise,\ntrue.firstComplianceViolationStringIndicates the reason that chunks for the namespace must be\nmoved or merged. Only returned if balancerCompliant is\nfalse.detailsObjectAddtional information about the current defragmentation\nstate. Only returned if firstComplianceViolation is\ndefragmentingChunks.currentPhaseStringCurrent defragmentation phase:For phase one, currentPhase is\nmergeAndMeasureChunks.Phase one merges contiguous chunks located on the same\nshard and calculates the data size for those chunks.For phase two, currentPhase is\nmoveAndMergeChunks.After phase one is complete, there might be some small\nchunks remaining. Phase two migrates those small chunks\nto other shards and merges the chunks on those shards.remainingChunksToProcessIntegerNumber of remaining chunks to process in the current phase.For additional information about the returned document fields, see\nthe balancer collection status output document.","name":"Examine output document"},{"@type":"HowToStep","text":"After defragmentation completes, the command returns either:balancerCompliant: true if your collection is balanced.balancerCompliant: false with firstComplianceViolation\nset to a string other than defragmentingChunks if\nyour collection is not balanced.Example output for a balanced collection after defragmentation\ncompletes:","name":"Confirm that defragmentation is complete"}],"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">
Monitor defragmentation
<a class="headerlink leafygreen-ui-1kepx47" href="#monitor-defragmentation" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="monitor-defragmentation">
</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 balancerCollectionStatus: \"test.ordersShardedCollection\"\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">balancerCollectionStatus</span>: <span class="lg-highlight-string">"test.ordersShardedCollection"</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">
Examine output document
<a class="headerlink leafygreen-ui-1kepx47" href="#examine-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="examine-output-document">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The previous command returns a document with information about
defragmentation status, current phase, and the defragmentation
work remaining. For example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"balancerCompliant\": false,\n \"firstComplianceViolation\": \"defragmentingChunks\",\n \"details\": {\n \"currentPhase\": \"moveAndMergeChunks\",\n \"progress\": { \"remainingChunksToProcess\": 1 }\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">"balancerCompliant"</span>: <span class="lg-highlight-literal">false</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"firstComplianceViolation"</span>: <span class="lg-highlight-string">"defragmentingChunks"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"details"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"currentPhase"</span>: <span class="lg-highlight-string">"moveAndMergeChunks"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"progress"</span>: <!-- -->{ <span class="lg-highlight-string">"remainingChunksToProcess"</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 following table describes the document 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">
balancerCompliant
</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>
<code class="leafygreen-ui-1l06pbn">
false
</code>
if collection chunks must be moved. Otherwise,
<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">
firstComplianceViolation
</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>
Indicates the reason that chunks for the namespace must be
moved or merged. Only returned if
<code class="leafygreen-ui-1l06pbn">
balancerCompliant
</code>
is
<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">
details
</code>
</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>
Addtional information about the current defragmentation
state. Only returned if
<code class="leafygreen-ui-1l06pbn">
firstComplianceViolation
</code>
is
<code class="leafygreen-ui-1l06pbn">
defragmentingChunks
</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">
currentPhase
</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">
Current defragmentation phase:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For phase one,
<code class="leafygreen-ui-1l06pbn">
currentPhase
</code>
is
<code class="leafygreen-ui-1l06pbn">
mergeAndMeasureChunks
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
Phase one merges contiguous chunks located on the same
shard and calculates the data size for those chunks.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For phase two,
<code class="leafygreen-ui-1l06pbn">
currentPhase
</code>
is
<code class="leafygreen-ui-1l06pbn">
moveAndMergeChunks
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
After phase one is complete, there might be some small
chunks remaining. Phase two migrates those small chunks
to other shards and merges the chunks on those shards.
</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">
remainingChunksToProcess
</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 remaining chunks to process in the current phase.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
For additional information about the returned document fields, see
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/balancerCollectionStatus/#std-label-cmd-balancer-CollectionStatus-output">
balancer collection status output document
<!-- -->
.
</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>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Confirm that defragmentation is complete
<a class="headerlink leafygreen-ui-1kepx47" href="#confirm-that-defragmentation-is-complete" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="confirm-that-defragmentation-is-complete">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
After defragmentation completes, the command returns either:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
balancerCompliant: true
</code>
if your collection is balanced.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
balancerCompliant: false
</code>
with
<code class="leafygreen-ui-1l06pbn">
firstComplianceViolation
</code>
set to a string other than
<code class="leafygreen-ui-1l06pbn">
defragmentingChunks
</code>
if
your collection is not balanced.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Example output for a balanced collection after defragmentation
completes:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n chunkSize: 0.2,\n balancerCompliant: true,\n ok: 1,\n '$clusterTime': {\n clusterTime: Timestamp({ t: 1677543079, i: 1 }),\n signature: {\n hash: Binary(Buffer.from(\"0000000000000000000000000000000000000000\", \"hex\"), 0),\n keyId: Long(\"0\")\n }\n },\n operationTime: Timestamp({ t: 1677543079, 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=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">chunkSize</span>: <span class="lg-highlight-number">0.2</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">balancerCompliant</span>: <span class="lg-highlight-literal">true</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">1677543079</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">1677543079</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>
</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">
If defragmentation has not yet completed, you can stop it. For details,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/defragment-sharded-collections/stop-defragmenting-sharded-collection/#std-label-stop-defragmenting-sharded-collection">
Stop Defragmenting 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">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/defragment-sharded-collections/start-defragmenting-sharded-collection/#std-label-start-defragmenting-sharded-collection">
Start defragmenting a sharded collection
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/defragment-sharded-collections/stop-defragmenting-sharded-collection/#std-label-stop-defragmenting-sharded-collection">
Stop defragmenting a sharded collection
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To view the balancer collection status output document, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/balancerCollectionStatus/#std-label-cmd-balancer-CollectionStatus-output">
Balancer collection status output document
</a>
</p>
</li>
</ul>
<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/reference/method/db.collection.find/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.collection.find()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.collection.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.find--">
</div>
</a>
<button aria-disabled="false" class="lg-ui-button-0000 leafygreen-ui-1a0rj3l" data-lgid="lg-button" role="button" type="button">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg alt="" aria-hidden="true" class="leafygreen-ui-18si895" fill="none" height="16" role="presentation" viewbox="0 0 16 16" width="16">
<path d="M6.11 13.262a.5.5 0 0 0 .395.586l.737.143a.5.5 0 0 0 .585-.396L9.926 2.738a.5.5 0 0 0-.396-.586l-.737-.143a.5.5 0 0 0-.585.396L6.109 13.262ZM1.36 7.246 3.976 5.11a.507.507 0 0 1 .704.063l.64.752a.483.483 0 0 1-.064.69L3.562 7.998 5.256 9.38c.212.173.24.482.064.69l-.64.752a.507.507 0 0 1-.704.062L1.36 8.75A.971.971 0 0 1 1 7.998c0-.29.132-.566.36-.752ZM14.636 7.246 12.02 5.11a.507.507 0 0 0-.704.063l-.64.752a.483.483 0 0 0 .064.69l1.694 1.382L10.74 9.38a.483.483 0 0 0-.064.69l.64.752a.507.507 0 0 0 .704.062l2.616-2.134a.971.971 0 0 0 .36-.752.971.971 0 0 0-.36-.752Z" fill="currentColor">
</path>
</svg>
Open Interactive Tutorial
</div>
</button>
</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>
<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 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://www.mongodb.com/docs/drivers/csharp/current/fundamentals/crud/read-operations/retrieve/" 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/crud/read-operations/retrieve/" 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://www.mongodb.com/docs/drivers/node/current/fundamentals/crud/read-operations/retrieve/" 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(#:R2dttqcmqn9:paint2)">
</path>
</g>
</g>
</g>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":R2dttqcmqn9: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=":R2dttqcmqn9: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=":R2dttqcmqn9: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/read/retrieve/" 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=":R2httqcmqn9: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(#:R2httqcmqn9:aa)">
</path>
<lineargradient gradienttransform="matrix(.5625 0 0 -.568 -160.323 -146.09)" gradientunits="userSpaceOnUse" id=":R2httqcmqn9: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(#:R2httqcmqn9:bb)">
</path>
<radialgradient cx="-2274.721" cy="144.526" gradienttransform="matrix(0 -.2399 -1.0547 0 208.637 -414.922)" gradientunits="userSpaceOnUse" id=":R2httqcmqn9: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(#:R2httqcmqn9:cc)" opacity=".444">
</path>
</svg>
<span>
PyMongo
</span>
</a>
<a class="leafygreen-ui-18cpm0b" href="https://mongoc.org/libmongoc/current/mongoc_collection_find_with_opts.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_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/read/retrieve/" 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/crud/read-operations/retrieve/" 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/read/retrieve-data/" 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://www.mongodb.com/docs/drivers/kotlin/coroutine/upcoming/fundamentals/crud/read-operations/retrieve/" 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(#:R35ttqcmqn9:paint0_linear_211_40588)">
</path>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":R35ttqcmqn9: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://www.mongodb.com/docs/languages/kotlin/kotlin-sync-driver/current/read/retrieve/" 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(#:R39ttqcmqn9:paint0_linear_211_40588)">
</path>
<defs>
<lineargradient gradientunits="userSpaceOnUse" id=":R39ttqcmqn9: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-find/" 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.mongodb.com/docs/mongoid/current/reference/queries/" 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=":R3httqcmqn9: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=":R3httqcmqn9: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=":R3httqcmqn9: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=":R3httqcmqn9: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=":R3httqcmqn9: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=":R3httqcmqn9: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=":R3httqcmqn9: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=":R3httqcmqn9: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=":R3httqcmqn9: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=":R3httqcmqn9: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=":R3httqcmqn9: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=":R3httqcmqn9: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=":R3httqcmqn9: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=":R3httqcmqn9: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=":R3httqcmqn9: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=":R3httqcmqn9: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(#:R3httqcmqn9:a)">
</path>
<path d="M240.677 241.257L224.482 129.48l-44.113 58.25 60.308 53.528z" fill="url(#:R3httqcmqn9:b)">
</path>
<path d="M240.896 241.257l-118.646-9.313-69.674 21.986 188.32-12.673z" fill="url(#:R3httqcmqn9:c)">
</path>
<path d="M52.744 253.955l29.64-97.1L17.16 170.8l35.583 83.154z" fill="url(#:R3httqcmqn9:d)">
</path>
<path d="M180.358 188.05L153.085 81.226l-78.047 73.16 105.32 33.666z" fill="url(#:R3httqcmqn9:e)">
</path>
<path d="M248.693 82.73l-73.777-60.256-20.544 66.418 94.321-6.162z" fill="url(#:R3httqcmqn9:f)">
</path>
<path d="M214.191.99L170.8 24.97 143.424.669l70.767.322z" fill="url(#:R3httqcmqn9:g)">
</path>
<path d="M0 203.372l18.177-33.151-14.704-39.494L0 203.372z" fill="url(#:R3httqcmqn9: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(#:R3httqcmqn9: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(#:R3httqcmqn9: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(#:R3httqcmqn9: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(#:R3httqcmqn9: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(#:R3httqcmqn9: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(#:R3httqcmqn9: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(#:R3httqcmqn9: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(#:R3httqcmqn9:p)">
</path>
</svg>
<span>
Mongoid
</span>
</a>
<a class="leafygreen-ui-18cpm0b" href="https://www.mongodb.com/docs/drivers/rust/current/fundamentals/crud/read-operations/retrieve/" 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/read-ops/" 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=":R3pttqcmqn9: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(#:R3pttqcmqn9: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">
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.find(query, projection, options)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.collection.find" 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.find">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Selects documents in a collection or view and returns a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-cursor">
cursor
</a>
to the selected documents.
</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>
A
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-cursor">
cursor
</a>
to the documents that match the
<code class="leafygreen-ui-1l06pbn">
query
</code>
criteria. When the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</span>
</a>
method
"returns documents," the method is actually returning a cursor to
the documents.
</td>
</tr>
</tbody>
</table>
</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 has limited support 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
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find()
</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( <query>, <projection>, <options> )","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<!-- -->><!-- -->,<!-- --> <!-- --><<!-- -->projection<!-- -->><!-- -->,<!-- --> <!-- --><<!-- -->options<!-- -->> <!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</span>
</a>
method takes the following
parameters:
</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-method-find-query" target="_self">
<span>
query
</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-method-find-query">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. Specifies selection filter using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/#std-label-query-projection-operators-top">
query
operators
</a>
. To return all
documents in a collection, omit this parameter or pass an
empty document (
<code class="leafygreen-ui-1l06pbn">
{}
</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-method-find-projection" target="_self">
<span>
projection
</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-method-find-projection">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. Specifies the fields to return in the documents that match the
query filter. To return all fields in the matching documents, omit this
parameter. For details, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-find-projection" target="_self">
<span>
Projection
<!-- -->
.
</span>
</a>
</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-method-find-options" target="_self">
<span>
options
</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-method-find-options">
</span>
<p class="leafygreen-ui-1kp3ins">
Optional. Specifies additional options for the query. These options
modify query behavior and how results are returned. For details,
see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-find-options" target="_self">
<span>
Options
<!-- -->
.
</span>
</a>
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-find-projection">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Projection
<a class="headerlink leafygreen-ui-1kepx47" href="#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="projection">
</div>
</a>
</h3>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Language Consistency
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
As part of making
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</span>
</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">
findAndModify()
</code>
</a>
projection consistent with
aggregation's
<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>
<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.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</span>
</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">
findAndModify()
</code>
</a>
projection can accept
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
aggregation expressions and syntax
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
MongoDB enforces additional restrictions with regards to
projections. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/limits/#mongodb-limit-Projection-Restrictions">
Projection Restrictions
</a>
for details.
</p>
</li>
</ul>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
projection
</code>
parameter determines which fields are returned in
the matching documents. The
<code class="leafygreen-ui-1l06pbn">
projection
</code>
parameter takes a document
of the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ <field1>: <value>, <field2>: <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">{ <<!-- -->field1<!-- -->><!-- -->: <<!-- -->value<!-- -->><!-- -->,<!-- --> <!-- --><<!-- -->field2<!-- -->><!-- -->: <<!-- -->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>
<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>
Projection
</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">
<field>: <1 or true>
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specifies the inclusion of a field. If you specify a non-zero
integer for the projection value, the operation treats the
value as
<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">
<field>: <0 or false>
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specifies the exclusion of a field.
</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">
"<field>.$": <1 or true>
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/projection/positional/#mongodb-projection-proj.-">
<code class="leafygreen-ui-1l06pbn">
$
</code>
</a>
array projection operator to return
the first element that matches the query condition on the
array field. If you specify a non-zero integer for the
projection value, the operation treats the value as
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
Not available for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/views/#std-label-views-landing-page">
views
<!-- -->
.
</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">
<field>: <array projection>
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Uses the array projection operators (
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/projection/elemMatch/#mongodb-projection-proj.-elemMatch">
<code class="leafygreen-ui-1l06pbn">
$elemMatch
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/projection/slice/#mongodb-projection-proj.-slice">
<code class="leafygreen-ui-1l06pbn">
$slice
</code>
</a>
) to specify the array elements to
include.
</p>
<p class="leafygreen-ui-1kp3ins">
Not available for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/views/#std-label-views-landing-page">
views
<!-- -->
.
</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">
<field>: <$meta expression>
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Uses 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>
operator expression to specify
the inclusion of available
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/meta/#mongodb-expression-exp.-meta">
<code class="leafygreen-ui-1l06pbn">
per-document metadata
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Not available for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/views/#std-label-views-landing-page">
views
<!-- -->
.
</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">
<field>: <aggregation expression>
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Specifies the value of the projected field.
</p>
<p class="leafygreen-ui-1kp3ins">
With the use of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
aggregation expressions and syntax
</a>
, including the use of literals and
aggregation variables, you can project new fields or project existing
fields with new values.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If you specify a non-numeric, non-boolean literal (such as a
literal string or an array or an operator expression) for
the projection value, the field is projected with the new
value, for example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{ field: [ 1, 2, 3, "$someExistingField" ] }
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{ field: "New String Value" }
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{ field: { status: "Active", total: { $sum: "$existingArray" } } }
</code>
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To project a literal value for a field, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/literal/#mongodb-expression-exp.-literal">
<code class="leafygreen-ui-1l06pbn">
$literal
</code>
</a>
aggregation expression; for example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{ field: { $literal: 5 } }
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{ field: { $literal: true } }
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
{ field: { $literal: { fieldWithValue0: 0, fieldWithValue1: 1 } } }
</code>
</p>
</li>
</ul>
</li>
</ul>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-find-options">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Options
<a class="headerlink leafygreen-ui-1kepx47" href="#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="options">
</div>
</a>
</h3>
<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>
Description
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
allowDiskUse
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Whether or not pipelines that require more than 100 megabytes of
memory to execute write to temporary files on disk. For details,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.allowDiskUse/#mongodb-method-cursor.allowDiskUse">
<code class="leafygreen-ui-1l06pbn">
cursor.allowDiskUse()
</code>
.
</a>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
allowPartialResults
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
For queries against a sharded collection, allows the command
(or subsequent getMore commands) to return partial results,
rather than an error, if one or more queried shards are
unavailable.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
awaitData
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
If the cursor is a a tailable-await cursor.
Requires
<code class="leafygreen-ui-1l06pbn">
tailable
</code>
to be
<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>
collation
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Collation settings for update operation.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
comment
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Adds a
<code class="leafygreen-ui-1l06pbn">
$comment
</code>
to the query that shows in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/database-profiler/#std-label-profiler">
profiler
</a>
logs.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
explain
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Adds explain output based on the verbosity mode provided.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
hint
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Forces the query optimizer to use specific indexes in the
query.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
limit
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Sets a limit of documents returned in the result set.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
max
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The exclusive upper bound for a specific index.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
maxAwaitTimeMS
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The maximum amount of time for the server to wait on
new documents to satisfy a tailable cursor query. Requires
<code class="leafygreen-ui-1l06pbn">
tailable
</code>
and
<code class="leafygreen-ui-1l06pbn">
awaitData
</code>
to be
<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>
maxTimeMS
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The maximum amount of time (in milliseconds) the
server should allow the query to run.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
min
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The inclusive lower bound for a specific index.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
noCursorTimeout
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Whether the server should timeout the cursor
after a period of inactivity (by default 10 minutes).
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
readConcern
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specifies the read concern level for the query.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
readPreference
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Specifies the read preference level for the query.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
returnKey
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Whether only the index keys are returned for a
query.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
showRecordId
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
If the
<code class="leafygreen-ui-1l06pbn">
$recordId
</code>
field is added to the returned
documents. The
<code class="leafygreen-ui-1l06pbn">
$recordId
</code>
indicates the position of the
document in the result set.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
skip
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
How many documents to skip before returning the
first document in the result set.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
sort
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The order of the documents returned in the result
set. Fields specified in the sort, must have an index.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
tailable
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Indicates if the cursor is tailable. Tailable cursors remain
open after the intial results of the query are exhausted.
Tailable cursors are only available on
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/capped-collections/#std-label-manual-capped-collection">
Capped Collections
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Embedded Field Specification
<a class="headerlink leafygreen-ui-1kepx47" href="#embedded-field-specification" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="embedded-field-specification">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
For fields in an embedded documents, you can specify the field using
either:
</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/document/#std-label-document-dot-notation-embedded-fields">
dot notation
</a>
, for example
<code class="leafygreen-ui-1l06pbn">
"field.nestedfield": <value>
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
nested form, for example
<code class="leafygreen-ui-1l06pbn">
{ field: { nestedfield: <value> } }
</code>
</p>
</li>
</ul>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
<code class="leafygreen-ui-1l06pbn">
_id
</code>
Field Projection
<a class="headerlink leafygreen-ui-1kepx47" href="#_id-field-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="_id-field-projection">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field is included in the returned documents by default unless
you explicitly specify
<code class="leafygreen-ui-1l06pbn">
_id: 0
</code>
in the projection to suppress the field.
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Inclusion or Exclusion
<a class="headerlink leafygreen-ui-1kepx47" href="#inclusion-or-exclusion" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="inclusion-or-exclusion">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
A
<code class="leafygreen-ui-1l06pbn">
projection
</code>
<em>
cannot
</em>
contain
<em>
both
</em>
include and exclude
specifications, with the exception of the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
In projections that
<em>
explicitly include
</em>
fields, the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field is
the only field that you can
<em>
explicitly exclude
</em>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
In projections that
<em>
explicitly excludes
</em>
fields, the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field
is the only field that you can
<em>
explicitly include
</em>
; however, the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field is included by default.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-find-projection-examples" target="_self">
<span>
Projection Examples
<!-- -->
.
</span>
</a>
</p>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Cursor Handling
<a class="headerlink leafygreen-ui-1kepx47" href="#cursor-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="cursor-handling">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Executing
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</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>
automatically iterates the cursor to display up to the first 20
documents. Type
<code class="leafygreen-ui-1l06pbn">
it
</code>
to continue iteration.
</p>
<p class="leafygreen-ui-1kp3ins">
To access the returned documents with a driver, use the appropriate
cursor handling mechanism for the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
driver language
<!-- -->
.
</span>
</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="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-crud-read-cursor" target="_self">
<span>
Iterate the Returned Cursor
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-find-modify-cursor" target="_self">
<span>
Modify the Cursor Behavior
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-find-cursor-methods" target="_self">
<span>
Available
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
Cursor Methods
</span>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Read Concern
<a class="headerlink leafygreen-ui-1kepx47" href="#read-concern" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-concern">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To specify the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern/#std-label-read-concern">
read concern
</a>
for
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</span>
</a>
, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.readConcern/#mongodb-method-cursor.readConcern">
<code class="leafygreen-ui-1l06pbn">
cursor.readConcern()
</code>
</a>
method.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-type-bracketing">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Type Bracketing
<a class="headerlink leafygreen-ui-1kepx47" href="#type-bracketing" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-bracketing">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB treats some data types as equivalent for comparison purposes.
For instance, numeric types undergo conversion before comparison. For
most data types, however,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query-comparison/#std-label-query-selectors-comparison">
comparison operators
</a>
only
perform comparisons on documents where the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-type-comparison-order/#std-label-bson-types-comparison-order">
BSON type
</a>
of the
target field matches the type of the query operand. Consider the
following collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\": \"apples\", \"qty\": 5 }\n{ \"_id\": \"bananas\", \"qty\": 7 }\n{ \"_id\": \"oranges\", \"qty\": { \"in stock\": 8, \"ordered\": 12 } }\n{ \"_id\": \"avocados\", \"qty\": \"fourteen\" }","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-string">"apples"</span>, <span class="lg-highlight-string">"qty"</span>: <span class="lg-highlight-number">5</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span>: <span class="lg-highlight-string">"bananas"</span>, <span class="lg-highlight-string">"qty"</span>: <span class="lg-highlight-number">7</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span>: <span class="lg-highlight-string">"oranges"</span>, <span class="lg-highlight-string">"qty"</span>: <!-- -->{ <span class="lg-highlight-string">"in stock"</span>: <span class="lg-highlight-number">8</span>, <span class="lg-highlight-string">"ordered"</span>: <span class="lg-highlight-number">12</span> } }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span>: <span class="lg-highlight-string">"avocados"</span>, <span class="lg-highlight-string">"qty"</span>: <span class="lg-highlight-string">"fourteen"</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 query uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/gt/#mongodb-query-op.-gt">
<code class="leafygreen-ui-1l06pbn">
$gt
</code>
</a>
to return documents where the
value of
<code class="leafygreen-ui-1l06pbn">
qty
</code>
is greater than
<code class="leafygreen-ui-1l06pbn">
4
</code>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.collection.find( { qty: { $gt: 4 } } )","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-attr">qty</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">4</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 returns the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\": \"apples\", \"qty\": 5 }\n{ \"_id\": \"bananas\", \"qty\": 7 }","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">"apples"</span>, <span class="lg-highlight-string">"qty"</span>: <span class="lg-highlight-number">5</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span>: <span class="lg-highlight-string">"bananas"</span>, <span class="lg-highlight-string">"qty"</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>
<p class="leafygreen-ui-1kp3ins">
The document with
<code class="leafygreen-ui-1l06pbn">
_id
</code>
equal to
<code class="leafygreen-ui-1l06pbn">
"avocados"
</code>
is not
returned because its
<code class="leafygreen-ui-1l06pbn">
qty
</code>
value is of type
<code class="leafygreen-ui-1l06pbn">
string
</code>
while the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/gt/#mongodb-query-op.-gt">
<code class="leafygreen-ui-1l06pbn">
$gt
</code>
</a>
operand is of type
<code class="leafygreen-ui-1l06pbn">
integer
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
The document with
<code class="leafygreen-ui-1l06pbn">
_id
</code>
equal to
<code class="leafygreen-ui-1l06pbn">
"oranges"
</code>
is not returned
because its
<code class="leafygreen-ui-1l06pbn">
qty
</code>
value is of type
<code class="leafygreen-ui-1l06pbn">
object
</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">
To enforce data types in a collection, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/schema-validation/">
Schema Validation
<!-- -->
.
</a>
</p>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Sessions
<a class="headerlink leafygreen-ui-1kepx47" href="#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="sessions">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
For cursors created inside a session, you cannot call
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
outside the session.
</p>
<p class="leafygreen-ui-1kp3ins">
Similarly, for cursors created outside of a session, you cannot call
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
inside a session.
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Session Idle Timeout
<a class="headerlink leafygreen-ui-1kepx47" href="#session-idle-timeout" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-idle-timeout">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
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>
associate all operations with a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/server-sessions/">
server session
</a>
, with the exception of unacknowledged
write operations. For operations not explicitly associated with a
session (i.e. using
<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>
), MongoDB drivers
and
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
create an implicit session and associate it
with the operation.
</p>
<p class="leafygreen-ui-1kp3ins">
If a session is idle for longer than 30 minutes, the MongoDB server
marks that session as expired and may close it at any time. When the
MongoDB server closes the session, it also kills any in-progress
operations and open cursors associated with the session. This
includes cursors configured with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.noCursorTimeout/#mongodb-method-cursor.noCursorTimeout">
<code class="leafygreen-ui-1l06pbn">
noCursorTimeout()
</code>
</a>
or
a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.maxTimeMS/#mongodb-method-cursor.maxTimeMS">
<code class="leafygreen-ui-1l06pbn">
maxTimeMS()
</code>
</a>
greater than 30 minutes.
</p>
<p class="leafygreen-ui-1kp3ins">
For operations that may be idle for longer than 30 minutes, associate
the operation with an explicit session using
<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>
and periodically refresh the session
using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/refreshSessions/#mongodb-dbcommand-dbcmd.refreshSessions">
<code class="leafygreen-ui-1l06pbn">
refreshSessions
</code>
</a>
command. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/limits/#mongodb-limit-Session-Idle-Timeout">
Session Idle Timeout
</a>
for more information.
</p>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Transactions
<a class="headerlink leafygreen-ui-1kepx47" href="#transactions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="transactions">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</span>
</a>
can be used inside
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
distributed transactions
<!-- -->
.
</a>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For cursors created outside of a transaction, you cannot call
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
inside the transaction.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For cursors created in a transaction, you cannot call
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/getMore/#mongodb-dbcommand-dbcmd.getMore">
<code class="leafygreen-ui-1l06pbn">
getMore
</code>
</a>
outside the transaction.
</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">
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>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Client Disconnection
<a class="headerlink leafygreen-ui-1kepx47" href="#client-disconnection" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-disconnection">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 4.2, if the client that issued
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</span>
</a>
disconnects before the operation completes, MongoDB marks
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</span>
</a>
for termination using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/killOp/#mongodb-dbcommand-dbcmd.killOp">
<code class="leafygreen-ui-1l06pbn">
killOp
</code>
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Query Settings
<a class="headerlink leafygreen-ui-1kepx47" href="#query-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="query-settings">
</div>
</a>
</h3>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
8.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
You can use query settings to set index hints, set
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/operation-rejection-filters/#std-label-operation-rejection-filters">
operation
rejection filters
</a>
, and other fields. The
settings apply to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/queryStats/#std-label-queryStats-queryShape">
query shape
</a>
on the
entire cluster. The cluster retains the settings after shutdown.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-query-optimizer">
query optimizer
</a>
uses the query settings as an additional
input during query planning, which affects the plan selected to run the
query. You can also use query settings to block a query shape.
</p>
<p class="leafygreen-ui-1kp3ins">
To add query settings and explore examples, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setQuerySettings/#mongodb-dbcommand-dbcmd.setQuerySettings">
<code class="leafygreen-ui-1l06pbn">
setQuerySettings
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
You can add query settings for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/find/#mongodb-dbcommand-dbcmd.find">
<code class="leafygreen-ui-1l06pbn">
find
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/distinct/#mongodb-dbcommand-dbcmd.distinct">
<code class="leafygreen-ui-1l06pbn">
distinct
</code>
</a>
,
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#mongodb-dbcommand-dbcmd.aggregate">
<code class="leafygreen-ui-1l06pbn">
aggregate
</code>
</a>
commands.
</p>
<p class="leafygreen-ui-1kp3ins">
Query settings have more functionality and are preferred over
deprecated
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-plans/#std-label-index-filters">
index filters
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To remove query settings, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/removeQuerySettings/#mongodb-dbcommand-dbcmd.removeQuerySettings">
<code class="leafygreen-ui-1l06pbn">
removeQuerySettings
</code>
</a>
. To
obtain the query settings, use a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/querySettings/#mongodb-pipeline-pipe.-querySettings">
<code class="leafygreen-ui-1l06pbn">
$querySettings
</code>
</a>
stage in an
aggregation pipeline.
</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 examples in this section use documents from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/#std-label-bios-example-collection">
bios
collection
</a>
where the documents
generally have the form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : <value>,\n \"name\" : { \"first\" : <string>, \"last\" : <string> }, // embedded document\n \"birth\" : <isodate>,\n \"death\" : <isodate>,\n \"contribs\" : [ <string>, ... ], // Array of Strings\n \"awards\" : [\n { \"award\" : <string>, year: <number>, by: <string> } // Array of embedded documents\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-string">"_id"</span> <!-- -->: <<!-- -->value<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"first"</span> <!-- -->: <<!-- -->string<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-string">"last"</span> <!-- -->: <<!-- -->string<!-- -->> <!-- -->}<!-- -->, <span class="lg-highlight-comment">// embedded document</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"birth"</span> <!-- -->: <<span class="lg-highlight-title lg-highlight-class">ISODate</span>><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"death"</span> <!-- -->: <<span class="lg-highlight-title lg-highlight-class">ISODate</span>><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"contribs"</span> <!-- -->: <!-- -->[ <<!-- -->string<!-- -->><!-- -->,<!-- --> <!-- -->...<!-- --> ]<!-- -->, <span class="lg-highlight-comment">// Array of Strings</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"awards"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"award"</span> <!-- -->: <<!-- -->string<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">year</span>: <<!-- -->number<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">by</span>: <<!-- -->string<!-- -->> <!-- -->} <span class="lg-highlight-comment">// Array of embedded documents</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">
To create and populate the
<code class="leafygreen-ui-1l06pbn">
bios
</code>
collection, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/#std-label-bios-example-collection">
bios collection
<!-- -->
.
</a>
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Find All Documents in a Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#find-all-documents-in-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="find-all-documents-in-a-collection">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</span>
</a>
method with no parameters
returns all documents from a collection and returns all fields for the
documents. For example, the following operation returns all documents in
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/#std-label-bios-example-collection">
bios collection
<!-- -->
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find()","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">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</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">
Find Documents that Match Query Criteria
<a class="headerlink leafygreen-ui-1kepx47" href="#find-documents-that-match-query-criteria" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-documents-that-match-query-criteria">
</div>
</a>
</h3>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Query for Equality
<a class="headerlink leafygreen-ui-1kepx47" href="#query-for-equality" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-equality">
</div>
</a>
</h4>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The following operation returns documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios
collection
</a>
where
<code class="leafygreen-ui-1l06pbn">
_id
</code>
equals
<code class="leafygreen-ui-1l06pbn">
5
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find( { _id: 5 } )","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">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The following operation returns documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios
collection
</a>
where the field
<code class="leafygreen-ui-1l06pbn">
last
</code>
in the
<code class="leafygreen-ui-1l06pbn">
name
</code>
embedded document equals
<code class="leafygreen-ui-1l06pbn">
"Hopper"
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find( { \"name.last\": \"Hopper\" } )","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">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-string">"name.last"</span>: <span class="lg-highlight-string">"Hopper"</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">
To access fields in an embedded document, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/document/#std-label-document-dot-notation-embedded-fields">
dot notation
</a>
(
<code class="leafygreen-ui-1l06pbn">
"<embedded
document>.<field>"
</code>
).
</p>
</div>
</div>
</li>
</ul>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Query Using Operators
<a class="headerlink leafygreen-ui-1kepx47" href="#query-using-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="query-using-operators">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
To find documents that match a set of selection criteria, call
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</span>
</a>
with the
<code class="leafygreen-ui-1l06pbn">
<criteria>
</code>
parameter.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB provides various
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/#std-label-query-selectors">
query operators
</a>
to
specify the criteria.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The following operation uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/in/#mongodb-query-op.-in">
<code class="leafygreen-ui-1l06pbn">
$in
</code>
</a>
operator to return
documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios collection
</a>
where
<code class="leafygreen-ui-1l06pbn">
_id
</code>
equals either
<code class="leafygreen-ui-1l06pbn">
5
</code>
or
<code class="leafygreen-ui-1l06pbn">
ObjectId("507c35dd8fada716c89d0013")
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find(\n { _id: { $in: [ 5, ObjectId(\"507c35dd8fada716c89d0013\") ] } }\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">bios</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">_id</span>: <!-- -->{ <span class="lg-highlight-attr">$in</span>: <!-- -->[ <span class="lg-highlight-number">5</span>, <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"507c35dd8fada716c89d0013"</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">
The following operation uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/gt/#mongodb-query-op.-gt">
<code class="leafygreen-ui-1l06pbn">
$gt
</code>
</a>
operator returns all
the documents from the
<code class="leafygreen-ui-1l06pbn">
bios
</code>
collection where
<code class="leafygreen-ui-1l06pbn">
birth
</code>
is
greater than
<code class="leafygreen-ui-1l06pbn">
new Date('1950-01-01')
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find( { birth: { $gt: new Date('1950-01-01') } } )","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">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">birth</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">'1950-01-01'</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">
The following operation uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/regex/#mongodb-query-op.-regex">
<code class="leafygreen-ui-1l06pbn">
$regex
</code>
</a>
operator to return
documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios collection
</a>
where
<code class="leafygreen-ui-1l06pbn">
name.last
</code>
field
starts with the letter
<code class="leafygreen-ui-1l06pbn">
N
</code>
(or is
<code class="leafygreen-ui-1l06pbn">
"LIKE N%"
</code>
)
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find(\n { \"name.last\": { $regex: /^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">bios</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">"name.last"</span>: <!-- -->{ <span class="lg-highlight-attr">$regex</span>: <span class="lg-highlight-regexp">/^N/</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 a list of the query operators, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/#std-label-query-selectors">
Query Selectors
<!-- -->
.
</a>
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Query for Ranges
<a class="headerlink leafygreen-ui-1kepx47" href="#query-for-ranges" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-ranges">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Combine comparison operators to specify ranges for a field. The
following operation returns from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios collection
</a>
documents where
<code class="leafygreen-ui-1l06pbn">
birth
</code>
is
between
<code class="leafygreen-ui-1l06pbn">
new Date('1940-01-01')
</code>
and
<code class="leafygreen-ui-1l06pbn">
new Date('1960-01-01')
</code>
(exclusive):
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find( { birth: { $gt: new Date('1940-01-01'), $lt: new Date('1960-01-01') } } )","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">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">birth</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">'1940-01-01'</span>)<!-- -->, <span class="lg-highlight-attr">$lt</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">'1960-01-01'</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 the query operators, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/#std-label-query-selectors">
Query Selectors
<!-- -->
.
</a>
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Query for Multiple Conditions
<a class="headerlink leafygreen-ui-1kepx47" href="#query-for-multiple-conditions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-multiple-conditions">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following operation returns all the documents from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios
collection
</a>
where
<code class="leafygreen-ui-1l06pbn">
birth
</code>
field
is
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/gt/#mongodb-query-op.-gt">
<code class="leafygreen-ui-1l06pbn">
greater than
</code>
</a>
<code class="leafygreen-ui-1l06pbn">
new Date('1950-01-01')
</code>
and
<code class="leafygreen-ui-1l06pbn">
death
</code>
field does not exists:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find( {\n birth: { $gt: new Date('1920-01-01') },\n death: { $exists: 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-property">bios</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">birth</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">'1920-01-01'</span>) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">death</span>: <!-- -->{ <span class="lg-highlight-attr">$exists</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">
For a list of the query operators, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/#std-label-query-selectors">
Query Selectors
<!-- -->
.
</a>
</p>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Compare Two Fields from A Single Document
<a class="headerlink leafygreen-ui-1kepx47" href="#compare-two-fields-from-a-single-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="compare-two-fields-from-a-single-document">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
can contain expressions that compare fields from the same
document.
</p>
<p class="leafygreen-ui-1kp3ins">
Create a
<code class="leafygreen-ui-1l06pbn">
monthlyBudget
</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.monthlyBudget.insertMany( [\n { _id : 1, category : \"food\", budget : 400, spent : 450 },\n { _id : 2, category : \"drinks\", budget : 100, spent : 150 },\n { _id : 3, category : \"clothes\", budget : 100, spent : 50 },\n { _id : 4, category : \"misc\", budget : 500, spent : 300 },\n { _id : 5, category : \"travel\", budget : 200, spent : 650 }\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">monthlyBudget</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>, <!-- -->category <!-- -->: <span class="lg-highlight-string">"food"</span>, <!-- -->budget <!-- -->: <span class="lg-highlight-number">400</span>, <!-- -->spent <!-- -->: <span class="lg-highlight-number">450</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">2</span>, <!-- -->category <!-- -->: <span class="lg-highlight-string">"drinks"</span>, <!-- -->budget <!-- -->: <span class="lg-highlight-number">100</span>, <!-- -->spent <!-- -->: <span class="lg-highlight-number">150</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">3</span>, <!-- -->category <!-- -->: <span class="lg-highlight-string">"clothes"</span>, <!-- -->budget <!-- -->: <span class="lg-highlight-number">100</span>, <!-- -->spent <!-- -->: <span class="lg-highlight-number">50</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">4</span>, <!-- -->category <!-- -->: <span class="lg-highlight-string">"misc"</span>, <!-- -->budget <!-- -->: <span class="lg-highlight-number">500</span>, <!-- -->spent <!-- -->: <span class="lg-highlight-number">300</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">5</span>, <!-- -->category <!-- -->: <span class="lg-highlight-string">"travel"</span>, <!-- -->budget <!-- -->: <span class="lg-highlight-number">200</span>, <!-- -->spent <!-- -->: <span class="lg-highlight-number">650</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
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
to find documents
where the
<code class="leafygreen-ui-1l06pbn">
spent
</code>
amount exceeds the
<code class="leafygreen-ui-1l06pbn">
budget
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.monthlyBudget.find( { $expr: { $gt: [ \"$spent\" , \"$budget\" ] } } )","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">monthlyBudget</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">$expr</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <!-- -->[ <span class="lg-highlight-string">"$spent"</span> <!-- -->, <span class="lg-highlight-string">"$budget"</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">
Output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ _id : 1, category : \"food\", budget : 400, spent : 450 }\n{ _id : 2, category : \"drinks\", budget : 100, spent : 150 }\n{ _id : 5, category : \"travel\", budget : 200, spent : 650 }","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">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">1</span>, <!-- -->category <!-- -->: <span class="lg-highlight-string">"food"</span>, <!-- -->budget <!-- -->: <span class="lg-highlight-number">400</span>, <!-- -->spent <!-- -->: <span class="lg-highlight-number">450</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">2</span>, <!-- -->category <!-- -->: <span class="lg-highlight-string">"drinks"</span>, <!-- -->budget <!-- -->: <span class="lg-highlight-number">100</span>, <!-- -->spent <!-- -->: <span class="lg-highlight-number">150</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <!-- -->_id <!-- -->: <span class="lg-highlight-number">5</span>, <!-- -->category <!-- -->: <span class="lg-highlight-string">"travel"</span>, <!-- -->budget <!-- -->: <span class="lg-highlight-number">200</span>, <!-- -->spent <!-- -->: <span class="lg-highlight-number">650</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-query-subdocuments">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-query-embedded-documents">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Query Embedded Documents
<a class="headerlink leafygreen-ui-1kepx47" href="#query-embedded-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="query-embedded-documents">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following examples query the
<code class="leafygreen-ui-1l06pbn">
name
</code>
embedded field in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/#std-label-bios-example-collection">
bios collection
<!-- -->
.
</a>
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Query Exact Matches on Embedded Documents
<a class="headerlink leafygreen-ui-1kepx47" href="#query-exact-matches-on-embedded-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="query-exact-matches-on-embedded-documents">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following operation returns documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios collection
</a>
where the embedded document
<code class="leafygreen-ui-1l06pbn">
name
</code>
is
<em>
exactly
</em>
<code class="leafygreen-ui-1l06pbn">
{ first: "Yukihiro", last: "Matsumoto" }
</code>
, including the
order:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find(\n { name: { first: \"Yukihiro\", last: \"Matsumoto\" } }\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">bios</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">name</span>: <!-- -->{ <span class="lg-highlight-attr">first</span>: <span class="lg-highlight-string">"Yukihiro"</span>, <span class="lg-highlight-attr">last</span>: <span class="lg-highlight-string">"Matsumoto"</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">
name
</code>
field must match the embedded document exactly. The query does
<strong>
not
</strong>
match documents with the following
<code class="leafygreen-ui-1l06pbn">
name
</code>
fields:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n first: \"Yukihiro\",\n aka: \"Matz\",\n last: \"Matsumoto\"\n}\n\n{\n last: \"Matsumoto\",\n first: \"Yukihiro\"\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">first</span>: <span class="lg-highlight-string">"Yukihiro"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">aka</span>: <span class="lg-highlight-string">"Matz"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">last</span>: <span class="lg-highlight-string">"Matsumoto"</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">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">last</span>: <span class="lg-highlight-string">"Matsumoto"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">first</span>: <span class="lg-highlight-string">"Yukihiro"</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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Query Fields of an Embedded Document
<a class="headerlink leafygreen-ui-1kepx47" href="#query-fields-of-an-embedded-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="query-fields-of-an-embedded-document">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following operation returns documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios collection
</a>
where the embedded document
<code class="leafygreen-ui-1l06pbn">
name
</code>
contains a field
<code class="leafygreen-ui-1l06pbn">
first
</code>
with the value
<code class="leafygreen-ui-1l06pbn">
"Yukihiro"
</code>
and a field
<code class="leafygreen-ui-1l06pbn">
last
</code>
with the value
<code class="leafygreen-ui-1l06pbn">
"Matsumoto"
</code>
. The query uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-dot-notation">
dot notation
</a>
to access fields in an embedded document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find(\n {\n \"name.first\": \"Yukihiro\",\n \"name.last\": \"Matsumoto\"\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">bios</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-string">"name.first"</span>: <span class="lg-highlight-string">"Yukihiro"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name.last"</span>: <span class="lg-highlight-string">"Matsumoto"</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 matches the document where the
<code class="leafygreen-ui-1l06pbn">
name
</code>
field contains an
embedded document with the field
<code class="leafygreen-ui-1l06pbn">
first
</code>
with the value
<code class="leafygreen-ui-1l06pbn">
"Yukihiro"
</code>
and a
field
<code class="leafygreen-ui-1l06pbn">
last
</code>
with the value
<code class="leafygreen-ui-1l06pbn">
"Matsumoto"
</code>
. For instance, the query
would match documents with
<code class="leafygreen-ui-1l06pbn">
name
</code>
fields that held either of the
following values:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n first: \"Yukihiro\",\n aka: \"Matz\",\n last: \"Matsumoto\"\n}\n\n{\n last: \"Matsumoto\",\n first: \"Yukihiro\"\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">first</span>: <span class="lg-highlight-string">"Yukihiro"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">aka</span>: <span class="lg-highlight-string">"Matz"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">last</span>: <span class="lg-highlight-string">"Matsumoto"</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">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">last</span>: <span class="lg-highlight-string">"Matsumoto"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">first</span>: <span class="lg-highlight-string">"Yukihiro"</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">
For more information and examples, see also
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/query-embedded-documents/">
Query on Embedded/Nested Documents
<!-- -->
.
</a>
</p>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Query Arrays
<a class="headerlink leafygreen-ui-1kepx47" href="#query-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="query-arrays">
</div>
</a>
</h3>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Query for an Array Element
<a class="headerlink leafygreen-ui-1kepx47" href="#query-for-an-array-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="query-for-an-array-element">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following examples query the
<code class="leafygreen-ui-1l06pbn">
contribs
</code>
array in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios
collection
<!-- -->
.
</a>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The following operation returns documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios
collection
</a>
where the array
field
<code class="leafygreen-ui-1l06pbn">
contribs
</code>
contains the element
<code class="leafygreen-ui-1l06pbn">
"UNIX"
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find( { contribs: \"UNIX\" } )","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">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">contribs</span>: <span class="lg-highlight-string">"UNIX"</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">
The following operation returns documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios
collection
</a>
where the array
field
<code class="leafygreen-ui-1l06pbn">
contribs
</code>
contains the element
<code class="leafygreen-ui-1l06pbn">
"ALGOL"
</code>
or
<code class="leafygreen-ui-1l06pbn">
"Lisp"
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find( { contribs: { $in: [ \"ALGOL\", \"Lisp\" ]} } )","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">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">contribs</span>: <!-- -->{ <span class="lg-highlight-attr">$in</span>: <!-- -->[ <span class="lg-highlight-string">"ALGOL"</span>, <span class="lg-highlight-string">"Lisp"</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">
The following operation use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/all/#mongodb-query-op.-all">
<code class="leafygreen-ui-1l06pbn">
$all
</code>
</a>
query operator to
return documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios collection
</a>
where the array field
<code class="leafygreen-ui-1l06pbn">
contribs
</code>
contains both the elements
<code class="leafygreen-ui-1l06pbn">
"ALGOL"
</code>
and
<code class="leafygreen-ui-1l06pbn">
"Lisp"
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find( { contribs: { $all: [ \"ALGOL\", \"Lisp\" ] } } )","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">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">contribs</span>: <!-- -->{ <span class="lg-highlight-attr">$all</span>: <!-- -->[ <span class="lg-highlight-string">"ALGOL"</span>, <span class="lg-highlight-string">"Lisp"</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="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/all/#mongodb-query-op.-all">
<code class="leafygreen-ui-1l06pbn">
$all
</code>
</a>
. See also
<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>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The following operation uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/size/#mongodb-query-op.-size">
<code class="leafygreen-ui-1l06pbn">
$size
</code>
</a>
operator to return
documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios collection
</a>
where the array size
of
<code class="leafygreen-ui-1l06pbn">
contribs
</code>
is 4:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find( { contribs: { $size: 4 } } )","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">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">contribs</span>: <!-- -->{ <span class="lg-highlight-attr">$size</span>: <span class="lg-highlight-number">4</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>
<p class="leafygreen-ui-1kp3ins">
For more information and examples of querying an array, 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/">
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/">
Query an Array of Embedded Documents
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For a list of array specific query operators, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/#std-label-operator-query-array">
Array
<!-- -->
.
</a>
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Query an Array of Documents
<a class="headerlink leafygreen-ui-1kepx47" href="#query-an-array-of-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="query-an-array-of-documents">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following examples query the
<code class="leafygreen-ui-1l06pbn">
awards
</code>
array in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios
collection
<!-- -->
.
</a>
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The following operation returns documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios
collection
</a>
where the
<code class="leafygreen-ui-1l06pbn">
awards
</code>
array contains an element with
<code class="leafygreen-ui-1l06pbn">
award
</code>
field equals
<code class="leafygreen-ui-1l06pbn">
"Turing Award"
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find(\n { \"awards.award\": \"Turing Award\" }\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">bios</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">"awards.award"</span>: <span class="lg-highlight-string">"Turing Award"</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">
The following operation returns documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios
collection
</a>
where the
<code class="leafygreen-ui-1l06pbn">
awards
</code>
array contains at least one element with both the
<code class="leafygreen-ui-1l06pbn">
award
</code>
field
equals
<code class="leafygreen-ui-1l06pbn">
"Turing Award"
</code>
and the
<code class="leafygreen-ui-1l06pbn">
year
</code>
field greater than 1980:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find(\n { awards: { $elemMatch: { award: \"Turing Award\", year: { $gt: 1980 } } } }\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">bios</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">awards</span>: <!-- -->{ <span class="lg-highlight-attr">$elemMatch</span>: <!-- -->{ <span class="lg-highlight-attr">award</span>: <span class="lg-highlight-string">"Turing Award"</span>, <span class="lg-highlight-attr">year</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-number">1980</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
<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 to specify multiple criteria on
an array element.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For more information and examples of querying an array, 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/">
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/">
Query an Array of Embedded Documents
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For a list of array specific query operators, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/#std-label-operator-query-array">
Array
<!-- -->
.
</a>
</p>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Query for BSON Regular Expressions
<a class="headerlink leafygreen-ui-1kepx47" href="#query-for-bson-regular-expressions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-bson-regular-expressions">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To find documents that contain BSON regular expressions as values, call
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</span>
</a>
with the
<code class="leafygreen-ui-1l06pbn">
bsonRegExp
</code>
option set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
. The
<code class="leafygreen-ui-1l06pbn">
bsonRegExp
</code>
option allows you to return regular
expressions that can't be represented as JavaScript regular expressions.
</p>
<p class="leafygreen-ui-1kp3ins">
The following operation returns documents in a collection named
<code class="leafygreen-ui-1l06pbn">
testbson
</code>
where the value of a field named
<code class="leafygreen-ui-1l06pbn">
foo
</code>
is a
<code class="leafygreen-ui-1l06pbn">
BSONRegExp
</code>
type:
</p>
<div class="css-1f5771r">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.testbson.find( {}, {}, { bsonRegExp: 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">db.<span class="lg-highlight-property">testbson</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{<!-- -->}<!-- -->, <!-- -->{<!-- -->}<!-- -->, <!-- -->{ <span class="lg-highlight-attr">bsonRegExp</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="css-9st569 efa38qy0" style="--border-color:#E8EDEB">
<button aria-disabled="false" class="lg-ui-button-0000 leafygreen-ui-hix6y6" data-lgid="lg-button" role="button" type="button">
<div class="leafygreen-ui-v038xi">
</div>
<div class="leafygreen-ui-16tr4y">
<svg alt="" aria-hidden="true" class="leafygreen-ui-hjnzs9" fill="none" height="16" role="presentation" viewbox="0 0 16 16" width="16" xmlns="http://www.w3.org/2000/svg">
<path clip-rule="evenodd" d="M14.364 10.778a1 1 0 0 0 0-1.414l-4.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 7.243l4.243 4.242a1 1 0 0 0 1.414 0l.707-.707Z" fill="currentColor" fill-rule="evenodd">
</path>
</svg>
HIDE OUTPUT
</div>
</button>
</div>
<div class="leafygreen-ui-1oteowz">
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: ObjectId('65e8ba8a4b3c33a76e6cacca'),\n foo: BSONRegExp('(?-i)AA_', 'i')\n }\n]","programmingLanguage":"JavaScript"}
</script>
<div class="css-1sax7hu e11q6jfk0" style="--code-container-border:initial;--code-pre-border:none">
<div class="leafygreen-ui-1fyvhqe">
<div class="leafygreen-ui-1jheeh3">
<pre class="leafygreen-ui-12cejcz" tabindex="-1"><code class="lg-highlight-hljs-dark 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">'65e8ba8a4b3c33a76e6cacca'</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">foo</span>: <span class="lg-highlight-title lg-highlight-class">BSONRegExp</span>(<span class="lg-highlight-string">'(?-i)AA_'</span>, <span class="lg-highlight-string">'i'</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-find-projection-examples">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Projections
<a class="headerlink leafygreen-ui-1kepx47" href="#projections" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="projections">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-find-projection" target="_self">
<span>
projection
</span>
</a>
parameter specifies which fields
to return. The parameter contains either include or exclude
specifications, not both, unless the exclude is for the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field.
</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">
Unless the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field is explicitly excluded in the projection
document
<code class="leafygreen-ui-1l06pbn">
_id: 0
</code>
, the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field is returned.
</p>
</div>
</div>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Specify the Fields to Return
<a class="headerlink leafygreen-ui-1kepx47" href="#specify-the-fields-to-return" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-the-fields-to-return">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following operation finds all documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios collection
</a>
and returns only the
<code class="leafygreen-ui-1l06pbn">
name
</code>
field,
<code class="leafygreen-ui-1l06pbn">
contribs
</code>
field and
<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":"db.bios.find( { }, { name: 1, contribs: 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">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <!-- -->}<!-- -->, <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">contribs</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 class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Unless the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field is explicitly excluded in the projection
document
<code class="leafygreen-ui-1l06pbn">
_id: 0
</code>
, the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field is returned.
</p>
</div>
</div>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Explicitly Excluded Fields
<a class="headerlink leafygreen-ui-1kepx47" href="#explicitly-excluded-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="explicitly-excluded-fields">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following operation queries the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios collection
</a>
and returns all fields
<em>
except
</em>
the
<code class="leafygreen-ui-1l06pbn">
first
</code>
field in the
<code class="leafygreen-ui-1l06pbn">
name
</code>
embedded document and the
<code class="leafygreen-ui-1l06pbn">
birth
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find(\n { contribs: 'OOP' },\n { 'name.first': 0, birth: 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">db.<span class="lg-highlight-property">bios</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">contribs</span>: <span class="lg-highlight-string">'OOP'</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">'name.first'</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">birth</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>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Explicitly Exclude the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
Field
<a class="headerlink leafygreen-ui-1kepx47" href="#explicitly-exclude-the-_id-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="explicitly-exclude-the-_id-field">
</div>
</a>
</h4>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Unless the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field is explicitly excluded in the projection
document
<code class="leafygreen-ui-1l06pbn">
_id: 0
</code>
, the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field is returned.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following operation finds documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios collection
</a>
and returns only the
<code class="leafygreen-ui-1l06pbn">
name
</code>
field and the
<code class="leafygreen-ui-1l06pbn">
contribs
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find(\n { },\n { name: 1, contribs: 1, _id: 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">db.<span class="lg-highlight-property">bios</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">name</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">contribs</span>: <span class="lg-highlight-number">1</span>, <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></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
On Arrays and Embedded Documents
<a class="headerlink leafygreen-ui-1kepx47" href="#on-arrays-and-embedded-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="on-arrays-and-embedded-documents">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following operation queries the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios collection
</a>
and returns the
<code class="leafygreen-ui-1l06pbn">
last
</code>
field in
the
<code class="leafygreen-ui-1l06pbn">
name
</code>
embedded document and the first two elements in the
<code class="leafygreen-ui-1l06pbn">
contribs
</code>
array:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find(\n { },\n { _id: 0, 'name.last': 1, contribs: { $slice: 2 } } )","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">bios</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">_id</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-string">'name.last'</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">contribs</span>: <!-- -->{ <span class="lg-highlight-attr">$slice</span>: <span class="lg-highlight-number">2</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 specify embedded fields using the nested form. For example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find(\n { },\n { _id: 0, name: { last: 1 }, contribs: { $slice: 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-property">bios</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">_id</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">name</span>: <!-- -->{ <span class="lg-highlight-attr">last</span>: <span class="lg-highlight-number">1</span> }<!-- -->, <span class="lg-highlight-attr">contribs</span>: <!-- -->{ <span class="lg-highlight-attr">$slice</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>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Use Aggregation Expression
<a class="headerlink leafygreen-ui-1kepx47" href="#use-aggregation-expression" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-aggregation-expression">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</span>
</a>
projection can accept
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
aggregation expressions and syntax
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
With the use of aggregation expressions and syntax, you can project new
fields or project existing fields with new values. For example, the
following operation uses aggregation expressions to override the value
of the
<code class="leafygreen-ui-1l06pbn">
name
</code>
and
<code class="leafygreen-ui-1l06pbn">
awards
</code>
fields as well as to include new fields
<code class="leafygreen-ui-1l06pbn">
reportDate
</code>
,
<code class="leafygreen-ui-1l06pbn">
reportBy
</code>
, and
<code class="leafygreen-ui-1l06pbn">
reportNumber
</code>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find(\n { },\n {\n _id: 0,\n name: {\n $concat: [\n { $ifNull: [ \"$name.aka\", \"$name.first\" ] },\n \" \",\n \"$name.last\"\n ]\n },\n birth: 1,\n contribs: 1,\n awards: { $cond: { if: { $isArray: \"$awards\" }, then: { $size: \"$awards\" }, else: 0 } },\n reportDate: { $dateToString: { date: new Date(), format: \"%Y-%m-%d\" } },\n reportBy: \"hellouser123\",\n reportNumber: { $literal: 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-property">bios</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"> <!-- -->{</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"> <span class="lg-highlight-attr">name</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$concat</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$ifNull</span>: <!-- -->[ <span class="lg-highlight-string">"$name.aka"</span>, <span class="lg-highlight-string">"$name.first"</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">"$name.last"</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">birth</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">contribs</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">awards</span>: <!-- -->{ <span class="lg-highlight-attr">$cond</span>: <!-- -->{ <span class="lg-highlight-attr">if</span>: <!-- -->{ <span class="lg-highlight-attr">$isArray</span>: <span class="lg-highlight-string">"$awards"</span> }<!-- -->, <span class="lg-highlight-attr">then</span>: <!-- -->{ <span class="lg-highlight-attr">$size</span>: <span class="lg-highlight-string">"$awards"</span> }<!-- -->, <span class="lg-highlight-attr">else</span>: <span class="lg-highlight-number">0</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">reportDate</span>: <!-- -->{ <span class="lg-highlight-attr">$dateToString</span>: <!-- -->{ <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<!-- -->)<!-- -->, <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"%Y-%m-%d"</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">reportBy</span>: <span class="lg-highlight-string">"hellouser123"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">reportNumber</span>: <!-- -->{ <span class="lg-highlight-attr">$literal</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>
<p class="leafygreen-ui-1kp3ins">
To set the
<code class="leafygreen-ui-1l06pbn">
reportRun
</code>
field to the value
<code class="leafygreen-ui-1l06pbn">
1
</code>
The operation returns the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"birth\" : ISODate(\"1924-12-03T05:00:00Z\"), \"contribs\" : [ \"Fortran\", \"ALGOL\", \"Backus-Naur Form\", \"FP\" ], \"name\" : \"John Backus\", \"awards\" : 4, \"reportDate\" : \"2020-06-05\", \"reportBy\" : \"hellouser123\", \"reportNumber\" : 1 }\n{ \"birth\" : ISODate(\"1927-09-04T04:00:00Z\"), \"contribs\" : [ \"Lisp\", \"Artificial Intelligence\", \"ALGOL\" ], \"name\" : \"John McCarthy\", \"awards\" : 3, \"reportDate\" : \"2020-06-05\", \"reportBy\" : \"hellouser123\", \"reportNumber\" : 1 }\n{ \"birth\" : ISODate(\"1906-12-09T05:00:00Z\"), \"contribs\" : [ \"UNIVAC\", \"compiler\", \"FLOW-MATIC\", \"COBOL\" ], \"name\" : \"Grace Hopper\", \"awards\" : 4, \"reportDate\" : \"2020-06-05\", \"reportBy\" : \"hellouser123\", \"reportNumber\" : 1 }\n{ \"birth\" : ISODate(\"1926-08-27T04:00:00Z\"), \"contribs\" : [ \"OOP\", \"Simula\" ], \"name\" : \"Kristen Nygaard\", \"awards\" : 3, \"reportDate\" : \"2020-06-05\", \"reportBy\" : \"hellouser123\", \"reportNumber\" : 1 }\n{ \"birth\" : ISODate(\"1931-10-12T04:00:00Z\"), \"contribs\" : [ \"OOP\", \"Simula\" ], \"name\" : \"Ole-Johan Dahl\", \"awards\" : 3, \"reportDate\" : \"2020-06-05\", \"reportBy\" : \"hellouser123\", \"reportNumber\" : 1 }\n{ \"birth\" : ISODate(\"1956-01-31T05:00:00Z\"), \"contribs\" : [ \"Python\" ], \"name\" : \"Guido van Rossum\", \"awards\" : 2, \"reportDate\" : \"2020-06-05\", \"reportBy\" : \"hellouser123\", \"reportNumber\" : 1 }\n{ \"birth\" : ISODate(\"1941-09-09T04:00:00Z\"), \"contribs\" : [ \"UNIX\", \"C\" ], \"name\" : \"Dennis Ritchie\", \"awards\" : 3, \"reportDate\" : \"2020-06-05\", \"reportBy\" : \"hellouser123\", \"reportNumber\" : 1 }\n{ \"birth\" : ISODate(\"1965-04-14T04:00:00Z\"), \"contribs\" : [ \"Ruby\" ], \"name\" : \"Matz Matsumoto\", \"awards\" : 1, \"reportDate\" : \"2020-06-05\", \"reportBy\" : \"hellouser123\", \"reportNumber\" : 1 }\n{ \"birth\" : ISODate(\"1955-05-19T04:00:00Z\"), \"contribs\" : [ \"Java\" ], \"name\" : \"James Gosling\", \"awards\" : 2, \"reportDate\" : \"2020-06-05\", \"reportBy\" : \"hellouser123\", \"reportNumber\" : 1 }\n{ \"contribs\" : [ \"Scala\" ], \"name\" : \"Martin Odersky\", \"awards\" : 0, \"reportDate\" : \"2020-06-05\", \"reportBy\" : \"hellouser123\", \"reportNumber\" : 1 }","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">"birth"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"1924-12-03T05:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"contribs"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Fortran"</span>, <span class="lg-highlight-string">"ALGOL"</span>, <span class="lg-highlight-string">"Backus-Naur Form"</span>, <span class="lg-highlight-string">"FP"</span> ]<!-- -->, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"John Backus"</span>, <span class="lg-highlight-string">"awards"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"reportDate"</span> <!-- -->: <span class="lg-highlight-string">"2020-06-05"</span>, <span class="lg-highlight-string">"reportBy"</span> <!-- -->: <span class="lg-highlight-string">"hellouser123"</span>, <span class="lg-highlight-string">"reportNumber"</span> <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"birth"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"1927-09-04T04:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"contribs"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Lisp"</span>, <span class="lg-highlight-string">"Artificial Intelligence"</span>, <span class="lg-highlight-string">"ALGOL"</span> ]<!-- -->, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"John McCarthy"</span>, <span class="lg-highlight-string">"awards"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"reportDate"</span> <!-- -->: <span class="lg-highlight-string">"2020-06-05"</span>, <span class="lg-highlight-string">"reportBy"</span> <!-- -->: <span class="lg-highlight-string">"hellouser123"</span>, <span class="lg-highlight-string">"reportNumber"</span> <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"birth"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"1906-12-09T05:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"contribs"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"UNIVAC"</span>, <span class="lg-highlight-string">"compiler"</span>, <span class="lg-highlight-string">"FLOW-MATIC"</span>, <span class="lg-highlight-string">"COBOL"</span> ]<!-- -->, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Grace Hopper"</span>, <span class="lg-highlight-string">"awards"</span> <!-- -->: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"reportDate"</span> <!-- -->: <span class="lg-highlight-string">"2020-06-05"</span>, <span class="lg-highlight-string">"reportBy"</span> <!-- -->: <span class="lg-highlight-string">"hellouser123"</span>, <span class="lg-highlight-string">"reportNumber"</span> <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"birth"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"1926-08-27T04:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"contribs"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"OOP"</span>, <span class="lg-highlight-string">"Simula"</span> ]<!-- -->, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Kristen Nygaard"</span>, <span class="lg-highlight-string">"awards"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"reportDate"</span> <!-- -->: <span class="lg-highlight-string">"2020-06-05"</span>, <span class="lg-highlight-string">"reportBy"</span> <!-- -->: <span class="lg-highlight-string">"hellouser123"</span>, <span class="lg-highlight-string">"reportNumber"</span> <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"birth"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"1931-10-12T04:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"contribs"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"OOP"</span>, <span class="lg-highlight-string">"Simula"</span> ]<!-- -->, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Ole-Johan Dahl"</span>, <span class="lg-highlight-string">"awards"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"reportDate"</span> <!-- -->: <span class="lg-highlight-string">"2020-06-05"</span>, <span class="lg-highlight-string">"reportBy"</span> <!-- -->: <span class="lg-highlight-string">"hellouser123"</span>, <span class="lg-highlight-string">"reportNumber"</span> <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"birth"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"1956-01-31T05:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"contribs"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Python"</span> ]<!-- -->, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Guido van Rossum"</span>, <span class="lg-highlight-string">"awards"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"reportDate"</span> <!-- -->: <span class="lg-highlight-string">"2020-06-05"</span>, <span class="lg-highlight-string">"reportBy"</span> <!-- -->: <span class="lg-highlight-string">"hellouser123"</span>, <span class="lg-highlight-string">"reportNumber"</span> <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"birth"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"1941-09-09T04:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"contribs"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"UNIX"</span>, <span class="lg-highlight-string">"C"</span> ]<!-- -->, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Dennis Ritchie"</span>, <span class="lg-highlight-string">"awards"</span> <!-- -->: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-string">"reportDate"</span> <!-- -->: <span class="lg-highlight-string">"2020-06-05"</span>, <span class="lg-highlight-string">"reportBy"</span> <!-- -->: <span class="lg-highlight-string">"hellouser123"</span>, <span class="lg-highlight-string">"reportNumber"</span> <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"birth"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"1965-04-14T04:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"contribs"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Ruby"</span> ]<!-- -->, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Matz Matsumoto"</span>, <span class="lg-highlight-string">"awards"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"reportDate"</span> <!-- -->: <span class="lg-highlight-string">"2020-06-05"</span>, <span class="lg-highlight-string">"reportBy"</span> <!-- -->: <span class="lg-highlight-string">"hellouser123"</span>, <span class="lg-highlight-string">"reportNumber"</span> <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"birth"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"1955-05-19T04:00:00Z"</span>)<!-- -->, <span class="lg-highlight-string">"contribs"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Java"</span> ]<!-- -->, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"James Gosling"</span>, <span class="lg-highlight-string">"awards"</span> <!-- -->: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-string">"reportDate"</span> <!-- -->: <span class="lg-highlight-string">"2020-06-05"</span>, <span class="lg-highlight-string">"reportBy"</span> <!-- -->: <span class="lg-highlight-string">"hellouser123"</span>, <span class="lg-highlight-string">"reportNumber"</span> <!-- -->: <span class="lg-highlight-number">1</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"contribs"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Scala"</span> ]<!-- -->, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Martin Odersky"</span>, <span class="lg-highlight-string">"awards"</span> <!-- -->: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-string">"reportDate"</span> <!-- -->: <span class="lg-highlight-string">"2020-06-05"</span>, <span class="lg-highlight-string">"reportBy"</span> <!-- -->: <span class="lg-highlight-string">"hellouser123"</span>, <span class="lg-highlight-string">"reportNumber"</span> <!-- -->: <span class="lg-highlight-number">1</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/project-fields-from-query-results/">
Project Fields to Return from Query
</a>
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-crud-read-cursor">
</span>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Iterate the Returned Cursor
<a class="headerlink leafygreen-ui-1kepx47" href="#iterate-the-returned-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-returned-cursor">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</span>
</a>
method returns a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/iterate-a-cursor/#std-label-read-operations-cursors">
cursor
</a>
to the results.
</p>
<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>
, if the returned cursor is not assigned to a
variable using the
<code class="leafygreen-ui-1l06pbn">
var
</code>
keyword, the cursor is automatically iterated to
access up to the first 20 documents that match the query. You can update the
<code class="leafygreen-ui-1l06pbn">
displayBatchSize
</code>
variable to change the number of automatically
iterated documents.
</p>
<p class="leafygreen-ui-1kp3ins">
The following example sets the batch size to 3. Future
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
operations will only return 3 documents per cursor iteration.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"config.set( \"displayBatchSize\", 3 )","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">config.<span class="lg-highlight-title lg-highlight-function">set</span>( <span class="lg-highlight-string">"displayBatchSize"</span>, <span class="lg-highlight-number">3</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 manually iterate over the results, assign the returned cursor to a variable
with the
<code class="leafygreen-ui-1l06pbn">
var
</code>
keyword, as shown in the following sections.
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
With Variable Name
<a class="headerlink leafygreen-ui-1kepx47" href="#with-variable-name" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-variable-name">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following example uses the variable
<code class="leafygreen-ui-1l06pbn">
myCursor
</code>
to iterate over the
cursor and print the matching documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var myCursor = db.bios.find( );\n\nmyCursor","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> myCursor <!-- -->= <!-- -->db.<span class="lg-highlight-property">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</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">myCursor</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
With
<code class="leafygreen-ui-1l06pbn">
next()
</code>
Method
<a class="headerlink leafygreen-ui-1kepx47" href="#with-next---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="with-next---method">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following example uses the cursor method
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.next/#mongodb-method-cursor.next">
<code class="leafygreen-ui-1l06pbn">
next()
</code>
</a>
to
access the documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var myCursor = db.bios.find( );\n\nvar myDocument = myCursor.hasNext() ? myCursor.next() : null;\n\nif (myDocument) {\n var myName = myDocument.name;\n print (tojson(myName));\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> myCursor <!-- -->= <!-- -->db.<span class="lg-highlight-property">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</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> myDocument <!-- -->= <!-- -->myCursor.<span class="lg-highlight-title lg-highlight-function">hasNext</span>(<!-- -->) <!-- -->? <!-- -->myCursor.<span class="lg-highlight-title lg-highlight-function">next</span>(<!-- -->) <!-- -->: <span class="lg-highlight-literal">null</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> <!-- -->(<!-- -->myDocument) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">var</span> myName <!-- -->= <!-- -->myDocument.<span class="lg-highlight-property">name</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->print <!-- -->(<span class="lg-highlight-title lg-highlight-function">tojson</span>(<!-- -->myName))<!-- -->;</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 print, you can also use the
<code class="leafygreen-ui-1l06pbn">
printjson()
</code>
method instead of
<code class="leafygreen-ui-1l06pbn">
print(tojson())
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"if (myDocument) {\n var myName = myDocument.name;\n printjson(myName);\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">if</span> <!-- -->(<!-- -->myDocument) <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-keyword">var</span> myName <!-- -->= <!-- -->myDocument.<span class="lg-highlight-property">name</span>;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">printjson</span>(<!-- -->myName)<!-- -->;</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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
With
<code class="leafygreen-ui-1l06pbn">
forEach()
</code>
Method
<a class="headerlink leafygreen-ui-1kepx47" href="#with-foreach---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="with-foreach---method">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following example uses the cursor method
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.forEach/#mongodb-method-cursor.forEach">
<code class="leafygreen-ui-1l06pbn">
forEach()
</code>
</a>
to iterate the cursor and access the documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var myCursor = db.bios.find( );\n\nmyCursor.forEach(printjson);","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> myCursor <!-- -->= <!-- -->db.<span class="lg-highlight-property">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</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">myCursor.<span class="lg-highlight-title lg-highlight-function">forEach</span>(<!-- -->printjson)<!-- -->;</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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-find-modify-cursor">
</span>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Modify the Cursor Behavior
<a class="headerlink leafygreen-ui-1kepx47" href="#modify-the-cursor-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="modify-the-cursor-behavior">
</div>
</a>
</h3>
<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>
and the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
<span>
drivers
</span>
</a>
provide several cursor methods that call on the
<em>
cursor
</em>
returned by the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
find()
</code>
</span>
</a>
method to
modify its behavior.
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Order Documents in the Result Set
<a class="headerlink leafygreen-ui-1kepx47" href="#order-documents-in-the-result-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="order-documents-in-the-result-set">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.sort/#mongodb-method-cursor.sort">
<code class="leafygreen-ui-1l06pbn">
sort()
</code>
</a>
method orders the documents in the result
set. The following operation returns documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/">
bios
collection
</a>
sorted in ascending
order by 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.bios.find().sort( { 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">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">name</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 class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.sort/#mongodb-method-cursor.sort">
<code class="leafygreen-ui-1l06pbn">
sort()
</code>
</a>
corresponds to the
<code class="leafygreen-ui-1l06pbn">
ORDER BY
</code>
statement in SQL.
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Limit the Number of Documents to Return
<a class="headerlink leafygreen-ui-1kepx47" href="#limit-the-number-of-documents-to-return" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-documents-to-return">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
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 limits the number of documents in
the result set. The following operation returns at most
<code class="leafygreen-ui-1l06pbn">
5
</code>
documents
in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/#std-label-bios-example-collection">
bios collection
<!-- -->
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find().limit( 5 )","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">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">limit</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">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.limit/#mongodb-method-cursor.limit">
<code class="leafygreen-ui-1l06pbn">
limit()
</code>
</a>
corresponds to the
<code class="leafygreen-ui-1l06pbn">
LIMIT
</code>
statement in SQL.
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Set the Starting Point of the Result Set
<a class="headerlink leafygreen-ui-1kepx47" href="#set-the-starting-point-of-the-result-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="set-the-starting-point-of-the-result-set">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.skip/#mongodb-method-cursor.skip">
<code class="leafygreen-ui-1l06pbn">
skip()
</code>
</a>
method controls the starting point of the
results set. The following operation skips the first
<code class="leafygreen-ui-1l06pbn">
5
</code>
documents in
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bios-example-collection/#std-label-bios-example-collection">
bios collection
</a>
and
returns all remaining documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find().skip( 5 )","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">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">skip</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>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Specify Collation
<a class="headerlink leafygreen-ui-1kepx47" href="#specify-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="specify-collation">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
Collation
</a>
allows users to specify
language-specific rules for string comparison, such as rules for
lettercase and accent marks.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.collation/#mongodb-method-cursor.collation">
<code class="leafygreen-ui-1l06pbn">
collation()
</code>
</a>
method specifies the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/collation/#std-label-collation">
collation
</a>
for the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.find" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.find()
</code>
</span>
</a>
operation.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find( { \"name.last\": \"hopper\" } ).collation( { locale: \"en_US\", 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-property">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-string">"name.last"</span>: <span class="lg-highlight-string">"hopper"</span> } ).<span class="lg-highlight-title lg-highlight-function">collation</span>( <!-- -->{ <span class="lg-highlight-attr">locale</span>: <span class="lg-highlight-string">"en_US"</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>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Combine Cursor Methods
<a class="headerlink leafygreen-ui-1kepx47" href="#combine-cursor-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="combine-cursor-methods">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following statements chain cursor methods
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.limit/#mongodb-method-cursor.limit">
<code class="leafygreen-ui-1l06pbn">
limit()
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.sort/#mongodb-method-cursor.sort">
<code class="leafygreen-ui-1l06pbn">
sort()
</code>
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.bios.find().sort( { name: 1 } ).limit( 5 )\ndb.bios.find().limit( 5 ).sort( { name: 1 } )","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">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-number">1</span> } ).<span class="lg-highlight-title lg-highlight-function">limit</span>( <span class="lg-highlight-number">5</span> )</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">bios</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">limit</span>( <span class="lg-highlight-number">5</span> ).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">name</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 two statements are equivalent; i.e. the order in which you chain
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>
and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.sort/#mongodb-method-cursor.sort">
<code class="leafygreen-ui-1l06pbn">
sort()
</code>
</a>
methods
is not significant. Both statements return the first five documents, as
determined by the ascending sort order on 'name'.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-find-cursor-methods">
</span>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Available
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
Cursor Methods
<a class="headerlink leafygreen-ui-1kepx47" href="#available-mongosh-cursor-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="available-mongosh-cursor-methods">
</div>
</a>
</h4>
<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/cursor.allowDiskUse/#mongodb-method-cursor.allowDiskUse">
<code class="leafygreen-ui-1l06pbn">
cursor.allowDiskUse()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.allowPartialResults/#mongodb-method-cursor.allowPartialResults">
<code class="leafygreen-ui-1l06pbn">
cursor.allowPartialResults()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<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>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.close/#mongodb-method-cursor.close">
<code class="leafygreen-ui-1l06pbn">
cursor.close()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.isClosed/#mongodb-method-cursor.isClosed">
<code class="leafygreen-ui-1l06pbn">
cursor.isClosed()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.collation/#mongodb-method-cursor.collation">
<code class="leafygreen-ui-1l06pbn">
cursor.collation()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.comment/#mongodb-method-cursor.comment">
<code class="leafygreen-ui-1l06pbn">
cursor.comment()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.count/#mongodb-method-cursor.count">
<code class="leafygreen-ui-1l06pbn">
cursor.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/cursor.explain/#mongodb-method-cursor.explain">
<code class="leafygreen-ui-1l06pbn">
cursor.explain()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.forEach/#mongodb-method-cursor.forEach">
<code class="leafygreen-ui-1l06pbn">
cursor.forEach()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.hasNext/#mongodb-method-cursor.hasNext">
<code class="leafygreen-ui-1l06pbn">
cursor.hasNext()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.hint/#mongodb-method-cursor.hint">
<code class="leafygreen-ui-1l06pbn">
cursor.hint()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.isExhausted/#mongodb-method-cursor.isExhausted">
<code class="leafygreen-ui-1l06pbn">
cursor.isExhausted()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.itcount/#mongodb-method-cursor.itcount">
<code class="leafygreen-ui-1l06pbn">
cursor.itcount()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.limit/#mongodb-method-cursor.limit">
<code class="leafygreen-ui-1l06pbn">
cursor.limit()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.map/#mongodb-method-cursor.map">
<code class="leafygreen-ui-1l06pbn">
cursor.map()
</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/cursor.max/#mongodb-method-cursor.max">
<code class="leafygreen-ui-1l06pbn">
cursor.max()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.maxTimeMS/#mongodb-method-cursor.maxTimeMS">
<code class="leafygreen-ui-1l06pbn">
cursor.maxTimeMS()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.min/#mongodb-method-cursor.min">
<code class="leafygreen-ui-1l06pbn">
cursor.min()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.next/#mongodb-method-cursor.next">
<code class="leafygreen-ui-1l06pbn">
cursor.next()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.noCursorTimeout/#mongodb-method-cursor.noCursorTimeout">
<code class="leafygreen-ui-1l06pbn">
cursor.noCursorTimeout()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.objsLeftInBatch/#mongodb-method-cursor.objsLeftInBatch">
<code class="leafygreen-ui-1l06pbn">
cursor.objsLeftInBatch()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.pretty/#mongodb-method-cursor.pretty">
<code class="leafygreen-ui-1l06pbn">
cursor.pretty()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.readConcern/#mongodb-method-cursor.readConcern">
<code class="leafygreen-ui-1l06pbn">
cursor.readConcern()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.readPref/#mongodb-method-cursor.readPref">
<code class="leafygreen-ui-1l06pbn">
cursor.readPref()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.returnKey/#mongodb-method-cursor.returnKey">
<code class="leafygreen-ui-1l06pbn">
cursor.returnKey()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.showRecordId/#mongodb-method-cursor.showRecordId">
<code class="leafygreen-ui-1l06pbn">
cursor.showRecordId()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.size/#mongodb-method-cursor.size">
<code class="leafygreen-ui-1l06pbn">
cursor.size()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.skip/#mongodb-method-cursor.skip">
<code class="leafygreen-ui-1l06pbn">
cursor.skip()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.sort/#mongodb-method-cursor.sort">
<code class="leafygreen-ui-1l06pbn">
cursor.sort()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.tailable/#mongodb-method-cursor.tailable">
<code class="leafygreen-ui-1l06pbn">
cursor.tailable()
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.toArray/#mongodb-method-cursor.toArray">
<code class="leafygreen-ui-1l06pbn">
cursor.toArray()
</code>
</a>
</p>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Use Variables in
<code class="leafygreen-ui-1l06pbn">
let
</code>
Option
<a class="headerlink leafygreen-ui-1kepx47" href="#use-variables-in-let-option" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-variables-in-let-option">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can specify query options to modify query behavior and indicate how
results are returned.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, to define variables that you can access elsewhere in the
<code class="leafygreen-ui-1l06pbn">
find
</code>
method, use the
<code class="leafygreen-ui-1l06pbn">
let
</code>
option. To filter results using a
variable, you must access the variable within the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
<code class="leafygreen-ui-1l06pbn">
$expr
</code>
</a>
operator.
</p>
<p class="leafygreen-ui-1kp3ins">
Create a collection
<code class="leafygreen-ui-1l06pbn">
cakeFlavors
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeFlavors.insertMany( [\n { _id: 1, flavor: \"chocolate\" },\n { _id: 2, flavor: \"strawberry\" },\n { _id: 3, flavor: \"cherry\" }\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">cakeFlavors</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">flavor</span>: <span class="lg-highlight-string">"chocolate"</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">flavor</span>: <span class="lg-highlight-string">"strawberry"</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">flavor</span>: <span class="lg-highlight-string">"cherry"</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 defines a
<code class="leafygreen-ui-1l06pbn">
targetFlavor
</code>
variable in
<code class="leafygreen-ui-1l06pbn">
let
</code>
and
uses the variable to retrieve the chocolate cake flavor:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeFlavors.find(\n { $expr: { $eq: [ \"$flavor\", \"$$targetFlavor\" ] } },\n { _id: 0 },\n { let : { targetFlavor: \"chocolate\" }\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">cakeFlavors</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-string">"$flavor"</span>, <span class="lg-highlight-string">"$$targetFlavor"</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"> <!-- -->{ <span class="lg-highlight-keyword">let</span> <!-- -->: <!-- -->{ <span class="lg-highlight-attr">targetFlavor</span>: <span class="lg-highlight-string">"chocolate"</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">
Output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[ { flavor: 'chocolate' } ]","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">flavor</span>: <span class="lg-highlight-string">'chocolate'</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-find-user-roles-system-variable-example">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Retrieve Documents for Roles Granted to the Current User
<a class="headerlink leafygreen-ui-1kepx47" href="#retrieve-documents-for-roles-granted-to-the-current-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="retrieve-documents-for-roles-granted-to-the-current-user">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 7.0, you can use the new
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/aggregation-variables/#mongodb-variable-variable.USER_ROLES">
<code class="leafygreen-ui-1l06pbn">
USER_ROLES
</code>
</a>
system variable to return user
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/authorization/#std-label-roles">
roles
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The scenario in this section shows users with various roles who have
limited access to documents in a collection containing budget
information.
</p>
<p class="leafygreen-ui-1kp3ins">
The scenario shows one possible use of
<code class="leafygreen-ui-1l06pbn">
USER_ROLES
</code>
. The
<code class="leafygreen-ui-1l06pbn">
budget
</code>
collection contains documents with a field named
<code class="leafygreen-ui-1l06pbn">
allowedRoles
</code>
. As
you'll see in the following scenario, you can write queries that compare
the user roles found in the
<code class="leafygreen-ui-1l06pbn">
allowedRoles
</code>
field with the roles
returned by the
<code class="leafygreen-ui-1l06pbn">
USER_ROLES
</code>
system variable.
</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 another
<code class="leafygreen-ui-1l06pbn">
USER_ROLES
</code>
example scenario, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/views/create-view/#std-label-create-view-user-roles-system-variable-medical-example">
Retrieve Medical Information for Roles Granted to the Current User
</a>
. That
example doesn't store the user roles in the document fields, as is
done in the following example.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For the budget scenario in this section, perform the following steps to
create the roles, users, and
<code class="leafygreen-ui-1l06pbn">
budget
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Run:","name":"Create the roles"},{"@type":"HowToStep","text":"Create users named John and Jane with the required roles.\nReplace the test database with your database name.","name":"Create the users"},{"@type":"HowToStep","text":"Run:","name":"Create the collection"}],"name":"Retrieve Documents for Roles Granted to the Current User","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 roles
<a class="headerlink leafygreen-ui-1kepx47" href="#create-the-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="create-the-roles">
</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.createRole( { role: \"Marketing\", roles: [], privileges: [] } )\ndb.createRole( { role: \"Sales\", roles: [], privileges: [] } )\ndb.createRole( { role: \"Development\", roles: [], privileges: [] } )\ndb.createRole( { role: \"Operations\", roles: [], privileges: [] } )","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">createRole</span>( <!-- -->{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"Marketing"</span>, <span class="lg-highlight-attr">roles</span>: <!-- -->[<!-- -->]<!-- -->, <span class="lg-highlight-attr">privileges</span>: <!-- -->[<!-- -->] } )</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">createRole</span>( <!-- -->{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"Sales"</span>, <span class="lg-highlight-attr">roles</span>: <!-- -->[<!-- -->]<!-- -->, <span class="lg-highlight-attr">privileges</span>: <!-- -->[<!-- -->] } )</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">createRole</span>( <!-- -->{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"Development"</span>, <span class="lg-highlight-attr">roles</span>: <!-- -->[<!-- -->]<!-- -->, <span class="lg-highlight-attr">privileges</span>: <!-- -->[<!-- -->] } )</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-title lg-highlight-function">createRole</span>( <!-- -->{ <span class="lg-highlight-attr">role</span>: <span class="lg-highlight-string">"Operations"</span>, <span class="lg-highlight-attr">roles</span>: <!-- -->[<!-- -->]<!-- -->, <span class="lg-highlight-attr">privileges</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">
Create the users
<a class="headerlink leafygreen-ui-1kepx47" href="#create-the-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="create-the-users">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Create users named
<code class="leafygreen-ui-1l06pbn">
John
</code>
and
<code class="leafygreen-ui-1l06pbn">
Jane
</code>
with the required roles.
Replace the
<code class="leafygreen-ui-1l06pbn">
test
</code>
database with your database name.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.createUser( {\n user: \"John\",\n pwd: \"jn008\",\n roles: [\n { role: \"Marketing\", db: \"test\" },\n { role: \"Development\", db: \"test\" },\n { role: \"Operations\", db: \"test\" },\n { role: \"read\", db: \"test\" }\n ]\n} )\n\ndb.createUser( {\n user: \"Jane\",\n pwd: \"je009\",\n roles: [\n { role: \"Sales\", db: \"test\" },\n { role: \"Operations\", db: \"test\" },\n { role: \"read\", db: \"test\" }\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">createUser</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">user</span>: <span class="lg-highlight-string">"John"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pwd</span>: <span class="lg-highlight-string">"jn008"</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">"Marketing"</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">"Development"</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">"Operations"</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">"read"</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><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">createUser</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">user</span>: <span class="lg-highlight-string">"Jane"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">pwd</span>: <span class="lg-highlight-string">"je009"</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">"Sales"</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">"Operations"</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">"read"</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><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">
Create the collection
<a class="headerlink leafygreen-ui-1kepx47" href="#create-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="create-the-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.budget.insertMany( [\n {\n _id: 0,\n allowedRoles: [ \"Marketing\" ],\n comment: \"For marketing team\",\n yearlyBudget: 15000\n },\n {\n _id: 1,\n allowedRoles: [ \"Sales\" ],\n comment: \"For sales team\",\n yearlyBudget: 17000,\n salesEventsBudget: 1000\n },\n {\n _id: 2,\n allowedRoles: [ \"Operations\" ],\n comment: \"For operations team\",\n yearlyBudget: 19000,\n cloudBudget: 12000\n },\n {\n _id: 3,\n allowedRoles: [ \"Development\" ],\n comment: \"For development team\",\n yearlyBudget: 27000\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">budget</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">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">allowedRoles</span>: <!-- -->[ <span class="lg-highlight-string">"Marketing"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">comment</span>: <span class="lg-highlight-string">"For marketing team"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">yearlyBudget</span>: <span class="lg-highlight-number">15000</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">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">allowedRoles</span>: <!-- -->[ <span class="lg-highlight-string">"Sales"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">comment</span>: <span class="lg-highlight-string">"For sales team"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">yearlyBudget</span>: <span class="lg-highlight-number">17000</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">salesEventsBudget</span>: <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"> <!-- -->{</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">allowedRoles</span>: <!-- -->[ <span class="lg-highlight-string">"Operations"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">comment</span>: <span class="lg-highlight-string">"For operations team"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">yearlyBudget</span>: <span class="lg-highlight-number">19000</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cloudBudget</span>: <span class="lg-highlight-number">12000</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">allowedRoles</span>: <!-- -->[ <span class="lg-highlight-string">"Development"</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">comment</span>: <span class="lg-highlight-string">"For development team"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">yearlyBudget</span>: <span class="lg-highlight-number">27000</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>
<p class="leafygreen-ui-1kp3ins">
Perform the following steps to retrieve the documents accessible to
<code class="leafygreen-ui-1l06pbn">
John
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Run:","name":"Log in as John"},{"@type":"HowToStep","text":"To use a system variable, add $$ to the start of the variable name.\nSpecify the USER_ROLES system variable as $$USER_ROLES.Run:The previous example returns the documents from the budget\ncollection that match at least one of the roles that the user who runs\nthe example has. To do that, the example uses\n$setIntersection to return documents where the\nintersection between the budget document allowedRoles field and\nthe set of user roles from $$USER_ROLES is not empty.","name":"Retrieve the documents"},{"@type":"HowToStep","text":"John has the Marketing, Operations, and Development\nroles, and sees these documents:","name":"Examine the documents"}],"name":"Retrieve Documents for Roles Granted to the Current User","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">
Log in as John
<a class="headerlink leafygreen-ui-1kepx47" href="#log-in-as-john" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="log-in-as-john">
</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.auth( \"John\", \"jn008\" )","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">auth</span>( <span class="lg-highlight-string">"John"</span>, <span class="lg-highlight-string">"jn008"</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">
Retrieve the documents
<a class="headerlink leafygreen-ui-1kepx47" href="#retrieve-the-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="retrieve-the-documents">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
To use a system variable, add
<code class="leafygreen-ui-1l06pbn">
$$
</code>
to the start of the variable name.
Specify the
<code class="leafygreen-ui-1l06pbn">
USER_ROLES
</code>
system variable as
<code class="leafygreen-ui-1l06pbn">
$$USER_ROLES
</code>
.
</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":"db.budget.find( {\n $expr: {\n $not: {\n $eq: [ { $setIntersection: [ \"$allowedRoles\", \"$$USER_ROLES.role\" ] }, [] ]\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">budget</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>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$not</span>: <!-- -->{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$eq</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$setIntersection</span>: <!-- -->[ <span class="lg-highlight-string">"$allowedRoles"</span>, <span class="lg-highlight-string">"$$USER_ROLES.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">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 previous example returns the documents from the
<code class="leafygreen-ui-1l06pbn">
budget
</code>
collection that match at least one of the roles that the user who runs
the example has. To do that, the example uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setIntersection/#mongodb-expression-exp.-setIntersection">
<code class="leafygreen-ui-1l06pbn">
$setIntersection
</code>
</a>
to return documents where the
intersection between the
<code class="leafygreen-ui-1l06pbn">
budget
</code>
document
<code class="leafygreen-ui-1l06pbn">
allowedRoles
</code>
field and
the set of user roles from
<code class="leafygreen-ui-1l06pbn">
$$USER_ROLES
</code>
is not empty.
</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 documents
<a class="headerlink leafygreen-ui-1kepx47" href="#examine-the-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="examine-the-documents">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
John
</code>
has the
<code class="leafygreen-ui-1l06pbn">
Marketing
</code>
,
<code class="leafygreen-ui-1l06pbn">
Operations
</code>
, and
<code class="leafygreen-ui-1l06pbn">
Development
</code>
roles, and sees these documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: 0,\n allowedRoles: [ 'Marketing' ],\n comment: 'For marketing team',\n yearlyBudget: 15000\n },\n {\n _id: 2,\n allowedRoles: [ 'Operations' ],\n comment: 'For operations team',\n yearlyBudget: 19000,\n cloudBudget: 12000\n },\n {\n _id: 3,\n allowedRoles: [ 'Development' ],\n comment: 'For development team',\n yearlyBudget: 27000\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-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">allowedRoles</span>: <!-- -->[ <span class="lg-highlight-string">'Marketing'</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">comment</span>: <span class="lg-highlight-string">'For marketing team'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">yearlyBudget</span>: <span class="lg-highlight-number">15000</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">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">allowedRoles</span>: <!-- -->[ <span class="lg-highlight-string">'Operations'</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">comment</span>: <span class="lg-highlight-string">'For operations team'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">yearlyBudget</span>: <span class="lg-highlight-number">19000</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cloudBudget</span>: <span class="lg-highlight-number">12000</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">allowedRoles</span>: <!-- -->[ <span class="lg-highlight-string">'Development'</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">comment</span>: <span class="lg-highlight-string">'For development team'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">yearlyBudget</span>: <span class="lg-highlight-number">27000</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>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Perform the following steps to retrieve the documents accessible to
<code class="leafygreen-ui-1l06pbn">
Jane
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Run:","name":"Log in as Jane"},{"@type":"HowToStep","text":"Run:","name":"Retrieve the documents"},{"@type":"HowToStep","text":"Jane has the Sales and Operations roles, and sees these\ndocuments:On a sharded cluster, a query can be run on a shard by another server\nnode on behalf of the user. In those queries, USER_ROLES is still\npopulated with the roles for the user.","name":"Examine the documents"}],"name":"Retrieve Documents for Roles Granted to the Current User","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">
Log in as Jane
<a class="headerlink leafygreen-ui-1kepx47" href="#log-in-as-jane" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="log-in-as-jane">
</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.auth( \"Jane\", \"je009\" )","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">auth</span>( <span class="lg-highlight-string">"Jane"</span>, <span class="lg-highlight-string">"je009"</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">
Retrieve the documents
<a class="headerlink leafygreen-ui-1kepx47" href="#retrieve-the-documents-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="retrieve-the-documents-1">
</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.budget.find( {\n $expr: {\n $not: {\n $eq: [ { $setIntersection: [ \"$allowedRoles\", \"$$USER_ROLES.role\" ] }, [] ]\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">budget</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>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$not</span>: <!-- -->{</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$eq</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$setIntersection</span>: <!-- -->[ <span class="lg-highlight-string">"$allowedRoles"</span>, <span class="lg-highlight-string">"$$USER_ROLES.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">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 documents
<a class="headerlink leafygreen-ui-1kepx47" href="#examine-the-documents-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-documents-1">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
Jane
</code>
has the
<code class="leafygreen-ui-1l06pbn">
Sales
</code>
and
<code class="leafygreen-ui-1l06pbn">
Operations
</code>
roles, and sees these
documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n {\n _id: 1,\n allowedRoles: [ 'Sales' ],\n comment: 'For sales team',\n yearlyBudget: 17000,\n salesEventsBudget: 1000\n },\n {\n _id: 2,\n allowedRoles: [ 'Operations' ],\n comment: 'For operations team',\n yearlyBudget: 19000,\n cloudBudget: 12000\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-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">allowedRoles</span>: <!-- -->[ <span class="lg-highlight-string">'Sales'</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">comment</span>: <span class="lg-highlight-string">'For sales team'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">yearlyBudget</span>: <span class="lg-highlight-number">17000</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">salesEventsBudget</span>: <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"> <!-- -->{</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">allowedRoles</span>: <!-- -->[ <span class="lg-highlight-string">'Operations'</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">comment</span>: <span class="lg-highlight-string">'For operations team'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">yearlyBudget</span>: <span class="lg-highlight-number">19000</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">cloudBudget</span>: <span class="lg-highlight-number">12000</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 class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
On a sharded cluster, a query can be run on a shard by another server
node on behalf of the user. In those queries,
<code class="leafygreen-ui-1l06pbn">
USER_ROLES
</code>
is still
populated with the roles for the user.
</p>
</div>
</div>
</section>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Modify a Query with options
<a class="headerlink leafygreen-ui-1kepx47" href="#modify-a-query-with-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="modify-a-query-with-options">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following examples show how you can use the
<code class="leafygreen-ui-1l06pbn">
options
</code>
field
in a
<code class="leafygreen-ui-1l06pbn">
find()
</code>
query. Use the following
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.insertMany/#mongodb-method-db.collection.insertMany">
<code class="leafygreen-ui-1l06pbn">
insertMany()
</code>
</a>
to setup the
<code class="leafygreen-ui-1l06pbn">
users
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.users.insertMany( [\n { username: \"david\", age: 27 },\n { username: \"amanda\", age: 25 },\n { username: \"rajiv\", age: 32 },\n { username: \"rajiv\", age: 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">users</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">username</span>: <span class="lg-highlight-string">"david"</span>, <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">27</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">username</span>: <span class="lg-highlight-string">"amanda"</span>, <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">25</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">username</span>: <span class="lg-highlight-string">"rajiv"</span>, <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">32</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">username</span>: <span class="lg-highlight-string">"rajiv"</span>, <span class="lg-highlight-attr">age</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>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
limit with options
<a class="headerlink leafygreen-ui-1kepx47" href="#limit-with-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="limit-with-options">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following query limits the number of documents in the result set
with the
<code class="leafygreen-ui-1l06pbn">
limit
</code>
options parameter:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.users.find(\n { username : \"rajiv\"}, // query\n { age : 1 }, // projection\n { limit : 1 } // options\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">find</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->username <!-- -->: <span class="lg-highlight-string">"rajiv"</span>}<!-- -->, <span class="lg-highlight-comment">// query</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->age <!-- -->: <span class="lg-highlight-number">1</span> }<!-- -->, <span class="lg-highlight-comment">// projection</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->limit <!-- -->: <span class="lg-highlight-number">1</span> } <span class="lg-highlight-comment">// options</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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
allowDiskUse with options
<a class="headerlink leafygreen-ui-1kepx47" href="#allowdiskuse-with-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="allowdiskuse-with-options">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following query uses the
<code class="leafygreen-ui-1l06pbn">
options
</code>
parameter to enable
<code class="leafygreen-ui-1l06pbn">
allowDiskUse
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.users.find(\n { username : \"david\" },\n { age : 1 },\n { allowDiskUse : 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">users</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->username <!-- -->: <span class="lg-highlight-string">"david"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->age <!-- -->: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->allowDiskUse <!-- -->: <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>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
explain with options
<a class="headerlink leafygreen-ui-1kepx47" href="#explain-with-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="explain-with-options">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following query uses the
<code class="leafygreen-ui-1l06pbn">
options
</code>
parameter to get the
<code class="leafygreen-ui-1l06pbn">
executionStats
</code>
explain output:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var cursor = db.users.find(\n { username: \"amanda\" },\n { age : 1 },\n { explain : \"executionStats\" }\n)\ncursor.next()","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> cursor <!-- -->= <!-- -->db.<span class="lg-highlight-property">users</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">username</span>: <span class="lg-highlight-string">"amanda"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->age <!-- -->: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->explain <!-- -->: <span class="lg-highlight-string">"executionStats"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">cursor.<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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Specify Multiple options in a query
<a class="headerlink leafygreen-ui-1kepx47" href="#specify-multiple-options-in-a-query" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-multiple-options-in-a-query">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The following query uses multiple
<code class="leafygreen-ui-1l06pbn">
options
</code>
in a single query. This
query uses
<code class="leafygreen-ui-1l06pbn">
limit
</code>
set to
<code class="leafygreen-ui-1l06pbn">
2
</code>
to return only two documents, and
<code class="leafygreen-ui-1l06pbn">
showRecordId
</code>
set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
to return the position of the document
in the result set:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.users.find(\n {},\n { username: 1, age: 1 },\n {\n limit: 2,\n showRecordId: true\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">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">username</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">age</span>: <span class="lg-highlight-number">1</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"> <span class="lg-highlight-attr">limit</span>: <span class="lg-highlight-number">2</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">showRecordId</span>: <span class="lg-highlight-literal">true</span></td></tr><tr class="leafygreen-ui-1wr1lm6"><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>
<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/method/db.collection.findOne/#mongodb-method-db.collection.findOne">
<code class="leafygreen-ui-1l06pbn">
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.collection.findAndModify/#mongodb-method-db.collection.findAndModify">
<code class="leafygreen-ui-1l06pbn">
findAndModify()
</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.findOneAndDelete/#mongodb-method-db.collection.findOneAndDelete">
<code class="leafygreen-ui-1l06pbn">
findOneAndDelete()
</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.findOneAndReplace/#mongodb-method-db.collection.findOneAndReplace">
<code class="leafygreen-ui-1l06pbn">
findOneAndReplace()
</code>
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/manage-journaling/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Configure Journaling for Self-Managed Deployments
<a class="headerlink leafygreen-ui-1kepx47" href="#configure-journaling-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-journaling-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="#procedures" target="_self">
<span>
Procedures
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
MongoDB uses
<em>
write ahead logging
</em>
to an on-disk
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-journal">
journal
</a>
to
guarantee
<a class="leafygreen-ui-tqgtui" href="/docs/manual/crud/#std-label-crud">
write operation
</a>
durability.
</p>
<p class="leafygreen-ui-1kp3ins">
The WiredTiger storage engine does not require journaling to guarantee
a consistent state after a crash. The database will be restored to the
last consistent
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger-checkpoints">
checkpoint
</a>
during recovery. However, if MongoDB exits unexpectedly in between
checkpoints, journaling is required to recover writes that occurred
after the last checkpoint.
</p>
<p class="leafygreen-ui-1kp3ins">
If
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
stops unexpectedly, the program can recover everything
written to the journal. MongoDB will re-apply the write operations on restart
and maintain a consistent state. By default, the greatest extent of lost writes,
i.e., those not made to the journal, are those made in the last 100
milliseconds, plus the time it takes to perform the actual journal writes. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-storage.journal.commitIntervalMs">
<code class="leafygreen-ui-1l06pbn">
commitIntervalMs
</code>
</a>
for more information on
the default.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Procedures
<a class="headerlink leafygreen-ui-1kepx47" href="#procedures" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="procedures">
</div>
</a>
</h2>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Get Commit Acknowledgement
<a class="headerlink leafygreen-ui-1kepx47" href="#get-commit-acknowledgement" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="get-commit-acknowledgement">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can get commit acknowledgment with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-write-concern">
Write Concern
</a>
and
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.j">
<code class="leafygreen-ui-1l06pbn">
j
</code>
</a>
option. For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-write-concern-operation">
Write Concern
<!-- -->
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Monitor Journal Status
<a class="headerlink leafygreen-ui-1kepx47" href="#monitor-journal-status" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="monitor-journal-status">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/serverStatus/#mongodb-dbcommand-dbcmd.serverStatus">
<code class="leafygreen-ui-1l06pbn">
serverStatus
</code>
</a>
command/
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.serverStatus/#mongodb-method-db.serverStatus">
<code class="leafygreen-ui-1l06pbn">
db.serverStatus()
</code>
</a>
method returns
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/serverStatus/#mongodb-serverstatus-serverstatus.wiredTiger.log">
<code class="leafygreen-ui-1l06pbn">
wiredTiger.log
</code>
</a>
, which contains
statistics on the journal.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Recover Data After Unexpected Shutdown
<a class="headerlink leafygreen-ui-1kepx47" href="#recover-data-after-unexpected-shutdown" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="recover-data-after-unexpected-shutdown">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
On a restart after a crash, MongoDB replays all journal files in the
journal directory before the server becomes available. If MongoDB must
replay journal files,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
notes these events in the log
output.
</p>
<p class="leafygreen-ui-1kp3ins">
There is no reason to run
<code class="leafygreen-ui-1l06pbn">
--repair
</code>
.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-manage-journaling-change-wt-journal-compressor">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Change WiredTiger Journal Compressor
<a class="headerlink leafygreen-ui-1kepx47" href="#change-wiredtiger-journal-compressor" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-wiredtiger-journal-compressor">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
With the WiredTiger storage engine, MongoDB, by default, uses the
<code class="leafygreen-ui-1l06pbn">
snappy
</code>
compressor for the journal. To specify a different
compressions algorithm or no compression for 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-13vw3ie" role="note">
<h2 class="leafygreen-ui-1fn4p2b">
Tip
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
If you encounter an unclean shutdown for a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
during this procedure, you must use the old compressor settings to
recover using the journal files. Once recovered, you can retry the
procedure.
</p>
</div>
</div>
<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 replset/shardedcluster/standalone" aria-orientation="horizontal" class="leafygreen-ui-m8p1vb lg-ui-tab-list-0000" data-lgid="lg-tabs-tab_list" role="tablist">
<button aria-controls="_izwzo74kh" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_ydjl1qqjy" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
Standalone
</div>
</button>
<button aria-controls="_xc8gxxlft" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_shpdlsin9" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Replica Set Member
</div>
</button>
<button aria-controls="_208lo6zwu" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_myh7t5nbs" name="" role="tab" tabindex="-1">
<div class="leafygreen-ui-1nxuzmg">
Sharded Cluster Member
</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="_72xhkbjyw" class="" id="_7va8cw7c6" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
Use the following procedure to change the journal compressor
for 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:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Update
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-storage.wiredTiger.engineConfig.journalCompressor">
<code class="leafygreen-ui-1l06pbn">
storage.wiredTiger.engineConfig.journalCompressor
</code>
</a>
to the new value.
</p>
<p class="leafygreen-ui-1kp3ins">
If you use command-line options instead of a configuration file,
you must update the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--wiredTigerJournalCompressor">
<code class="leafygreen-ui-1l06pbn">
--wiredTigerJournalCompressor
</code>
</a>
command-line option during
the restart below.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Perform a clean shutdown of 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 example, 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 instance and issue
<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>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB('admin').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">getSiblingDB</span>(<span class="lg-highlight-string">'admin'</span>).<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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Once you have confirmed that the process is no longer running,
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:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If you are using a configuration file:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod -f <path>","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 -f <path<!-- -->/to/myconfig.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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If you are using command-line options instead of a configuration
file, update
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--wiredTigerJournalCompressor">
<code class="leafygreen-ui-1l06pbn">
--wiredTigerJournalCompressor
</code>
</a>
to the new value.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --wiredTigerJournalCompressor <differentcompressor|none> ...","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 --wiredTigerJournalCompressor <differentCompressor|none> ...</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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>
</li>
</ol>
</div>
</div>
</div>
<div>
<div aria-labelledby="_ohuomta17" class="leafygreen-ui-38lglc" id="_xllflfqvm" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
Use the following procedure to change the journal compressor
for a member of a replica set:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Perform a clean shutdown of 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 example, 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 instance and issue
<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>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB('admin').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">getSiblingDB</span>(<span class="lg-highlight-string">'admin'</span>).<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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Update
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-storage.wiredTiger.engineConfig.journalCompressor">
<code class="leafygreen-ui-1l06pbn">
storage.wiredTiger.engineConfig.journalCompressor
</code>
</a>
to the new value.
</p>
<p class="leafygreen-ui-1kp3ins">
If you use command-line options instead of a configuration file,
you must update the command-line options during the
restart below.
</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:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If you are using a configuration file:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod -f <path>","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 -f <path<!-- -->/to/myconfig.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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If you are using command-line options instead of a configuration
file, update
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--wiredTigerJournalCompressor">
<code class="leafygreen-ui-1l06pbn">
--wiredTigerJournalCompressor
</code>
</a>
to the new
value.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --wiredTigerJournalCompressor <differentcompressor|none> ...","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 --wiredTigerJournalCompressor <differentCompressor|none> ...</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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>
</li>
</ol>
</div>
</div>
</div>
<div>
<div aria-labelledby="_yipbz6ncy" class="leafygreen-ui-38lglc" id="_f0l0blgrl" role="tabpanel">
<div class="leafygreen-ui-1kdmo88">
<p class="leafygreen-ui-1kp3ins">
Use the following procedure to change the journal compressor
for a member of a shard replica set or config server replica set:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Perform a clean shutdown of 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 example, 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 instance and issue
<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>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB('admin').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">getSiblingDB</span>(<span class="lg-highlight-string">'admin'</span>).<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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Update
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-storage.wiredTiger.engineConfig.journalCompressor">
<code class="leafygreen-ui-1l06pbn">
storage.wiredTiger.engineConfig.journalCompressor
</code>
</a>
to the new value.
</p>
<p class="leafygreen-ui-1kp3ins">
If you use command-line options instead of a configuration file,
you must update the command-line options during the
restart below.
</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:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If you are using a configuration file:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod -f <path>","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 -f <path<!-- -->/to/myconfig.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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If you are using command-line options instead of a configuration
file, update
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--wiredTigerJournalCompressor">
<code class="leafygreen-ui-1l06pbn">
--wiredTigerJournalCompressor
</code>
</a>
to the new value.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --shardsvr --wiredTigerJournalCompressor <differentcompressor|none> --replSet ...","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 --shardsvr --wiredTigerJournalCompressor <differentCompressor|none> --replSet ...</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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>
</li>
</ol>
</div>
</div>
</div>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/cursor.map/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
cursor.map()
<a class="headerlink leafygreen-ui-1kepx47" href="#cursor.map--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.map--">
</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="#examples" target="_self">
<span>
Examples
</span>
</a>
</li>
</ul>
</div>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
cursor.map(function)
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-cursor.map" 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.map">
</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">
Applies a
<code class="leafygreen-ui-1l06pbn">
function
</code>
to each document visited by the cursor and
collects the return values from successive applications of the
<code class="leafygreen-ui-1l06pbn">
function
</code>
into a
<code class="leafygreen-ui-1l06pbn">
Cursor
</code>
object.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.map" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
cursor.map()
</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">
function
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
function
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A function to apply to each document visited by the cursor.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</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>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-cursor.map" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
cursor.map()
</code>
</span>
</a>
returns a
<code class="leafygreen-ui-1l06pbn">
Cursor
</code>
object. Note that
<code class="leafygreen-ui-1l06pbn">
.map()
</code>
only converts the type, it does not create a new cursor. You
can convert the
<code class="leafygreen-ui-1l06pbn">
Cursor
</code>
object to an
<code class="leafygreen-ui-1l06pbn">
Array
</code>
with
<code class="leafygreen-ui-1l06pbn">
.toArray()
</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">
These examples refer to the products collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.insertMany([\n { _id: 1, name: 'widget', price: 10.89 },\n { _id: 2, name: 'thing', price: 11.24 },\n { _id: 3, name: 'moppet', price: 8 },\n { _id: 4, name: 'cosa', price: 24.19 }\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">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">'widget'</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">10.89</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">'thing'</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">11.24</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">'moppet'</span>, <span class="lg-highlight-attr">price</span>: <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">4</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">'cosa'</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">24.19</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">
Return a Value From a Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#return-a-value-from-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="return-a-value-from-a-collection">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Get the product names.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.find().map( function(p) { return p.name; } ) ;","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">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">map</span>( <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">p</span>)<!-- --> <!-- -->{ <span class="lg-highlight-keyword">return</span> <!-- -->p.<span class="lg-highlight-property">name</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">
Return Results as an
<code class="leafygreen-ui-1l06pbn">
Array
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#return-results-as-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="return-results-as-an-array">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Calculate a discounted sale price and return the results as an array.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var salePrices = db.products.find().map( function(p) { return p.price * .9 } ).toArray() ;","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> salePrices <!-- -->= <!-- -->db.<span class="lg-highlight-property">products</span>.<span class="lg-highlight-title lg-highlight-function">find</span>(<!-- -->).<span class="lg-highlight-title lg-highlight-function">map</span>( <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">p</span>)<!-- --> <!-- -->{ <span class="lg-highlight-keyword">return</span> <!-- -->p.<span class="lg-highlight-property">price</span> <!-- -->* <span class="lg-highlight-number">.9</span> } ).<span class="lg-highlight-title lg-highlight-function">toArray</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">
Confirm that the output is an
<code class="leafygreen-ui-1l06pbn">
Array
</code>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"salePrices.constructor.name","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">salePrices.<span class="lg-highlight-property">constructor</span>.<span class="lg-highlight-property">name</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/method/cursor.forEach/#mongodb-method-cursor.forEach">
<code class="leafygreen-ui-1l06pbn">
cursor.forEach()
</code>
</a>
for similar functionality.
</p>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/data-modeling/schema-design-process/map-relationships/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Map Schema Relationships
<a class="headerlink leafygreen-ui-1kepx47" href="#map-schema-relationships" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="map-schema-relationships">
</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="#identify-related-data-in-your-schema" target="_self">
<span>
Identify related data in your schema
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#create-a-schema-map-for-your-related-data" target="_self">
<span>
Create a schema map for your related data
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#choose-whether-to-embed-related-data-or-use-references" target="_self">
<span>
Choose whether to embed related data or use references
</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="#optimize-queries-for-articles" target="_self">
<span>
Optimize Queries for Articles
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#optimize-queries-for-articles-and-authors" target="_self">
<span>
Optimize Queries for Articles and Authors
</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">
When you design your schema, consider how your application needs to query and
return related data. How you map relationships between data entities affects
your application's performance and scalability.
</p>
<p class="leafygreen-ui-1kp3ins">
The recommended way to handle related data is to embed it in a sub-document.
Embedding related data lets your application query the data it needs with a
single read operation and avoid slow
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/lookup/#mongodb-pipeline-pipe.-lookup">
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
</a>
operations.
</p>
<p class="leafygreen-ui-1kp3ins">
For some use cases, you can use a reference to point to related data in a
separate collection.
</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">
To determine if you should embed related data or use references,
consider the relative importance of the following goals for your
application:
</p>
<dl>
<dt>
Improve queries on related data
</dt>
<dd class="css-1ufxuu7">
If your application frequently queries one entity to return data about
another entity, embed the data to avoid the need for frequent
<code class="leafygreen-ui-1l06pbn">
$lookup
</code>
operations.
</dd>
<dt>
Improve data returned from different entities
</dt>
<dd class="css-1ufxuu7">
If your application returns data from related entities together,
embed the data in a single collection.
</dd>
<dt>
Improve update performance
</dt>
<dd class="css-1ufxuu7">
If your application frequently updates related data, consider
storing the data in its own collection and using a reference to access it.
When you use a reference, you reduce your application's write workload by only
needing to update the data in a single place.
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
To learn more about the benefits of embedded data and references, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/data-modeling/concepts/embedding-vs-references/#std-label-data-modeling-decisions">
Embedded Data Versus References
<!-- -->
.
</a>
</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">
Mapping relationships is the second step of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/data-modeling/schema-design-process/#std-label-data-modeling-schema-design">
schema design
process
</a>
. Before you map relationships,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/data-modeling/schema-design-process/identify-workload/#std-label-data-modeling-identify-workload">
identify your application's workload
</a>
to determine the data it needs.
</p>
</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":"Identify the data that your application queries and how entities\nrelate to each other.Consider the operations you identified from your application's\nworkload in the first step of the schema design process. Note the\ninformation these operations write and return, and what\ninformation overlaps between multiple operations.","name":"Identify related data in your schema"},{"@type":"HowToStep","text":"Your schema map should show related data fields and the type of\nrelationship between those fields (one-to-one, one-to-many,\nmany-to-many).Your schema map can resemble an entity-relationship model.","name":"Create a schema map for your related data"},{"@type":"HowToStep","text":"The decision to embed data or use references depends on your\napplication's common queries. Review the queries you identified in\nthe Identify Application Workload step and use the guidelines\nmentioned earlier on this page to design your schema to support\nfrequent and critical queries.Configure your databases, collections, and application logic to\nmatch the approach you choose.","name":"Choose whether to embed related data or use references"}],"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">
Identify related data in your schema
<a class="headerlink leafygreen-ui-1kepx47" href="#identify-related-data-in-your-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="identify-related-data-in-your-schema">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Identify the data that your application queries and how entities
relate to each other.
</p>
<p class="leafygreen-ui-1kp3ins">
Consider the operations you identified from your application's
workload in the first step of the schema design process. Note the
information these operations write and return, and what
information overlaps between multiple operations.
</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">
Create a schema map for your related data
<a class="headerlink leafygreen-ui-1kepx47" href="#create-a-schema-map-for-your-related-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="create-a-schema-map-for-your-related-data">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Your schema map should show related data fields and the type of
relationship between those fields (one-to-one, one-to-many,
many-to-many).
</p>
<p class="leafygreen-ui-1kp3ins">
Your schema map can resemble an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model" rel="noopener noreferrer">
<span>
entity-relationship model
<!-- -->
.
</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>
</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">
Choose whether to embed related data or use references
<a class="headerlink leafygreen-ui-1kepx47" href="#choose-whether-to-embed-related-data-or-use-references" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="choose-whether-to-embed-related-data-or-use-references">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The decision to embed data or use references depends on your
application's common queries. Review the queries you identified in
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/data-modeling/schema-design-process/identify-workload/#std-label-data-modeling-identify-workload">
Identify Application Workload
</a>
step and use the guidelines
mentioned earlier on this page to design your schema to support
frequent and critical queries.
</p>
<p class="leafygreen-ui-1kp3ins">
Configure your databases, collections, and application logic to
match the approach you choose.
</p>
</section>
</div>
</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>
<p class="leafygreen-ui-1kp3ins">
Consider the following schema map for a blog application:
</p>
<div class="figure css-5vkxs0" style="width:auto">
<div class="gatsby-image-wrapper gatsby-image-wrapper-constrained leafygreen-ui-12xaq6r" data-gatsby-image-wrapper="">
<div style="max-width:1482px;display:block">
<img alt="" aria-hidden="true" role="presentation" src="data:image/svg+xml;charset=utf-8,%3Csvg%20height='817'%20width='1482'%20xmlns='http://www.w3.org/2000/svg'%20version='1.1'%3E%3C/svg%3E" style="max-width:100%;display:block;position:static"/>
</div>
<div aria-hidden="true" data-placeholder-image="" style="opacity:1;transition:opacity 500ms linear">
</div>
<img alt="Schema map for a blog application" data-main-image="" decoding="async" height="817" loading="lazy" sizes="(min-width: 1482px) 1482px, 100vw" src="/docs/manual/static/cff9c2ce07f98a37acea08e825ead70d/77560/blog-schema-map.webp" srcset="/docs/manual/static/cff9c2ce07f98a37acea08e825ead70d/74842/blog-schema-map.webp 371w,/docs/manual/static/cff9c2ce07f98a37acea08e825ead70d/247f0/blog-schema-map.webp 741w,/docs/manual/static/cff9c2ce07f98a37acea08e825ead70d/77560/blog-schema-map.webp 1482w" style="--border-color:#C1C7C6;opacity:0" width="1482"/>
<noscript>
<img alt="Schema map for a blog application" data-gatsby-image-ssr="" data-main-image="" decoding="async" height="817" loading="lazy" sizes="(min-width: 1482px) 1482px, 100vw" src="/docs/manual/static/cff9c2ce07f98a37acea08e825ead70d/77560/blog-schema-map.webp" srcset="/docs/manual/static/cff9c2ce07f98a37acea08e825ead70d/74842/blog-schema-map.webp 371w,/docs/manual/static/cff9c2ce07f98a37acea08e825ead70d/247f0/blog-schema-map.webp 741w,/docs/manual/static/cff9c2ce07f98a37acea08e825ead70d/77560/blog-schema-map.webp 1482w" style="--border-color:#C1C7C6;opacity:0" width="1482"/>
</noscript>
<script type="module">
const t="undefined"!=typeof HTMLImageElement&&"loading"in HTMLImageElement.prototype;if(t){const t=document.querySelectorAll("img[data-main-image]");for(let e of t){e.dataset.src&&(e.setAttribute("src",e.dataset.src),e.removeAttribute("data-src")),e.dataset.srcset&&(e.setAttribute("srcset",e.dataset.srcset),e.removeAttribute("data-srcset"));const t=e.parentNode.querySelectorAll("source[data-srcset]");for(let e of t)e.setAttribute("srcset",e.dataset.srcset),e.removeAttribute("data-srcset");e.complete&&(e.style.opacity=1,e.parentNode.parentNode.querySelector("[data-placeholder-image]").style.opacity=0)}}
</script>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following examples show how to optimize your schema for different
queries depending on the needs of your application.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Optimize Queries for Articles
<a class="headerlink leafygreen-ui-1kepx47" href="#optimize-queries-for-articles" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-queries-for-articles">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If your application primarily queries articles for information such as title,
embed related information in the
<code class="leafygreen-ui-1l06pbn">
articles
</code>
collection to return all data
needed by the application in a single operation.
</p>
<p class="leafygreen-ui-1kp3ins">
The following document is optimized for queries on articles:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.articles.insertOne(\n {\n title: \"My Favorite Vacation\",\n date: ISODate(\"2023-06-02\"),\n text: \"We spent seven days in Italy...\",\n tags: [\n {\n name: \"travel\",\n url: \"<blog-site>/tags/travel\"\n },\n {\n name: \"adventure\",\n url: \"<blog-site>/tags/adventure\"\n }\n ],\n comments: [\n {\n name: \"pedro123\",\n text: \"Great article!\"\n }\n ],\n author: {\n name: \"alice123\",\n email: \"[email protected]\",\n avatar: \"photo1.jpg\"\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">articles</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-attr">title</span>: <span class="lg-highlight-string">"My Favorite Vacation"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2023-06-02"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">text</span>: <span class="lg-highlight-string">"We spent seven days in Italy..."</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">tags</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">"travel"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">url</span>: <span class="lg-highlight-string">"<blog-site>/tags/travel"</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">"adventure"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">url</span>: <span class="lg-highlight-string">"<blog-site>/tags/adventure"</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">comments</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">"pedro123"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">text</span>: <span class="lg-highlight-string">"Great article!"</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">author</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"alice123"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">email</span>: <span class="lg-highlight-string">"[email protected]"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">avatar</span>: <span class="lg-highlight-string">"photo1.jpg"</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>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Optimize Queries for Articles and Authors
<a class="headerlink leafygreen-ui-1kepx47" href="#optimize-queries-for-articles-and-authors" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-queries-for-articles-and-authors">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If your application returns article information and author information
separately, consider storing articles and authors in separate
collections. This schema design reduces the work required to return
author information, and lets you return only author information without
including unneeded fields.
</p>
<p class="leafygreen-ui-1kp3ins">
In the following schema, the
<code class="leafygreen-ui-1l06pbn">
articles
</code>
collection contains an
<code class="leafygreen-ui-1l06pbn">
authorId
</code>
field, which is a reference to the
<code class="leafygreen-ui-1l06pbn">
authors
</code>
collection.
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Articles Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#articles-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="articles-collection">
</div>
</a>
</h4>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.articles.insertOne(\n {\n title: \"My Favorite Vacation\",\n date: ISODate(\"2023-06-02\"),\n text: \"We spent seven days in Italy...\",\n authorId: 987,\n tags: [\n {\n name: \"travel\",\n url: \"<blog-site>/tags/travel\"\n },\n {\n name: \"adventure\",\n url: \"<blog-site>/tags/adventure\"\n }\n ],\n comments: [\n {\n name: \"pedro345\",\n text: \"Great article!\"\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">articles</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-attr">title</span>: <span class="lg-highlight-string">"My Favorite Vacation"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2023-06-02"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">text</span>: <span class="lg-highlight-string">"We spent seven days in Italy..."</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">authorId</span>: <span class="lg-highlight-number">987</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">tags</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">"travel"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">url</span>: <span class="lg-highlight-string">"<blog-site>/tags/travel"</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">"adventure"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">url</span>: <span class="lg-highlight-string">"<blog-site>/tags/adventure"</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">comments</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">"pedro345"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">text</span>: <span class="lg-highlight-string">"Great article!"</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>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Authors Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#authors-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="authors-collection">
</div>
</a>
</h4>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.authors.insertOne(\n {\n _id: 987,\n name: \"alice123\",\n email: \"[email protected]\",\n avatar: \"photo1.jpg\"\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">authors</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</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">_id</span>: <span class="lg-highlight-number">987</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"alice123"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">email</span>: <span class="lg-highlight-string">"[email protected]"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">avatar</span>: <span class="lg-highlight-string">"photo1.jpg"</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>
<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">
After you map relationships for your application's data, the next step
in the schema design process is to apply design patterns to optimize
your schema. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/data-modeling/schema-design-process/apply-patterns/#std-label-data-modeling-apply-patterns">
Apply Design Patterns
<!-- -->
.
</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">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/databases-and-collections/#std-label-databases-and-collections">
Databases and Collections in MongoDB
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/data-modeling/handle-duplicate-data/#std-label-data-modeling-duplicate-data">
Handle Duplicate Data
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/model-data-for-keyword-search/#std-label-data-model-example-keyword-search">
Model Data to Support Keyword Search
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/inconsistency-type/RoutingTableRangeGap/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
RoutingTableRangeGap
<a class="headerlink leafygreen-ui-1kepx47" href="#routingtablerangegap" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="routingtablerangegap">
</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">
RoutingTableRangeGap
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-data-RoutingTableRangeGap" 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-RoutingTableRangeGap">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
A range gap exists in the routing table between two consecutive chunks
for the same collection.
</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: \"RoutingTableRangeGap\",\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">"RoutingTableRangeGap"</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">
RoutingTableRangeGap
</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">
RoutingTableRangeGap
</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 has a gap 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 has a gap 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">
RoutingTableRangeGap
</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: \"RoutingTableRangeGap\",\n description: \"Found a gap between two 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">"RoutingTableRangeGap"</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 a gap between two 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/indexOfArray/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$indexOfArray (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-indexofarray--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="-indexofarray--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">
$indexOfArray
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-indexOfArray" 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.-indexOfArray">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Searches an array for an occurrence of a specified value and returns
the array index of the first occurrence. Array indexes start at zero.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-indexOfArray" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$indexOfArray
</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":"{ $indexOfArray: [ <array>, <search>, <start>, <end> ] }","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">$indexOfArray</span>: <!-- -->[ <!-- --><array expression>, <search expression>, <start>, <end> ] }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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>
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">
<array>
</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">
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 an array. 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>
<p class="leafygreen-ui-1kp3ins">
If the array expression 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.-indexOfArray" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$indexOfArray
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
null
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
If the array expression does not resolve to an array or
<code class="leafygreen-ui-1l06pbn">
null
</code>
nor
refers to a missing field,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-indexOfArray" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$indexOfArray
</code>
</span>
</a>
returns an
error.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
<search value>
</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>
Can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
. For
more information on expressions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
Expression Operators
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
<start>
</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>
<p class="leafygreen-ui-1kp3ins">
Optional. An integer, or a number that can be represented as integers (such as
2.0), that specifies the starting index position for the search. 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 non-negative integral number.
</p>
<p class="leafygreen-ui-1kp3ins">
If unspecified, the starting index position for the search is the
beginning of the string.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
<end>
</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>
<p class="leafygreen-ui-1kp3ins">
Optional. An integer, or a number that can be represented as integers (such as
2.0), that specifies the ending index position for the search. 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 non-negative integral number. If you specify a
<code class="leafygreen-ui-1l06pbn">
<end>
</code>
index value, you should also specify a
<code class="leafygreen-ui-1l06pbn">
<start>
</code>
index
value; otherwise,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-indexOfArray" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$indexOfArray
</code>
</span>
</a>
uses the
<code class="leafygreen-ui-1l06pbn">
<end>
</code>
value as the
<code class="leafygreen-ui-1l06pbn">
<start>
</code>
index value instead of the
<code class="leafygreen-ui-1l06pbn">
<end>
</code>
value.
</p>
<p class="leafygreen-ui-1kp3ins">
If unspecified, the ending index position for the search is the
end of the string.
</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">
If the
<code class="leafygreen-ui-1l06pbn">
<search expression>
</code>
is found multiple times within the
<code class="leafygreen-ui-1l06pbn">
<array expression>
</code>
, then
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-indexOfArray" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$indexOfArray
</code>
</span>
</a>
returns the
index of the first
<code class="leafygreen-ui-1l06pbn">
<search expression>
</code>
from the starting index
position.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-indexOfArray" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$indexOfArray
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
null
</code>
:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
<array expression>
</code>
is null, or
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
<array expression>
</code>
refers to a non-existing field in the input
document.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-indexOfArray" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$indexOfArray
</code>
</span>
</a>
returns an error:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
<array expression>
</code>
is not an array and not null, or
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
<start>
</code>
or
<code class="leafygreen-ui-1l06pbn">
<end>
</code>
is a negative integer (or a value that
can be represented as a negative integer, like -5.0).
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-indexOfArray" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$indexOfArray
</code>
</span>
</a>
returns
<code class="leafygreen-ui-1l06pbn">
-1
</code>
:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the <search expression> is not found in the array, or
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
<start>
</code>
is a number greater than
<code class="leafygreen-ui-1l06pbn">
<end>
</code>
, or
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
<start>
</code>
is a number greater than the length of the array.
</p>
</li>
</ul>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:95%"/>
<col style="width:5%"/>
</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">
{ $indexOfArray: [ [ "a", "abc" ], "a" ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<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">
{ $indexOfArray: [ [ "a", "abc", "de", ["de"] ], ["de"] ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
3
</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">
{ $indexOfArray: [ [ 1, 2 ], 5 ] }
</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="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $indexOfArray: [ [ 1, 2, 3 ], [1, 2] ] }
</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="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $indexOfArray: [ [ 10, 9, 9, 8, 9 ], 9, 3 ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
4
</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">
{ $indexOfArray: [ [ "a", "abc", "b" ], "b", 0, 1 ] }
</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="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $indexOfArray: [ [ "a", "abc", "b" ], "b", 1, 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="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $indexOfArray: [ [ "a", "abc", "b" ], "b", 20 ] }
</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="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $indexOfArray: [ [ null, null, null ], null ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<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">
{ $indexOfArray: [ null, "foo" ] }
</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="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
{ $indexOfArray: [ "foo", "foo" ] }
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Error
</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">
The example uses this
<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 { _id: 0, items: [ \"one\", \"two\", \"three\" ] },\n { _id: 1, items: [ 1, 2, 3 ] },\n { _id: 2, items: [ 1, 2, 3, 2 ] },\n { _id: 3, items: [ null, null, 2 ] },\n { _id: 4, items: [ 2, null, null, 2 ] },\n { _id: 5, items: null },\n { _id: 6, amount: 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">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-attr">_id</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">items</span>: <!-- -->[ <span class="lg-highlight-string">"one"</span>, <span class="lg-highlight-string">"two"</span>, <span class="lg-highlight-string">"three"</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">items</span>: <!-- -->[ <span class="lg-highlight-number">1</span>, <span class="lg-highlight-number">2</span>, <span class="lg-highlight-number">3</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">items</span>: <!-- -->[ <span class="lg-highlight-number">1</span>, <span class="lg-highlight-number">2</span>, <span class="lg-highlight-number">3</span>, <span class="lg-highlight-number">2</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">items</span>: <!-- -->[ <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-number">2</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">items</span>: <!-- -->[ <span class="lg-highlight-number">2</span>, <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-number">2</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">items</span>: <span class="lg-highlight-literal">null</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">6</span>, <span class="lg-highlight-attr">amount</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 example uses
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-indexOfArray" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$indexOfArray
</code>
</span>
</a>
to find
<code class="leafygreen-ui-1l06pbn">
2
</code>
in
the
<code class="leafygreen-ui-1l06pbn">
items
</code>
array:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.inventory.aggregate( [ {\n $project: {\n index: { $indexOfArray: [ \"$items\", 2 ] }\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"> <span class="lg-highlight-attr">$project</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">index</span>: <!-- -->{ <span class="lg-highlight-attr">$indexOfArray</span>: <!-- -->[ <span class="lg-highlight-string">"$items"</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></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 returns:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The first array index for the value
<code class="leafygreen-ui-1l06pbn">
2
</code>
in each
<code class="leafygreen-ui-1l06pbn">
items
</code>
array, if
found. Array indexes start at
<code class="leafygreen-ui-1l06pbn">
0
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
-1
</code>
for the index if
<code class="leafygreen-ui-1l06pbn">
2
</code>
is not in the
<code class="leafygreen-ui-1l06pbn">
items
</code>
array.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
null
</code>
for the index if
<code class="leafygreen-ui-1l06pbn">
items
</code>
is not an array or
<code class="leafygreen-ui-1l06pbn">
items
</code>
does
not exist.
</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 { _id: 0, index: -1 },\n { _id: 1, index: 1 },\n { _id: 2, index: 1 },\n { _id: 3, index: 2 },\n { _id: 4, index: 0 },\n { _id: 5, index: null },\n { _id: 6, index: null }\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>, <span class="lg-highlight-attr">index</span>: <!-- -->-<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">1</span>, <span class="lg-highlight-attr">index</span>: <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">2</span>, <span class="lg-highlight-attr">index</span>: <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">index</span>: <span class="lg-highlight-number">2</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">index</span>: <span class="lg-highlight-number">0</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">index</span>: <span class="lg-highlight-literal">null</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">6</span>, <span class="lg-highlight-attr">index</span>: <span class="lg-highlight-literal">null</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</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">
<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/indexOfBytes/#mongodb-expression-exp.-indexOfBytes">
<code class="leafygreen-ui-1l06pbn">
$indexOfBytes
</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/indexOfCP/#mongodb-expression-exp.-indexOfCP">
<code class="leafygreen-ui-1l06pbn">
$indexOfCP
</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/in/#mongodb-expression-exp.-in">
<code class="leafygreen-ui-1l06pbn">
$in
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/abs/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$abs (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-abs--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="-abs--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">
$abs
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-abs" 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.-abs">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns the absolute value of a number.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-abs" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$abs
</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":"{ $abs: <number> }","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">$abs</span>: <<!-- -->number<!-- -->> <!-- -->}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
<number>
</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 number. 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">
If the argument resolves to a value of
<code class="leafygreen-ui-1l06pbn">
null
</code>
or refers to a field that is
missing,
<code class="leafygreen-ui-1l06pbn">
$abs
</code>
returns
<code class="leafygreen-ui-1l06pbn">
null
</code>
. If the argument resolves to
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
,
<code class="leafygreen-ui-1l06pbn">
$abs
</code>
returns
<code class="leafygreen-ui-1l06pbn">
NaN
</code>
.
</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">
{ $abs: -1 }
</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">
{ $abs: 1 }
</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">
{ $abs: 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>
</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
<code class="leafygreen-ui-1l06pbn">
temperatureChange
</code>
contains the following documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.temperatureChange.insertMany( [\n { _id: 1, startTemp: 50, endTemp: 80 },\n { _id: 2, startTemp: 40, endTemp: 40 },\n { _id: 3, startTemp: 90, endTemp: 70 },\n { _id: 4, startTemp: 60, endTemp: 70 }\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">temperatureChange</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">startTemp</span>: <span class="lg-highlight-number">50</span>, <span class="lg-highlight-attr">endTemp</span>: <span class="lg-highlight-number">80</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">startTemp</span>: <span class="lg-highlight-number">40</span>, <span class="lg-highlight-attr">endTemp</span>: <span class="lg-highlight-number">40</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">startTemp</span>: <span class="lg-highlight-number">90</span>, <span class="lg-highlight-attr">endTemp</span>: <span class="lg-highlight-number">70</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">startTemp</span>: <span class="lg-highlight-number">60</span>, <span class="lg-highlight-attr">endTemp</span>: <span class="lg-highlight-number">70</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 calculates the magnitude of difference between
the
<code class="leafygreen-ui-1l06pbn">
startTemp
</code>
and
<code class="leafygreen-ui-1l06pbn">
endTemp
</code>
ratings:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.temperatureChange.aggregate([\n {\n $project: { delta: { $abs: { $subtract: [ \"$startTemp\", \"$endTemp\" ] } } }\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">temperatureChange</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>: <!-- -->{ <span class="lg-highlight-attr">delta</span>: <!-- -->{ <span class="lg-highlight-attr">$abs</span>: <!-- -->{ <span class="lg-highlight-attr">$subtract</span>: <!-- -->[ <span class="lg-highlight-string">"$startTemp"</span>, <span class="lg-highlight-string">"$endTemp"</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 results:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"delta\" : 30 }\n{ \"_id\" : 2, \"delta\" : 0 }\n{ \"_id\" : 3, \"delta\" : 20 }\n{ \"_id\" : 4, \"delta\" : 10 }","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">"delta"</span> <!-- -->: <span class="lg-highlight-number">30</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">"delta"</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-number">3</span>, <span class="lg-highlight-string">"delta"</span> <!-- -->: <span class="lg-highlight-number">20</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">"delta"</span> <!-- -->: <span class="lg-highlight-number">10</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/strLenBytes/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$strLenBytes (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-strlenbytes--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="-strlenbytes--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">
$strLenBytes
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-strLenBytes" 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.-strLenBytes">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns the number of UTF-8 encoded bytes in the specified string.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-strLenBytes" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$strLenBytes
</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":"{ $strLenBytes: <string> }","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">$strLenBytes</span>: <span class="lg-highlight-tag lg-highlight-xml"><</span><span class="lg-highlight-name lg-highlight-tag lg-highlight-xml">string</span><span class="lg-highlight-tag lg-highlight-xml"> </span><span class="lg-highlight-attr lg-highlight-tag lg-highlight-xml">expression</span><span class="lg-highlight-tag lg-highlight-xml">></span><span class="lg-highlight-xml"> }</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 argument 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 string. 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>
<p class="leafygreen-ui-1kp3ins">
If the argument resolves to a value of
<code class="leafygreen-ui-1l06pbn">
null
</code>
or refers to a
missing field,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-strLenBytes" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$strLenBytes
</code>
</span>
</a>
returns an error.
</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">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-strLenBytes" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$strLenBytes
</code>
</span>
</a>
operator counts the number of UTF-8
encoded bytes in a string where each character may use between one
and four bytes.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, US-ASCII characters are encoded using one byte. Characters
with diacritic markings and additional Latin alphabetical characters
(Latin characters outside of the English alphabet) are encoded
using two bytes. Chinese, Japanese and Korean characters typically
require three bytes, and other planes of unicode (emoji, mathematical
symbols, etc.) require four bytes.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-strLenBytes" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$strLenBytes
</code>
</span>
</a>
operator differs from
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/strLenCP/#mongodb-expression-exp.-strLenCP">
<code class="leafygreen-ui-1l06pbn">
$strLenCP
</code>
</a>
operator which counts the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="http://www.unicode.org/glossary/#code_point" rel="noopener noreferrer">
<span>
code points
</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>
in the specified string regardless of how many bytes each character
uses.
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:40%"/>
<col style="width:15%"/>
<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>
Example
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Results
</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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $strLenBytes: \"abcde\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$strLenBytes</span>: <span class="lg-highlight-string">"abcde"</span> }</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">
5
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Each character is encoded using one byte.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $strLenBytes: \"Hello World!\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$strLenBytes</span>: <span class="lg-highlight-string">"Hello World!"</span> }</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">
12
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Each character is encoded using one byte.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $strLenBytes: \"cafeteria\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$strLenBytes</span>: <span class="lg-highlight-string">"cafeteria"</span> }</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">
9
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Each character is encoded using one byte.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $strLenBytes: \"cafétéria\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$strLenBytes</span>: <span class="lg-highlight-string">"cafétéria"</span> }</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">
11
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
é
</code>
is encoded using two bytes.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $strLenBytes: \"\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$strLenBytes</span>: <span class="lg-highlight-string">""</span> }</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">
0
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Empty strings return 0.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $strLenBytes: \"$€λG\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$strLenBytes</span>: <span class="lg-highlight-string">"$€λG"</span> }</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">
7
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
€
</code>
is encoded using three bytes.
<code class="leafygreen-ui-1l06pbn">
λ
</code>
is encoded using two bytes.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $strLenBytes: \"寿司\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$strLenBytes</span>: <span class="lg-highlight-string">"寿司"</span> }</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">
6
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Each character is encoded using three bytes.
</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>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Single-Byte and Multibyte Character Set
<a class="headerlink leafygreen-ui-1kepx47" href="#single-byte-and-multibyte-character-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="single-byte-and-multibyte-character-set">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Create a
<code class="leafygreen-ui-1l06pbn">
food
</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.food.insertMany(\n [\n { \"_id\" : 1, \"name\" : \"apple\" },\n { \"_id\" : 2, \"name\" : \"banana\" },\n { \"_id\" : 3, \"name\" : \"éclair\" },\n { \"_id\" : 4, \"name\" : \"hamburger\" },\n { \"_id\" : 5, \"name\" : \"jalapeño\" },\n { \"_id\" : 6, \"name\" : \"pizza\" },\n { \"_id\" : 7, \"name\" : \"tacos\" },\n { \"_id\" : 8, \"name\" : \"寿司\" }\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">food</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-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"apple"</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">"banana"</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">"éclair"</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">"hamburger"</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">"jalapeño"</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">"pizza"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">7</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"tacos"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">8</span>, <span class="lg-highlight-string">"name"</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></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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
<code class="leafygreen-ui-1l06pbn">
$strLenBytes
</code>
operator to calculate
the
<code class="leafygreen-ui-1l06pbn">
length
</code>
of each
<code class="leafygreen-ui-1l06pbn">
name
</code>
value:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.food.aggregate(\n [\n {\n $project: {\n \"name\": 1,\n \"length\": { $strLenBytes: \"$name\" }\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">food</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-string">"name"</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"length"</span>: <!-- -->{ <span class="lg-highlight-attr">$strLenBytes</span>: <span class="lg-highlight-string">"$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><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\" : \"apple\", \"length\" : 5 }\n{ \"_id\" : 2, \"name\" : \"banana\", \"length\" : 6 }\n{ \"_id\" : 3, \"name\" : \"éclair\", \"length\" : 7 }\n{ \"_id\" : 4, \"name\" : \"hamburger\", \"length\" : 9 }\n{ \"_id\" : 5, \"name\" : \"jalapeño\", \"length\" : 9 }\n{ \"_id\" : 6, \"name\" : \"pizza\", \"length\" : 5 }\n{ \"_id\" : 7, \"name\" : \"tacos\", \"length\" : 5 }\n{ \"_id\" : 8, \"name\" : \"寿司\", \"length\" : 6 }","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">"apple"</span>, <span class="lg-highlight-string">"length"</span> <!-- -->: <span class="lg-highlight-number">5</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">"banana"</span>, <span class="lg-highlight-string">"length"</span> <!-- -->: <span class="lg-highlight-number">6</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">"éclair"</span>, <span class="lg-highlight-string">"length"</span> <!-- -->: <span class="lg-highlight-number">7</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">"hamburger"</span>, <span class="lg-highlight-string">"length"</span> <!-- -->: <span class="lg-highlight-number">9</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">"jalapeño"</span>, <span class="lg-highlight-string">"length"</span> <!-- -->: <span class="lg-highlight-number">9</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">"pizza"</span>, <span class="lg-highlight-string">"length"</span> <!-- -->: <span class="lg-highlight-number">5</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">7</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"tacos"</span>, <span class="lg-highlight-string">"length"</span> <!-- -->: <span class="lg-highlight-number">5</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">8</span>, <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"寿司"</span>, <span class="lg-highlight-string">"length"</span> <!-- -->: <span class="lg-highlight-number">6</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 documents with
<code class="leafygreen-ui-1l06pbn">
_id: 3
</code>
and
<code class="leafygreen-ui-1l06pbn">
_id: 5
</code>
each contain a diacritic
character (
<code class="leafygreen-ui-1l06pbn">
é
</code>
and
<code class="leafygreen-ui-1l06pbn">
ñ
</code>
respectively) that requires two bytes to
encode. The document with
<code class="leafygreen-ui-1l06pbn">
_id: 8
</code>
contains two Japanese characters
that are encoded using three bytes each. This makes the
<code class="leafygreen-ui-1l06pbn">
length
</code>
greater than the number of characters in
<code class="leafygreen-ui-1l06pbn">
name
</code>
for the documents
with
<code class="leafygreen-ui-1l06pbn">
_id: 3
</code>
,
<code class="leafygreen-ui-1l06pbn">
_id: 5
</code>
and
<code class="leafygreen-ui-1l06pbn">
_id: 8
</code>
.
</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/operator/aggregation/strLenCP/#mongodb-expression-exp.-strLenCP">
<code class="leafygreen-ui-1l06pbn">
$strLenCP
</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/binarySize/#mongodb-expression-exp.-binarySize">
<code class="leafygreen-ui-1l06pbn">
$binarySize
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/facet/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$facet (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-facet--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="-facet--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="#considerations" target="_self">
<span>
Considerations
</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="pipeline">
<dt>
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-pipeline-pipe.-facet" 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.-facet">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Processes multiple
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline/#std-label-aggregation-pipeline">
aggregation pipelines
</a>
within a single stage on the same set of
input documents. Each sub-pipeline has its own field in the output
document where its results are stored as an array of documents.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
stage allows you to create multi-faceted aggregations
which characterize data across multiple dimensions, or facets,
within a single aggregation stage. Multi-faceted aggregations
provide multiple filters and categorizations to guide data browsing
and analysis. Retailers commonly use faceting to narrow search results
by creating filters on product price, manufacturer, size, etc.
</p>
<p class="leafygreen-ui-1kp3ins">
Input documents are passed to the
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
stage only once.
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
enables various aggregations on the same set of input
documents, without needing to retrieve the input documents multiple
times.
</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">
$facet
</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
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
stage has the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $facet:\n {\n <outputfield1>: [ <stage1>, <stage2>, ... ],\n <outputfield2>: [ <stage1>, <stage2>, ... ],\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-attr">$facet</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <<!-- -->outputField1<!-- -->><!-- -->: <!-- -->[ <<!-- -->stage1<!-- -->><!-- -->,<!-- --> <!-- --><<!-- -->stage2<!-- -->><!-- -->,<!-- --> <!-- -->...<!-- --> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <<!-- -->outputField2<!-- -->><!-- -->: <!-- -->[ <<!-- -->stage1<!-- -->><!-- -->,<!-- --> <!-- --><<!-- -->stage2<!-- -->><!-- -->,<!-- --> <!-- -->...<!-- --> ]<!-- -->,</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">}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 the output field name for each specified pipeline.
</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">
As each stage in a
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
executes, the resulting document is
limited to 100 megabytes. Note the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/aggregate/#std-label-aggregate-cmd-allowDiskUse">
allowDiskUse
</a>
flag doesn't affect the 100 megabyte size
limit, since
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
can't spill to disk.
</p>
<p class="leafygreen-ui-1kp3ins">
The final output document is subject to the 16 megabyte
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/limits/#mongodb-limit-BSON-Document-Size">
BSON
document size limit
</a>
. If it exceeds 16 megabytes,
the aggregation produces an error.
</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/aggregation-pipeline-limits/#std-label-agg-pipeline-limits">
Aggregation Pipeline Limits
</a>
</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">
Facet-related aggregation stages categorize and group incoming
documents. Specify any of the following facet-related stages within
different
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
sub-pipeline's
<code class="leafygreen-ui-1l06pbn">
<stage>
</code>
to perform a
multi-faceted aggregation:
</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/bucket/#mongodb-pipeline-pipe.-bucket">
<code class="leafygreen-ui-1l06pbn">
$bucket
</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/bucketAuto/#mongodb-pipeline-pipe.-bucketAuto">
<code class="leafygreen-ui-1l06pbn">
$bucketAuto
</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/sortByCount/#mongodb-pipeline-pipe.-sortByCount">
<code class="leafygreen-ui-1l06pbn">
$sortByCount
</code>
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Other
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation-pipeline/#std-label-aggregation-pipeline-operator-reference">
aggregation stages
</a>
can
also be used with
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
with the following exceptions:
</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/collStats/#mongodb-pipeline-pipe.-collStats">
<code class="leafygreen-ui-1l06pbn">
$collStats
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
</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/operator/aggregation/indexStats/#mongodb-pipeline-pipe.-indexStats">
<code class="leafygreen-ui-1l06pbn">
$indexStats
</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/out/#mongodb-pipeline-pipe.-out">
<code class="leafygreen-ui-1l06pbn">
$out
</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/merge/#mongodb-pipeline-pipe.-merge">
<code class="leafygreen-ui-1l06pbn">
$merge
</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/planCacheStats/#mongodb-pipeline-pipe.-planCacheStats">
<code class="leafygreen-ui-1l06pbn">
$planCacheStats
</code>
</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/atlas/atlas-vector-search/vector-search-stage/#mongodb-pipeline-pipe.-vectorSearch" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$vectorSearch
</code>
</span>
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Each sub-pipeline within
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
is passed the exact same set of
input documents. These sub-pipelines are completely independent of one
another and the document array output by each is stored in separate
fields in the output document. The output of one sub-pipeline can not
be used as the input for a different sub-pipeline within the same
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
stage. If further aggregations are required, add additional
stages after
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
and specify the field name,
<code class="leafygreen-ui-1l06pbn">
<outputField>
</code>
,
of the desired sub-pipeline output.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Index Use
<a class="headerlink leafygreen-ui-1kepx47" href="#index-use" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-use">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Pipeline order determines how the
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
stage uses indexes.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
stage is the first stage in a pipeline, the stage
will perform a
<code class="leafygreen-ui-1l06pbn">
COLLSCAN
</code>
. The
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
stage does not make use of
indexes if it is the first stage in the pipeline.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
stage comes later in the pipeline and earlier stages
have used indexes,
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
will not trigger a
<code class="leafygreen-ui-1l06pbn">
COLLSCAN
</code>
during
execution.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For example,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match">
<code class="leafygreen-ui-1l06pbn">
$match
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sort/#mongodb-pipeline-pipe.-sort">
<code class="leafygreen-ui-1l06pbn">
$sort
</code>
</a>
stages that come
before a
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
stage can make use of indexes and the
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
will not trigger a
<code class="leafygreen-ui-1l06pbn">
COLLSCAN
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
For optimization suggestions, see:
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/aggregation-pipeline-optimization/#std-label-agg-pipeline-optimization">
Aggregation Pipeline Optimization
<!-- -->
.
</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 an online store whose inventory is stored in the following
<code class="leafygreen-ui-1l06pbn">
artwork
</code>
collection:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 1, \"title\" : \"The Pillars of Society\", \"artist\" : \"Grosz\", \"year\" : 1926,\n \"price\" : NumberDecimal(\"199.99\"),\n \"tags\" : [ \"painting\", \"satire\", \"Expressionism\", \"caricature\" ] }\n{ \"_id\" : 2, \"title\" : \"Melancholy III\", \"artist\" : \"Munch\", \"year\" : 1902,\n \"price\" : NumberDecimal(\"280.00\"),\n \"tags\" : [ \"woodcut\", \"Expressionism\" ] }\n{ \"_id\" : 3, \"title\" : \"Dancer\", \"artist\" : \"Miro\", \"year\" : 1925,\n \"price\" : NumberDecimal(\"76.04\"),\n \"tags\" : [ \"oil\", \"Surrealism\", \"painting\" ] }\n{ \"_id\" : 4, \"title\" : \"The Great Wave off Kanagawa\", \"artist\" : \"Hokusai\",\n \"price\" : NumberDecimal(\"167.30\"),\n \"tags\" : [ \"woodblock\", \"ukiyo-e\" ] }\n{ \"_id\" : 5, \"title\" : \"The Persistence of Memory\", \"artist\" : \"Dali\", \"year\" : 1931,\n \"price\" : NumberDecimal(\"483.00\"),\n \"tags\" : [ \"Surrealism\", \"painting\", \"oil\" ] }\n{ \"_id\" : 6, \"title\" : \"Composition VII\", \"artist\" : \"Kandinsky\", \"year\" : 1913,\n \"price\" : NumberDecimal(\"385.00\"),\n \"tags\" : [ \"oil\", \"painting\", \"abstract\" ] }\n{ \"_id\" : 7, \"title\" : \"The Scream\", \"artist\" : \"Munch\", \"year\" : 1893,\n \"tags\" : [ \"Expressionism\", \"painting\", \"oil\" ] }\n{ \"_id\" : 8, \"title\" : \"Blue Flower\", \"artist\" : \"O'Keefe\", \"year\" : 1918,\n \"price\" : NumberDecimal(\"118.42\"),\n \"tags\" : [ \"abstract\", \"painting\" ] }","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">"title"</span> <!-- -->: <span class="lg-highlight-string">"The Pillars of Society"</span>, <span class="lg-highlight-string">"artist"</span> <!-- -->: <span class="lg-highlight-string">"Grosz"</span>, <span class="lg-highlight-string">"year"</span> <!-- -->: <span class="lg-highlight-number">1926</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"199.99"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tags"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"painting"</span>, <span class="lg-highlight-string">"satire"</span>, <span class="lg-highlight-string">"Expressionism"</span>, <span class="lg-highlight-string">"caricature"</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">"title"</span> <!-- -->: <span class="lg-highlight-string">"Melancholy III"</span>, <span class="lg-highlight-string">"artist"</span> <!-- -->: <span class="lg-highlight-string">"Munch"</span>, <span class="lg-highlight-string">"year"</span> <!-- -->: <span class="lg-highlight-number">1902</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"280.00"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tags"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"woodcut"</span>, <span class="lg-highlight-string">"Expressionism"</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">"title"</span> <!-- -->: <span class="lg-highlight-string">"Dancer"</span>, <span class="lg-highlight-string">"artist"</span> <!-- -->: <span class="lg-highlight-string">"Miro"</span>, <span class="lg-highlight-string">"year"</span> <!-- -->: <span class="lg-highlight-number">1925</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"76.04"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tags"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"oil"</span>, <span class="lg-highlight-string">"Surrealism"</span>, <span class="lg-highlight-string">"painting"</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">"title"</span> <!-- -->: <span class="lg-highlight-string">"The Great Wave off Kanagawa"</span>, <span class="lg-highlight-string">"artist"</span> <!-- -->: <span class="lg-highlight-string">"Hokusai"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"167.30"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tags"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"woodblock"</span>, <span class="lg-highlight-string">"ukiyo-e"</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">"title"</span> <!-- -->: <span class="lg-highlight-string">"The Persistence of Memory"</span>, <span class="lg-highlight-string">"artist"</span> <!-- -->: <span class="lg-highlight-string">"Dali"</span>, <span class="lg-highlight-string">"year"</span> <!-- -->: <span class="lg-highlight-number">1931</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"483.00"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tags"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Surrealism"</span>, <span class="lg-highlight-string">"painting"</span>, <span class="lg-highlight-string">"oil"</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">"title"</span> <!-- -->: <span class="lg-highlight-string">"Composition VII"</span>, <span class="lg-highlight-string">"artist"</span> <!-- -->: <span class="lg-highlight-string">"Kandinsky"</span>, <span class="lg-highlight-string">"year"</span> <!-- -->: <span class="lg-highlight-number">1913</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"385.00"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tags"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"oil"</span>, <span class="lg-highlight-string">"painting"</span>, <span class="lg-highlight-string">"abstract"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">7</span>, <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"The Scream"</span>, <span class="lg-highlight-string">"artist"</span> <!-- -->: <span class="lg-highlight-string">"Munch"</span>, <span class="lg-highlight-string">"year"</span> <!-- -->: <span class="lg-highlight-number">1893</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tags"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"Expressionism"</span>, <span class="lg-highlight-string">"painting"</span>, <span class="lg-highlight-string">"oil"</span> ] }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">8</span>, <span class="lg-highlight-string">"title"</span> <!-- -->: <span class="lg-highlight-string">"Blue Flower"</span>, <span class="lg-highlight-string">"artist"</span> <!-- -->: <span class="lg-highlight-string">"O'Keefe"</span>, <span class="lg-highlight-string">"year"</span> <!-- -->: <span class="lg-highlight-number">1918</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">NumberDecimal</span>(<span class="lg-highlight-string">"118.42"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"tags"</span> <!-- -->: <!-- -->[ <span class="lg-highlight-string">"abstract"</span>, <span class="lg-highlight-string">"painting"</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 operation uses MongoDB's faceting features to provide
customers with the store's inventory categorized across multiple
dimensions such as tags, price, and year created. This
<code class="leafygreen-ui-1l06pbn">
$facet
</code>
stage has three sub-pipelines that use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sortByCount/#mongodb-pipeline-pipe.-sortByCount">
<code class="leafygreen-ui-1l06pbn">
$sortByCount
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/bucket/#mongodb-pipeline-pipe.-bucket">
<code class="leafygreen-ui-1l06pbn">
$bucket
</code>
</a>
, or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/bucketAuto/#mongodb-pipeline-pipe.-bucketAuto">
<code class="leafygreen-ui-1l06pbn">
$bucketAuto
</code>
</a>
to perform this multi-faceted aggregation.
The input documents from
<code class="leafygreen-ui-1l06pbn">
artwork
</code>
are fetched from the database only
once, at the beginning of the operation:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.artwork.aggregate( [\n {\n $facet: {\n \"categorizedByTags\": [\n { $unwind: \"$tags\" },\n { $sortByCount: \"$tags\" }\n ],\n \"categorizedByPrice\": [\n // Filter out documents without a price e.g., _id: 7\n { $match: { price: { $exists: 1 } } },\n {\n $bucket: {\n groupBy: \"$price\",\n boundaries: [ 0, 150, 200, 300, 400 ],\n default: \"Other\",\n output: {\n \"count\": { $sum: 1 },\n \"titles\": { $push: \"$title\" }\n }\n }\n }\n ],\n \"categorizedByYears(Auto)\": [\n {\n $bucketAuto: {\n groupBy: \"$year\",\n buckets: 4\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">artwork</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">$facet</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"categorizedByTags"</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$unwind</span>: <span class="lg-highlight-string">"$tags"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$sortByCount</span>: <span class="lg-highlight-string">"$tags"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"categorizedByPrice"</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> Filter out documents </span><span class="lg-highlight-comment">without a price e.g., _id: 7</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$match</span>: <!-- -->{ <span class="lg-highlight-attr">price</span>: <!-- -->{ <span class="lg-highlight-attr">$exists</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">$bucket</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">groupBy</span>: <span class="lg-highlight-string">"$price"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">boundaries</span>: <!-- -->[ <span class="lg-highlight-number">0</span>, <span class="lg-highlight-number">150</span>, <span class="lg-highlight-number">200</span>, <span class="lg-highlight-number">300</span>, <span class="lg-highlight-number">400</span> ]<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">default</span>: <span class="lg-highlight-string">"Other"</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-string">"count"</span>: <!-- -->{ <span class="lg-highlight-attr">$sum</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"titles"</span>: <!-- -->{ <span class="lg-highlight-attr">$push</span>: <span class="lg-highlight-string">"$title"</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"> <span class="lg-highlight-string">"categorizedByYears(Auto)"</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$bucketAuto</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">groupBy</span>: <span class="lg-highlight-string">"$year"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">buckets</span>: <span class="lg-highlight-number">4</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">
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 \"categorizedByYears(Auto)\" : [\n // First bucket includes the document without a year, e.g., _id: 4\n { \"_id\" : { \"min\" : null, \"max\" : 1902 }, \"count\" : 2 },\n { \"_id\" : { \"min\" : 1902, \"max\" : 1918 }, \"count\" : 2 },\n { \"_id\" : { \"min\" : 1918, \"max\" : 1926 }, \"count\" : 2 },\n { \"_id\" : { \"min\" : 1926, \"max\" : 1931 }, \"count\" : 2 }\n ],\n \"categorizedByPrice\" : [\n {\n \"_id\" : 0,\n \"count\" : 2,\n \"titles\" : [\n \"Dancer\",\n \"Blue Flower\"\n ]\n },\n {\n \"_id\" : 150,\n \"count\" : 2,\n \"titles\" : [\n \"The Pillars of Society\",\n \"The Great Wave off Kanagawa\"\n ]\n },\n {\n \"_id\" : 200,\n \"count\" : 1,\n \"titles\" : [\n \"Melancholy III\"\n ]\n },\n {\n \"_id\" : 300,\n \"count\" : 1,\n \"titles\" : [\n \"Composition VII\"\n ]\n },\n {\n // Includes document price outside of bucket boundaries, e.g., _id: 5\n \"_id\" : \"Other\",\n \"count\" : 1,\n \"titles\" : [\n \"The Persistence of Memory\"\n ]\n }\n ],\n \"categorizedByTags\" : [\n { \"_id\" : \"painting\", \"count\" : 6 },\n { \"_id\" : \"oil\", \"count\" : 4 },\n { \"_id\" : \"Expressionism\", \"count\" : 3 },\n { \"_id\" : \"Surrealism\", \"count\" : 2 },\n { \"_id\" : \"abstract\", \"count\" : 2 },\n { \"_id\" : \"woodblock\", \"count\" : 1 },\n { \"_id\" : \"woodcut\", \"count\" : 1 },\n { \"_id\" : \"ukiyo-e\", \"count\" : 1 },\n { \"_id\" : \"satire\", \"count\" : 1 },\n { \"_id\" : \"caricature\", \"count\" : 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">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"categorizedByYears(Auto)"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> First bucket includes </span><span class="lg-highlight-comment">the</span><span class="lg-highlight-comment"> document without a </span><span class="lg-highlight-comment">year, e.g., _id: 4</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"min"</span> <!-- -->: <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-string">"max"</span> <!-- -->: <span class="lg-highlight-number">1902</span> }<!-- -->, <span class="lg-highlight-string">"count"</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-string">"min"</span> <!-- -->: <span class="lg-highlight-number">1902</span>, <span class="lg-highlight-string">"max"</span> <!-- -->: <span class="lg-highlight-number">1918</span> }<!-- -->, <span class="lg-highlight-string">"count"</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-string">"min"</span> <!-- -->: <span class="lg-highlight-number">1918</span>, <span class="lg-highlight-string">"max"</span> <!-- -->: <span class="lg-highlight-number">1926</span> }<!-- -->, <span class="lg-highlight-string">"count"</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-string">"min"</span> <!-- -->: <span class="lg-highlight-number">1926</span>, <span class="lg-highlight-string">"max"</span> <!-- -->: <span class="lg-highlight-number">1931</span> }<!-- -->, <span class="lg-highlight-string">"count"</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"> <span class="lg-highlight-string">"categorizedByPrice"</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">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"titles"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Dancer"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Blue Flower"</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">150</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"titles"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"The Pillars of Society"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"The Great Wave off Kanagawa"</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">200</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"titles"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Melancholy III"</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">300</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"titles"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Composition VII"</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-comment">//</span><span class="lg-highlight-comment"> Includes document price </span><span class="lg-highlight-comment">outside of bucket boundaries, e.g., _id: 5</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Other"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"titles"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"The Persistence of Memory"</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">"categorizedByTags"</span> <!-- -->: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"painting"</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">6</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"oil"</span>, <span class="lg-highlight-string">"count"</span> <!-- -->: <span class="lg-highlight-number">4</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"Expressionism"</span>, <span class="lg-highlight-string">"count"</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-string">"Surrealism"</span>, <span class="lg-highlight-string">"count"</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-string">"abstract"</span>, <span class="lg-highlight-string">"count"</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-string">"woodblock"</span>, <span class="lg-highlight-string">"count"</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-string">"woodcut"</span>, <span class="lg-highlight-string">"count"</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-string">"ukiyo-e"</span>, <span class="lg-highlight-string">"count"</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-string">"satire"</span>, <span class="lg-highlight-string">"count"</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-string">"caricature"</span>, <span class="lg-highlight-string">"count"</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>
|
https://www.mongodb.com/docs/manual/release-notes/5.0-downgrade-replica-set/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Downgrade
<!-- -->
5.0
<!-- -->
Replica Set to
<!-- -->
4.4
<a class="headerlink leafygreen-ui-1kepx47" href="#downgrade-newversion-replica-set-to-oldversion" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="downgrade-newversion-replica-set-to-oldversion">
</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="#downgrade-path" target="_self">
<span>
Downgrade Path
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#create-backup" target="_self">
<span>
Create Backup
</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="#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>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
Before you attempt any downgrade, familiarize yourself with the content
of this document.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Downgrade Path
<a class="headerlink leafygreen-ui-1kepx47" href="#downgrade-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="downgrade-path">
</div>
</a>
</h2>
<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 you upgrade or downgrade a replica set, ensure all replica set
members are running. If you do not, the upgrade or downgrade will not
complete until all members are started.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If you need to downgrade from
<!-- -->
5.0
<!-- -->
, downgrade to the latest
patch release of
<!-- -->
4.4
<!-- -->
.
</p>
<p class="leafygreen-ui-1kp3ins">
MongoDB only supports single-version downgrades. You cannot downgrade to a
release that is multiple versions behind your current release.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, you may downgrade a
<!-- -->
5.0-series
<!-- -->
to a
<!-- -->
4.4-series
<!-- -->
deployment.
However, further downgrading that
<!-- -->
4.4-series
<!-- -->
deployment to a
<!-- -->
4.2-series
<!-- -->
deployment is not supported.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Create Backup
<a class="headerlink leafygreen-ui-1kepx47" href="#create-backup" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-backup">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
<em>
Optional but Recommended.
</em>
Create a backup of your database.
</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">
If your replica set has access control enabled, your downgrade user
privileges must include privileges to list and manage indexes across
databases. A user with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/built-in-roles/#mongodb-authrole-root">
<code class="leafygreen-ui-1l06pbn">
root
</code>
</a>
role has the required
privileges.
</p>
</section>
<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">
To downgrade from
<!-- -->
5.0
<!-- -->
to
<!-- -->
4.4
<!-- -->
, you must remove
incompatible features that are persisted and/or update incompatible
configuration settings. These include:
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
1. Cluster Default Read and Write Concerns
<a class="headerlink leafygreen-ui-1kepx47" href="#1.-cluster-default-read-and-write-concerns" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="1.-cluster-default-read-and-write-concerns">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB 5.0 changed the default value for cluster-wide read and write
concerns, and downgrading to MongoDB 4.4 might change those defaults
back. Consider manually configuring your cluster's default read and
write concern before downgrading:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To manually configure a default value for a cluster's read or write
concern, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setDefaultRWConcern/#mongodb-dbcommand-dbcmd.setDefaultRWConcern">
<code class="leafygreen-ui-1l06pbn">
setDefaultRWConcern
</code>
</a>
command.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If your cluster includes an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-arbiter/">
arbiter
</a>
, and you had previously disabled
<code class="leafygreen-ui-1l06pbn">
"Majority"
</code>
read concern to prevent cache pressure in certain
situations, you may want to configure
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--enableMajorityReadConcern">
<code class="leafygreen-ui-1l06pbn">
--enableMajorityReadConcern false
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-replication.enableMajorityReadConcern">
<code class="leafygreen-ui-1l06pbn">
replication.enableMajorityReadConcern: false
</code>
</a>
once you downgrade.
</p>
</li>
</ul>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
2. Document Fields with
<code class="leafygreen-ui-1l06pbn">
.
</code>
or
<code class="leafygreen-ui-1l06pbn">
$
</code>
Characters
<a class="headerlink leafygreen-ui-1kepx47" href="#2.-document-fields-with-.-or---characters" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="2.-document-fields-with-.-or---characters">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB 5.0 adds support for including the
<code class="leafygreen-ui-1l06pbn">
.
</code>
or
<code class="leafygreen-ui-1l06pbn">
$
</code>
characters in
document field names. You must delete any documents containing field
names that include the
<code class="leafygreen-ui-1l06pbn">
.
</code>
or
<code class="leafygreen-ui-1l06pbn">
$
</code>
characters before downgrading
to MongoDB 4.4.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
3. Slim-format Timezone Data Files
<a class="headerlink leafygreen-ui-1kepx47" href="#3.-slim-format-timezone-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="3.-slim-format-timezone-data-files">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB 5.0 enables support for slim-format timezone data files. If
using slim-format timezone data files in your deployment, as provided
to MongoDB with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--timeZoneInfo">
<code class="leafygreen-ui-1l06pbn">
--timeZoneInfo
</code>
</a>
command line option or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-processManagement.timeZoneInfo">
<code class="leafygreen-ui-1l06pbn">
processManagement.timeZoneInfo
</code>
</a>
configuration file setting, you must downgrade to MongoDB 4.4.7 or
later, or else revert your timezone data files to use the previous
non-slim-format data files.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-5.0-downgrade-feature-compatibility-rs">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
4. Downgrade Feature Compatibility Version (fCV)
<a class="headerlink leafygreen-ui-1kepx47" href="#4.-downgrade-feature-compatibility-version--fcv-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="4.-downgrade-feature-compatibility-version--fcv-">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
First, verify the following:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Ensure that no initial sync is in progress. Running
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setFeatureCompatibilityVersion/#mongodb-dbcommand-dbcmd.setFeatureCompatibilityVersion">
<code class="leafygreen-ui-1l06pbn">
setFeatureCompatibilityVersion
</code>
</a>
command while an
initial sync is in progress will cause the initial sync to restart.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Ensure that no nodes have a
<code class="leafygreen-ui-1l06pbn">
newlyAdded
</code>
field in their
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/">
replica set configuration
</a>
.
Run the following command on each node in your replica set to
verify this:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use local\ndb.system.replset.find( { \"members.newlyAdded\" : { $exists : 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">use local</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">system</span>.<span class="lg-highlight-property">replset</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-string">"members.newlyAdded"</span> <!-- -->: <!-- -->{ <!-- -->$exists<!-- --> <!-- -->: <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">
newlyAdded
</code>
field only appears in a node's replica set
configuration document during and shortly after an initial sync.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Ensure that no replica set member is in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-states/#mongodb-replstate-replstate.ROLLBACK">
<code class="leafygreen-ui-1l06pbn">
ROLLBACK
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-states/#mongodb-replstate-replstate.RECOVERING">
<code class="leafygreen-ui-1l06pbn">
RECOVERING
</code>
</a>
state.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Next, to downgrade the
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
of your replica
set:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Connect a
<code class="leafygreen-ui-1l06pbn">
mongo
</code>
shell to the
<!-- -->
primary
<!-- -->
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Downgrade the
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
to
<code class="leafygreen-ui-1l06pbn">
"4.4"
</code>
.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand({setFeatureCompatibilityVersion: \"4.4\"})","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">setFeatureCompatibilityVersion</span>: <span class="lg-highlight-string">"4.4"</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/command/setFeatureCompatibilityVersion/#mongodb-dbcommand-dbcmd.setFeatureCompatibilityVersion">
<code class="leafygreen-ui-1l06pbn">
setFeatureCompatibilityVersion
</code>
</a>
command performs writes
to an internal system collection and is idempotent. If for any reason
the command does not complete successfully, retry the command on the
<!-- -->
primary
<!-- -->
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To ensure that all members of the replica set reflect the updated
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
, connect to each replica set member and
check the
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 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">adminCommand</span>( <!-- -->{ <span class="lg-highlight-attr">getParameter</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">featureCompatibilityVersion</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">
All members should return a result that includes:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"\"featureCompatibilityVersion\" : { \"version\" : \"4.4\" }","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">"featureCompatibilityVersion"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"version"</span> <!-- -->: <span class="lg-highlight-string">"4.4"</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 any member returns a
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
of
<code class="leafygreen-ui-1l06pbn">
"5.0"
</code>
,
wait for the member to reflect version
<code class="leafygreen-ui-1l06pbn">
"4.4"
</code>
before proceeding.
</p>
</li>
</ol>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Arbiters do not replicate the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/system-collections/#mongodb-data-admin.system.version">
<code class="leafygreen-ui-1l06pbn">
admin.system.version
</code>
</a>
collection.
Because of this, arbiters always have a feature compatibility version equal
to the downgrade version of the binary, regardless of the fCV value of the
replica set.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, an arbiter in a MongoDB
<!-- -->
5.0
<!-- -->
cluster, has an fCV
value of
<!-- -->
4.4
<!-- -->
.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For more information on the returned
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
value, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setFeatureCompatibilityVersion/#std-label-view-fcv">
Get FeatureCompatibilityVersion
<!-- -->
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
5. Remove fCV
<!-- -->
5.0
<!-- -->
Persisted Features
<a class="headerlink leafygreen-ui-1kepx47" href="#5.-remove-fcv-newversion-persisted-features" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="5.-remove-fcv-newversion-persisted-features">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following steps are necessary only if fCV has ever been set to
<code class="leafygreen-ui-1l06pbn">
"5.0"
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
Remove all persisted
<!-- -->
5.0
<!-- -->
<a class="leafygreen-ui-tqgtui" href="/docs/manual/release-notes/5.0-compatibility/#std-label-5.0-compatibility-enabled">
features
</a>
that are incompatible with
<!-- -->
4.4
<!-- -->
. These include:
</p>
<dl>
<dt>
Time-series Collections
</dt>
<dd class="css-1ufxuu7">
Remove all
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/timeseries-collections/#std-label-manual-timeseries-collection">
time series collections
<!-- -->
.
</a>
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
Runtime Audit Filter Management
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Reset the defaults on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
server in the group with
<code class="leafygreen-ui-1l06pbn">
db.admin.runCommand
</code>
. The primary should be the last
configuration server in the group to be updated.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.admin.runCommand(\n {\n setAuditConfig: 1,\n filter: {},\n auditAuthorizationSuccess: false\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">admin</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">setAuditConfig</span>: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">filter</span>: <!-- -->{<!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">auditAuthorizationSuccess</span>: <span class="lg-highlight-literal">false</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 configuration document can also be removed after downgrade:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"config.settings.remove({_id: 'audit'});","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">config.<span class="lg-highlight-property">settings</span>.<span class="lg-highlight-title lg-highlight-function">remove</span>(<!-- -->{<span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">'audit'</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">
Disable Runtime Audit Filter Management on each node by setting
<code class="leafygreen-ui-1l06pbn">
auditLog.runtimeConfiguration
</code>
to
<code class="leafygreen-ui-1l06pbn">
false
</code>
in the node's
configuration file.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Update the audit filters for this instance in the local
configuration file.
</p>
</li>
</ul>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
6. Remove
<!-- -->
5.0
<!-- -->
Features
<a class="headerlink leafygreen-ui-1kepx47" href="#6.-remove-newversion-features" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="6.-remove-newversion-features">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Remove all persisted features that use
<!-- -->
5.0
<!-- -->
features. These
include but are not limited to:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If any view definitions include 5.0 operators, such as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/dateAdd/#mongodb-expression-exp.-dateAdd">
<code class="leafygreen-ui-1l06pbn">
$dateAdd
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/sampleRate/#mongodb-expression-exp.-sampleRate">
<code class="leafygreen-ui-1l06pbn">
$sampleRate
</code>
</a>
, they must be
removed. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/release-notes/5.0/#std-label-5.0-rel-notes-new-agg-operators">
New Aggregation Operators
</a>
for the full list.
</p>
</li>
</ul>
</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>
<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 proceeding with the downgrade procedure, ensure that all
replica set members, including delayed replica set members, reflect
the prerequisite changes. That is, check the
<code class="leafygreen-ui-1l06pbn">
featureCompatibilityVersion
</code>
and the removal of incompatible
features for each node before downgrading.
</p>
</div>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Download the latest 4.4 binaries.Using either a package manager or a manual download, get the latest\nrelease in the 4.4 series. If using a package manager, add a new\nrepository for the 4.4 binaries, then perform the actual downgrade\nprocess.Before you upgrade or downgrade a replica set, ensure all replica set\nmembers are running. If you do not, the upgrade or downgrade will not\ncomplete until all members are started.If you need to downgrade from 5.0, downgrade to the latest\npatch release of 4.4."},{"@type":"HowToStep","text":"Downgrade secondary members of the replica set.Downgrade each secondary member of the replica set, one at a\ntime:Run the following command in mongosh to\nperform a clean shutdown, or refer to\nStop mongod Processes for additional ways to safely\nterminate the mongod process:Replace the 5.0 binary with the 4.4 binary and restart.Wait for the member to recover to SECONDARY state\nbefore downgrading the next secondary. To check the member's state,\nuse the rs.status() method in mongosh.Once the member is in SECONDARY stage, downgrade the next\nsecondary."},{"@type":"HowToStep","text":"Downgrade arbiter replica set member, if any.Skip this step if the replica set does not include an arbiter.Downgrade the arbiter member of\nthe replica set:Run the following command in mongosh to\nperform a clean shutdown, or refer to\nStop mongod Processes for additional ways to safely\nterminate the mongod process:Delete the contents of the arbiter data directory. The\nstorage.dbPath configuration setting or\n--dbpath command line option specify the\ndata directory of the arbiter mongod.Replace the 5.0 binary with the 4.4 binary and restart.Wait for the member to recover to ARBITER state. To check\nthe member's state, connect mongosh to the\nmember and run rs.status() method."},{"@type":"HowToStep","text":"Step down the primary.Use rs.stepDown() in mongosh to\nstep down the primary and force the normal failover procedure.rs.stepDown() expedites the failover procedure and is\npreferable to shutting down the primary directly."},{"@type":"HowToStep","text":"Replace and restart former primary mongod.When rs.status() shows that the primary has stepped down\nand another member has assumed PRIMARY state:Run the following command in mongosh to\nperform a clean shutdown, or refer to\nStop mongod Processes for additional ways to safely\nterminate the mongod process:Replace the mongod binary with\nthe 4.4 binary and restart."}],"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">
Download the latest
<!-- -->
4.4
<!-- -->
binaries.
<a class="headerlink leafygreen-ui-1kepx47" href="#download-the-latest-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-the-latest-binaries">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Using either a package manager or a manual download, get the latest
release in the
<!-- -->
4.4
<!-- -->
series. If using a package manager, add a new
repository for the
<!-- -->
4.4
<!-- -->
binaries, then perform the actual downgrade
process.
</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">
Before you upgrade or downgrade a replica set, ensure all replica set
members are running. If you do not, the upgrade or downgrade will not
complete until all members are started.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
If you need to downgrade from
<!-- -->
5.0
<!-- -->
, downgrade to the latest
patch release of
<!-- -->
4.4
<!-- -->
.
</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">
Downgrade secondary members of the replica set.
<a class="headerlink leafygreen-ui-1kepx47" href="#downgrade-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="downgrade-secondary-members-of-the-replica-set">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Downgrade each
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary">
secondary
</a>
member of the replica set, one at a
time:
</p>
<ol class="leafygreen-ui-rioki0" type="a">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Run the following command 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
perform a clean shutdown, or refer to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-mongodb-processes/#std-label-terminate-mongod-processes">
Stop
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
Processes
</a>
for additional ways to safely
terminate 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:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { shutdown: 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">shutdown</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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Replace the
<!-- -->
5.0
<!-- -->
binary with the
<!-- -->
4.4
<!-- -->
binary and restart.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Wait for the member to recover to
<code class="leafygreen-ui-1l06pbn">
SECONDARY
</code>
state
before downgrading the next secondary. To check the member's state,
use the
<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>
method 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">
Once the member is in
<code class="leafygreen-ui-1l06pbn">
SECONDARY
</code>
stage, downgrade the next
secondary.
</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">
Downgrade arbiter replica set member, if any.
<a class="headerlink leafygreen-ui-1kepx47" href="#downgrade-arbiter-replica-set-member-if-any" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="downgrade-arbiter-replica-set-member-if-any">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Skip this step if the replica set does not include an arbiter.
</p>
<p class="leafygreen-ui-1kp3ins">
Downgrade the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-arbiter/#std-label-replica-set-arbiter-configuration">
arbiter
</a>
member of
the replica set:
</p>
<ol class="leafygreen-ui-rioki0" type="a">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Run the following command 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
perform a clean shutdown, or refer to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-mongodb-processes/#std-label-terminate-mongod-processes">
Stop
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
Processes
</a>
for additional ways to safely
terminate 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:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { shutdown: 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">shutdown</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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Delete the contents of the arbiter data directory. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-storage.dbPath">
<code class="leafygreen-ui-1l06pbn">
storage.dbPath
</code>
</a>
configuration setting or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--dbpath">
<code class="leafygreen-ui-1l06pbn">
--dbpath
</code>
</a>
command line option specify the
data directory of the arbiter
<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":"rm -rf /path/to/mongodb/datafiles/*","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-built_in">rm</span> -rf <!-- -->/path/to/mongodb/datafiles<!-- -->/*</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Replace the
<!-- -->
5.0
<!-- -->
binary with the
<!-- -->
4.4
<!-- -->
binary and restart.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Wait for the member to recover to
<code class="leafygreen-ui-1l06pbn">
ARBITER
</code>
state. To check
the member's state, 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
member 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>
method.
</p>
</li>
</ol>
</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">
Step down the primary.
<a class="headerlink leafygreen-ui-1kepx47" href="#step-down-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="step-down-the-primary">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
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>
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
step down the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
and force the normal
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-high-availability/#std-label-replica-set-failover">
failover
</a>
procedure.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"rs.stepDown()","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>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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/rs.stepDown/#mongodb-method-rs.stepDown">
<code class="leafygreen-ui-1l06pbn">
rs.stepDown()
</code>
</a>
expedites the failover procedure and is
preferable to shutting down the primary directly.
</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">
Replace and restart former primary
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
.
<a class="headerlink leafygreen-ui-1kepx47" href="#replace-and-restart-former-primary-mongod" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-and-restart-former-primary-mongod">
</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:
</p>
<ol class="leafygreen-ui-rioki0" type="a">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Run the following command 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
perform a clean shutdown, or refer to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-mongodb-processes/#std-label-terminate-mongod-processes">
Stop
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
Processes
</a>
for additional ways to safely
terminate 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:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { shutdown: 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">shutdown</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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Replace the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
binary with
the
<!-- -->
4.4
<!-- -->
binary and restart.
</p>
</li>
</ol>
</section>
</div>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-windows/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Install MongoDB Community Edition on Windows
<a class="headerlink leafygreen-ui-1kepx47" href="#install-mongodb-community-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-community-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-community-edition" target="_self">
<span>
Install MongoDB Community Edition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#run-mongodb-community-edition-as-a-windows-service" target="_self">
<span>
Run MongoDB Community Edition as a Windows Service
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#run-mongodb-community-edition-from-the-command-interpreter" target="_self">
<span>
Run MongoDB Community Edition from the Command Interpreter
</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
<!-- -->
Community
<!-- -->
Edition on
Windows using the default installation wizard.
</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
<!-- -->
Community
<!-- -->
Edition. To install a different version of MongoDB
<!-- -->
Community
<!-- -->
,
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-on-windows-unattended/#std-label-install-mdb-community-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 Community 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 Community Edition
<a class="headerlink leafygreen-ui-1kepx47" href="#install-mongodb-community-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-community-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
<!-- -->
Community
<!-- -->
Edition using the
MongoDB Installer 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 Community .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 Community 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
<!-- -->
Community
<!-- -->
<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/community?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
<!-- -->
Community
<!-- -->
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="_hdcyiz04h" aria-selected="true" class="leafygreen-ui-1l773z1" data-text="" id="_8ykplkqlq" name="" role="tab" tabindex="0">
<div class="leafygreen-ui-1nxuzmg">
MongoDB Service
</div>
</button>
<button aria-controls="_ia5rzzllt" aria-selected="false" class="leafygreen-ui-1ogxb8f" data-text="" id="_4c1d61xp2" 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="_59bhgslu8" class="" id="_ngzr20yyz" 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="_9ntp7q4aj" class="leafygreen-ui-38lglc" id="_gn86o2wov" 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">
Install
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#install-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-mongosh">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
.msi
</code>
installer does not include
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/" target="_self">
<span>
mongosh
</span>
</a>
. 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 shell separately.
</p>
</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.
</p>
<p class="leafygreen-ui-1kp3ins">
If you would like to customize the service, you must
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-stop-mongodb-community-edition-windows-service" target="_self">
<span>
stop the service
</span>
</a>
. Customize the
MongoDB instance by editing the configuration file at
<code class="file docutils literal">
<span class="pre">
<install directory>\bin\mongod.cfg
</span>
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
For information about the available configuration options, refer to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#std-label-configuration-options">
Self-Managed Configuration File Options
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
After making changes,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-start-mongodb-community-edition-windows-service" target="_self">
<span>
start the service again
<!-- -->
.
</span>
</a>
</p>
</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-from-cmd" target="_self">
<span>
Run MongoDB Community Edition from the Command Interpreter
</span>
</a>
for instructions to start a MongoDB
instance.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Run MongoDB Community Edition as a Windows Service
<a class="headerlink leafygreen-ui-1kepx47" href="#run-mongodb-community-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="run-mongodb-community-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
installation. 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 a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-start-mongodb-community-edition-windows-service">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Start MongoDB Community Edition as a Windows Service
<a class="headerlink leafygreen-ui-1kepx47" href="#start-mongodb-community-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-community-edition-as-a-windows-service">
</div>
</a>
</h3>
<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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-stop-mongodb-community-edition-windows-service">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Stop MongoDB Community Edition as a Windows Service
<a class="headerlink leafygreen-ui-1kepx47" href="#stop-mongodb-community-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-community-edition-as-a-windows-service">
</div>
</a>
</h3>
<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>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Remove MongoDB Community Edition as a Windows Service
<a class="headerlink leafygreen-ui-1kepx47" href="#remove-mongodb-community-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-community-edition-as-a-windows-service">
</div>
</a>
</h3>
<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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-run-mongodb-from-cmd">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Run MongoDB Community Edition from the Command Interpreter
<a class="headerlink leafygreen-ui-1kepx47" href="#run-mongodb-community-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="run-mongodb-community-edition-from-the-command-interpreter">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can run MongoDB Community Edition from the Windows command
prompt/interpreter (
<code class="leafygreen-ui-1l06pbn">
cmd.exe
</code>
) instead of as a service.
</p>
<p class="leafygreen-ui-1kp3ins">
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>
.
</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 open the command interpreter as an
<span class="guilabel css-h15tq0">
Administrator
</span>
.
</p>
</div>
</div>
<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":"Run MongoDB Community 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>
</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>
), the
<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">
If you 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>
you can omit the full path to the MongoDB Server
binaries. You should also add the path to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/" target="_self">
<span>
mongosh
</span>
</a>
if you
have not already done so.
</p>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-windows-unattended/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Install MongoDB Community on Windows using
<code class="leafygreen-ui-1l06pbn">
msiexec.exe
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#install-mongodb-community-on-windows-using-msiexec.exe" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-community-on-windows-using-msiexec.exe">
</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-community-edition" target="_self">
<span>
Install MongoDB Community Edition
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#start-mongodb-community-edition-from-the-command-interpreter" target="_self">
<span>
Start MongoDB Community Edition from the Command Interpreter
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#run-mongodb-community-edition-as-a-windows-service" target="_self">
<span>
Run MongoDB Community 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
<!-- -->
Community
<!-- -->
Edition on
Windows in an unattended fashion using
<code class="leafygreen-ui-1l06pbn">
msiexec.exe
</code>
from the command
line. This is useful for system administrators who wish to
deploy MongoDB using automation.
</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
<!-- -->
Community
<!-- -->
Edition. To install a different version of MongoDB
<!-- -->
Community
<!-- -->
,
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 command-line tool
<code class="leafygreen-ui-1l06pbn">
msiexec.exe
</code>
. To install MongoDB using the graphical MSI Installer
instead, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/install-mongodb-on-windows/#std-label-install-mdb-community-windows">
Install MongoDB using the MSI Installer
<!-- -->
.
</a>
</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 Community 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 Community Edition
<a class="headerlink leafygreen-ui-1kepx47" href="#install-mongodb-community-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-community-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
<!-- -->
Community
<!-- -->
Edition unattended on
Windows from the
<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>
) using
<code class="leafygreen-ui-1l06pbn">
msiexec.exe
</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 Community .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 Windows Installer from the Windows Command Interpreter.You must open the command interpreter as an\nAdministrator.Use the .msi installer to install all MongoDB binaries, including\nMongoDB Compass.From the Command Interpreter, go to the directory containing the\n.msi installation binary and run:The operation installs the binaries to the default directory\nC:\\Program Files\\MongoDB\\Server\\8.0\\bin.To specify a different installation location for the executables,\nadd the INSTALLLOCATION value.To suppress the installation of MongoDB\nCompass, you must explicitly include the\nSHOULD_INSTALL_COMPASS=\"0\" argument.To install specific MongoDB component sets, you can specify them in\nthe ADDLOCAL argument using a comma-separated list including one\nor more of the following component sets:Component Set NameBinaries Included in the Component SetServerNoServicemongod.exeServerServiceSet up mongod.exe as a Windows service.Routermongos.exeMonitoringToolsmongostat.exe,\nmongotop.exeImportExportToolsmongodump.exe,\nmongorestore.exe,\nmongoexport.exe,\nmongoimport.exeMiscellaneousToolsmongodecrypt.exe,\nmongokerberos.exe,\nmongoldap.exeFor example, to install the MongoDB server\n(mongod.exe) with the legacy mongo\nclient and then set up the MongoDB server as a Windows service,\nrun:To include Compass in the installs, remove SHOULD_INSTALL_COMPASS=\"0\"."}],"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
<!-- -->
Community
<!-- -->
<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/community?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 Windows Installer from the Windows Command Interpreter.
<a class="headerlink leafygreen-ui-1kepx47" href="#run-the-windows-installer-from-the-windows-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="run-the-windows-installer-from-the-windows-command-interpreter">
</div>
</a>
</h4>
<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 open the command interpreter as an
<span class="guilabel css-h15tq0">
Administrator
</span>
.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Use the
<code class="leafygreen-ui-1l06pbn">
.msi
</code>
installer to install all MongoDB binaries, including
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https:///www.mongodb.com/products/compass" target="_self">
<span>
MongoDB Compass
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
From the Command Interpreter, go to the directory containing the
<code class="leafygreen-ui-1l06pbn">
.msi
</code>
installation binary and run:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"msiexec.exe /l*v mdbinstall.log /qb /i mongodb-windows-x86_64-8.0-signed.msi\n"}
</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">msiexec.exe /l*v mdbinstall.log /qb /i mongodb-windows-x86_64-8.0-signed.msi</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 installs the binaries to the default directory
<code class="leafygreen-ui-1l06pbn">
C:\Program Files\MongoDB\Server\8.0\bin
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
To specify a different installation location for the executables,
add the
<code class="leafygreen-ui-1l06pbn">
INSTALLLOCATION
</code>
value.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"msiexec.exe /l*v mdbinstall.log /qb /i mongodb-windows-x86_64-8.0-signed.msi ^\n INSTALLLOCATION=\"C:\\MongoDB\\Server\\8.0\\\"\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">msiexec.exe /l*v mdbinstall.log /qb /i mongodb-windows-x86_64-8.0-signed.msi ^</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> INSTALLLOCATION="C:\MongoDB\Server\8.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>
<p class="leafygreen-ui-1kp3ins">
To suppress the installation of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/compass/current/#std-label-compass-index" target="_self">
<span>
MongoDB
Compass
</span>
</a>
, you must explicitly include the
<code class="leafygreen-ui-1l06pbn">
SHOULD_INSTALL_COMPASS="0"
</code>
argument.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"msiexec.exe /l*v mdbinstall.log /qb /i mongodb-windows-x86_64-8.0-signed.msi ^\n SHOULD_INSTALL_COMPASS=\"0\"\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">msiexec.exe /l*v mdbinstall.log /qb /i mongodb-windows-x86_64-8.0-signed.msi ^</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> SHOULD_INSTALL_COMPASS="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>
<p class="leafygreen-ui-1kp3ins">
To install specific MongoDB component sets, you can specify them in
the
<code class="leafygreen-ui-1l06pbn">
ADDLOCAL
</code>
argument using a comma-separated list including one
or more of the following component sets:
</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>
Component Set Name
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Binaries Included in the Component Set
</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">
ServerNoService
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<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>
</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">
ServerService
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Set up
<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>
as a Windows service.
</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">
Router
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos.exe/#mongodb-binary-bin.mongos.exe">
<code class="leafygreen-ui-1l06pbn">
mongos.exe
</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">
MonitoringTools
</code>
</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/database-tools/mongostat/#mongodb-binary-bin.mongostat" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongostat.exe
</code>
</span>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongotop/#mongodb-binary-bin.mongotop" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongotop.exe
</code>
</span>
</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">
ImportExportTools
</code>
</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/database-tools/mongodump/#mongodb-binary-bin.mongodump" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongodump.exe
</code>
</span>
</a>
,
<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.exe
</code>
</span>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongoexport/#mongodb-binary-bin.mongoexport" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongoexport.exe
</code>
</span>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongoimport/#mongodb-binary-bin.mongoimport" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongoimport.exe
</code>
</span>
</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">
MiscellaneousTools
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
mongodecrypt.exe
</code>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongokerberos/#mongodb-binary-bin.mongokerberos">
<code class="leafygreen-ui-1l06pbn">
mongokerberos.exe
</code>
</a>
,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongoldap/#mongodb-binary-bin.mongoldap">
<code class="leafygreen-ui-1l06pbn">
mongoldap.exe
</code>
</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
For example, to install the MongoDB server
(
<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>
) with the legacy
<code class="leafygreen-ui-1l06pbn">
mongo
</code>
client and then set up the MongoDB server as a Windows service,
run:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"msiexec.exe /l*v mdbinstall.log /qb /i mongodb-windows-x86_64-8.0-signed.msi ^\n ADDLOCAL=\"ServerService,LegacyClient\" ^\n SHOULD_INSTALL_COMPASS=\"0\"\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">msiexec.exe /l*v mdbinstall.log /qb /i mongodb-windows-x86_64-8.0-signed.msi ^</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ADDLOCAL="ServerService,LegacyClient" ^</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> SHOULD_INSTALL_COMPASS="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>
<p class="leafygreen-ui-1kp3ins">
To include Compass in the installs, remove
<code class="leafygreen-ui-1l06pbn">
SHOULD_INSTALL_COMPASS="0"
</code>
.
</p>
</section>
</div>
</div>
</div>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Start MongoDB Community Edition from the Command Interpreter
<a class="headerlink leafygreen-ui-1kepx47" href="#start-mongodb-community-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-community-edition-from-the-command-interpreter">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
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>
.
</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 open the command interpreter as an
<span class="guilabel css-h15tq0">
Administrator
</span>
.
</p>
</div>
</div>
<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 Community 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>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Run MongoDB Community Edition as a Windows Service
<a class="headerlink leafygreen-ui-1kepx47" href="#run-mongodb-community-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="run-mongodb-community-edition-as-a-windows-service">
</div>
</a>
</h2>
<span class="leafygreen-ui-1qnjd6d" id="std-label-manually-create-windows-service">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Start MongoDB Community Edition as a Windows Service
<a class="headerlink leafygreen-ui-1kepx47" href="#start-mongodb-community-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-community-edition-as-a-windows-service">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can install and configure MongoDB as a
<span class="guilabel css-h15tq0">
Windows Service
</span>
during
the install, and the MongoDB service starts upon successful installation.
</p>
<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 Community 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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
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>
</h4>
<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>
<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">
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>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
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>
</h4>
<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>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Stop MongoDB Community Edition as a Windows Service
<a class="headerlink leafygreen-ui-1kepx47" href="#stop-mongodb-community-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-community-edition-as-a-windows-service">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To stop/pause the MongoDB service, you can 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>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Remove MongoDB Community Edition as a Windows Service
<a class="headerlink leafygreen-ui-1kepx47" href="#remove-mongodb-community-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-community-edition-as-a-windows-service">
</div>
</a>
</h3>
<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>
<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>
), the
<code class="leafygreen-ui-1l06pbn">
.msi
</code>
automatically upgrades within the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/versioning/#std-label-release-version-numbers">
same 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">
If you 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>
you can omit the full path to the MongoDB Server
binaries. You should also add the path to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/" target="_self">
<span>
mongosh
</span>
</a>
if you
have not already done so.
</p>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/db.collection.insert/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.collection.insert()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.collection.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="db.collection.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="#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="#behaviors" target="_self">
<span>
Behaviors
</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="#writeresult" target="_self">
<span>
WriteResult
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#bulkwriteresult" target="_self">
<span>
BulkWriteResult
</span>
</a>
</li>
</ul>
</div>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Deprecated mongosh Method
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
This method is deprecated 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>
. For alternative
methods, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/reference/compatibility/#std-label-compatibility" target="_self">
<span>
Compatibility Changes with Legacy mongo Shell
<!-- -->
.
</span>
</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="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
db.collection.insert()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.collection.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-db.collection.insert">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Inserts a document or documents into a collection.
</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>
</td>
</tr>
</tbody>
</table>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-writeresults-insert" target="_self">
<span>
WriteResult
</span>
</a>
object for single inserts.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-bulkwriteresults-insert" target="_self">
<span>
BulkWriteResult
</span>
</a>
object for bulk inserts.
</p>
</li>
</ul>
</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">
db.collection.Insert()
</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-method-db.collection.insert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
insert()
</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.insert(\n <document>,\n {\n writeConcern: <document>,\n ordered: <boolean>\n }\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.collection.insert(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <document or array of documents>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> writeConcern: <document>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> ordered: <boolean></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-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">
document
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
document or array
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A document or array of documents to insert into the collection.
</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. A document expressing the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/">
write concern
</a>
. Omit to use the default write
concern. See
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-insert-wc" target="_self">
<span>
Write Concern
<!-- -->
.
</span>
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Do not explicitly set the write concern for the operation if run in
a transaction. To use write concern with transactions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions-write-concern">
Transactions and Write Concern
<!-- -->
.
</a>
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<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>
<p class="leafygreen-ui-1kp3ins">
Optional. If
<code class="leafygreen-ui-1l06pbn">
true
</code>
, perform an ordered insert of the
documents in the
array, and if an error occurs with one of documents, MongoDB will
return without processing the remaining documents in the array.
</p>
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
false
</code>
, perform an unordered insert, and if an error occurs
with one of documents, continue processing the remaining
documents in the array.
</p>
<p class="leafygreen-ui-1kp3ins">
Defaults to
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</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-db.collection.insert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
insert()
</code>
</span>
</a>
returns an object that
contains the status of the operation.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Behaviors
<a class="headerlink leafygreen-ui-1kepx47" href="#behaviors" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="behaviors">
</div>
</a>
</h2>
<span class="leafygreen-ui-1qnjd6d" id="std-label-insert-wc">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Write Concern
<a class="headerlink leafygreen-ui-1kepx47" href="#write-concern" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="write-concern">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.insert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
insert()
</code>
</span>
</a>
method uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/insert/#mongodb-dbcommand-dbcmd.insert">
<code class="leafygreen-ui-1l06pbn">
insert
</code>
</a>
command, which uses the default
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/">
write concern
</a>
. To specify a different write concern,
include the write concern in the options parameter.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Create Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#create-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-collection">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the collection does not exist, then the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.insert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
insert()
</code>
</span>
</a>
method will create the collection.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
<code class="leafygreen-ui-1l06pbn">
_id
</code>
Field
<a class="headerlink leafygreen-ui-1kepx47" href="#_id-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="_id-field">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the document does not specify an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-_id">
_id
</a>
field, then MongoDB
will add the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field and assign a unique
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/ObjectId/#mongodb-method-ObjectId">
<code class="leafygreen-ui-1l06pbn">
ObjectId()
</code>
</a>
for the document before inserting. Most
drivers create an ObjectId and insert the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field, but the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
will create and populate the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
if the driver or
application does not.
</p>
<p class="leafygreen-ui-1kp3ins">
If the document contains an
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field, the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
value must be
unique within the collection to avoid duplicate key error.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Transactions
<a class="headerlink leafygreen-ui-1kepx47" href="#transactions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="transactions">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.insert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.collection.insert()
</code>
</span>
</a>
can be used inside
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions">
distributed 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>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Collection Creation in Transactions
<a class="headerlink leafygreen-ui-1kepx47" href="#collection-creation-in-transactions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="collection-creation-in-transactions">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
You can create collections and indexes inside a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions-create-collections-indexes">
distributed
transaction
</a>
if the
transaction is
<strong class="css-4dei7l">
not
</strong>
a cross-shard write transaction.
</p>
<p class="leafygreen-ui-1kp3ins">
If you specify an insert on a non-existing collection
in a transaction, MongoDB creates the collection implicitly.
</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/transactions/#std-label-transactions-create-collections-indexes">
Create Collections and Indexes in a Transaction
</a>
</p>
</div>
</div>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Write Concerns and Transactions
<a class="headerlink leafygreen-ui-1kepx47" href="#write-concerns-and-transactions" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="write-concerns-and-transactions">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
Do not explicitly set the write concern for the operation if run in
a transaction. To use write concern with transactions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/transactions/#std-label-transactions-write-concern">
Transactions and Write Concern
<!-- -->
.
</a>
</p>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Oplog Entries
<a class="headerlink leafygreen-ui-1kepx47" href="#oplog-entries" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="oplog-entries">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If a
<code class="leafygreen-ui-1l06pbn">
db.collection.insert()
</code>
operation successfully inserts a document,
the operation adds an entry on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-oplog">
oplog
</a>
(operations log).
If the operation fails, the operation does not add an entry on the
oplog.
</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 examples insert documents into the
<code class="leafygreen-ui-1l06pbn">
products
</code>
collection. If the collection does not exist, the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.insert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
insert()
</code>
</span>
</a>
method creates the collection.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Insert a Document without Specifying an
<code class="leafygreen-ui-1l06pbn">
_id
</code>
Field
<a class="headerlink leafygreen-ui-1kepx47" href="#insert-a-document-without-specifying-an-_id-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="insert-a-document-without-specifying-an-_id-field">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
In the following example, the document passed to the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.insert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
insert()
</code>
</span>
</a>
method does not contain 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":"db.products.insert( { item: \"card\", qty: 15 } )","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">insert</span>( <!-- -->{ <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"card"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">15</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">
During the insert,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
will create the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field and
assign it a unique
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/ObjectId/#mongodb-method-ObjectId">
<code class="leafygreen-ui-1l06pbn">
ObjectId()
</code>
</a>
value, as verified by
the inserted document:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : ObjectId(\"5063114bd386d8fadbd6b004\"), \"item\" : \"card\", \"qty\" : 15 }","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-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"5063114bd386d8fadbd6b004"</span>)<!-- -->, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"card"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">15</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">
ObjectId
</code>
values are specific to the machine and time when the
operation is run. As such, your values may differ from those in the
example.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Insert a Document Specifying an
<code class="leafygreen-ui-1l06pbn">
_id
</code>
Field
<a class="headerlink leafygreen-ui-1kepx47" href="#insert-a-document-specifying-an-_id-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="insert-a-document-specifying-an-_id-field">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
In the following example, the document passed to the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.insert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
insert()
</code>
</span>
</a>
method includes the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field.
The value of
<code class="leafygreen-ui-1l06pbn">
_id
</code>
must be unique within the collection to avoid
duplicate key error.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.insert( { _id: 10, item: \"box\", qty: 20 } )","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">insert</span>( <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"box"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">20</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 inserts the following document in 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":"{ \"_id\" : 10, \"item\" : \"box\", \"qty\" : 20 }","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">10</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"box"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">20</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">
Insert Multiple Documents
<a class="headerlink leafygreen-ui-1kepx47" href="#insert-multiple-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="insert-multiple-documents">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following example performs a bulk insert of three documents by
passing an array of documents to the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.insert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
insert()
</code>
</span>
</a>
method. By default, MongoDB performs an
<em>
ordered
</em>
insert. With
<em>
ordered
</em>
inserts, if an error occurs during an insert of one of the
documents, MongoDB returns on error without processing the remaining
documents in the array.
</p>
<p class="leafygreen-ui-1kp3ins">
The documents in the array do not need to have the same fields. For
instance, the first document in the array has an
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field and a
<code class="leafygreen-ui-1l06pbn">
type
</code>
field. Because the second and third documents do not contain
an
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
will create the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field for
the second and third documents during the insert:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.insert(\n [\n { _id: 11, item: \"pencil\", qty: 50, type: \"no.2\" },\n { item: \"pen\", qty: 20 },\n { item: \"eraser\", qty: 25 }\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">insert</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">11</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"pencil"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">50</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"no.2"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"pen"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">20</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"eraser"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">25</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 inserted the following three documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 11, \"item\" : \"pencil\", \"qty\" : 50, \"type\" : \"no.2\" }\n{ \"_id\" : ObjectId(\"51e0373c6f35bd826f47e9a0\"), \"item\" : \"pen\", \"qty\" : 20 }\n{ \"_id\" : ObjectId(\"51e0373c6f35bd826f47e9a1\"), \"item\" : \"eraser\", \"qty\" : 25 }","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">11</span>, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"pencil"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">50</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"no.2"</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">"51e0373c6f35bd826f47e9a0"</span>)<!-- -->, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"pen"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">20</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">"51e0373c6f35bd826f47e9a1"</span>)<!-- -->, <span class="lg-highlight-string">"item"</span> <!-- -->: <span class="lg-highlight-string">"eraser"</span>, <span class="lg-highlight-string">"qty"</span> <!-- -->: <span class="lg-highlight-number">25</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">
Perform an Unordered Insert
<a class="headerlink leafygreen-ui-1kepx47" href="#perform-an-unordered-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="perform-an-unordered-insert">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following example performs an
<em>
unordered
</em>
insert of three
documents. With
<em>
unordered
</em>
inserts, if an error occurs during an
insert of one of the documents, MongoDB continues to insert the
remaining documents in the array.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.insert(\n [\n { _id: 20, item: \"lamp\", qty: 50, type: \"desk\" },\n { _id: 21, item: \"lamp\", qty: 20, type: \"floor\" },\n { _id: 22, item: \"bulk\", qty: 100 }\n ],\n { ordered: 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-property">products</span>.<span class="lg-highlight-title lg-highlight-function">insert</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">20</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"lamp"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">50</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"desk"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">21</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"lamp"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">20</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"floor"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">22</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"bulk"</span>, <span class="lg-highlight-attr">qty</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"> <!-- -->{ <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>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Override Default Write Concern
<a class="headerlink leafygreen-ui-1kepx47" href="#override-default-write-concern" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="override-default-write-concern">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following operation to a replica set specifies a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/">
write concern
</a>
of
<code class="leafygreen-ui-1l06pbn">
w: 2
</code>
with a
<code class="leafygreen-ui-1l06pbn">
wtimeout
</code>
of 5000
milliseconds. This operation either returns after the write propagates
to both the primary and one secondary, or times out after 5 seconds.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.products.insert(\n { item: \"envelopes\", qty : 100, type: \"Clasp\" },\n { writeConcern: { w: 2, 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">db.<span class="lg-highlight-property">products</span>.<span class="lg-highlight-title lg-highlight-function">insert</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"envelopes"</span>, <!-- -->qty <!-- -->: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"Clasp"</span> }<!-- -->,</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-number">2</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-writeresults-insert">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
WriteResult
<a class="headerlink leafygreen-ui-1kepx47" href="#writeresult" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
When passed a single document,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.insert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
insert()
</code>
</span>
</a>
returns a
<code class="leafygreen-ui-1l06pbn">
WriteResult
</code>
object.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Successful Results
<a class="headerlink leafygreen-ui-1kepx47" href="#successful-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="successful-results">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.insert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
insert()
</code>
</span>
</a>
returns a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/WriteResult/#mongodb-method-WriteResult">
<code class="leafygreen-ui-1l06pbn">
WriteResult()
</code>
</a>
object that contains the status of the operation. Upon success, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/WriteResult/#mongodb-method-WriteResult">
<code class="leafygreen-ui-1l06pbn">
WriteResult()
</code>
</a>
object contains information on the number of
documents inserted:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"WriteResult({ \"nInserted\" : 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"><span class="lg-highlight-title lg-highlight-class">WriteResult</span>(<!-- -->{ <span class="lg-highlight-string">"nInserted"</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>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Write Concern Errors
<a class="headerlink leafygreen-ui-1kepx47" href="#write-concern-errors" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="write-concern-errors">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.insert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
insert()
</code>
</span>
</a>
method encounters write
concern errors, the results include the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/WriteResult/#mongodb-data-WriteResult.writeConcernError">
<code class="leafygreen-ui-1l06pbn">
WriteResult.writeConcernError
</code>
</a>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"WriteResult({\n \"nInserted\" : 1,\n \"writeConcernError\"({\n \"code\" : 64,\n \"errmsg\" : \"waiting for replication timed out\",\n \"errInfo\" : {\n \"wtimeout\" : true,\n \"writeConcern\" : {\n \"w\" : \"majority\",\n \"wtimeout\" : 100,\n \"provenance\" : \"getLastErrorDefaults\"\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-title lg-highlight-class">WriteResult</span>(<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nInserted"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"writeConcernError"</span>(<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"code"</span> <!-- -->: <span class="lg-highlight-number">64</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"errmsg"</span> <!-- -->: <span class="lg-highlight-string">"waiting for replication timed out"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"errInfo"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"wtimeout"</span> <!-- -->: <span class="lg-highlight-literal">true</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"writeConcern"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"w"</span> <!-- -->: <span class="lg-highlight-string">"majority"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"wtimeout"</span> <!-- -->: <span class="lg-highlight-number">100</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"provenance"</span> <!-- -->: <span class="lg-highlight-string">"getLastErrorDefaults"</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-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-data-WriteResult.writeConcernError">
<code class="leafygreen-ui-1l06pbn">
WriteResult.writeConcernError
</code>
</a>
</p>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Errors Unrelated to Write Concern
<a class="headerlink leafygreen-ui-1kepx47" href="#errors-unrelated-to-write-concern" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="errors-unrelated-to-write-concern">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.insert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
insert()
</code>
</span>
</a>
method encounters a non-write
concern error, the results include the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/WriteResult/#mongodb-data-WriteResult.writeError">
<code class="leafygreen-ui-1l06pbn">
WriteResult.writeError
</code>
</a>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"WriteResult({\n \"nInserted\" : 0,\n \"writeError\" : {\n \"code\" : 11000,\n \"errmsg\" : \"insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.foo.$_id_ dup key: { : 1.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"><span class="lg-highlight-title lg-highlight-class">WriteResult</span>(<!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"nInserted"</span> <!-- -->: <span class="lg-highlight-number">0</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"writeError"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"code"</span> <!-- -->: <span class="lg-highlight-number">11000</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"errmsg"</span> <!-- -->: <span class="lg-highlight-string">"insertDocument :: caused by :: 11000 E11000 duplicate key error index: test.foo.$_id_ dup key: { : 1.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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-bulkwriteresults-insert">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
BulkWriteResult
<a class="headerlink leafygreen-ui-1kepx47" href="#bulkwriteresult" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="bulkwriteresult">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
When passed an array of documents,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.collection.insert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
insert()
</code>
</span>
</a>
returns a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/BulkWriteResult/#mongodb-method-BulkWriteResult">
<code class="leafygreen-ui-1l06pbn">
BulkWriteResult()
</code>
</a>
object. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/BulkWriteResult/#mongodb-method-BulkWriteResult">
<code class="leafygreen-ui-1l06pbn">
BulkWriteResult()
</code>
</a>
for details.
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/indexes/index-types/geospatial/2d/create/define-location-precision/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Define Location Precision for a 2d Index
<a class="headerlink leafygreen-ui-1kepx47" href="#define-location-precision-for-a-2d-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="define-location-precision-for-a-2d-index">
</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">
In a 2d index, location precision is defined by the size in bits of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-geohash">
geohash
</a>
values used to store the indexed data. By default, 2d
indexes use 26 bits of precision, which is equivalent to approximately
two feet (60 centimeters).
</p>
<p class="leafygreen-ui-1kp3ins">
Location precision affects performance for insert and read operations.
</p>
<p class="leafygreen-ui-1kp3ins">
To change the default precision, specify a
<code class="leafygreen-ui-1l06pbn">
bits
</code>
value when you
create the 2d index. You can specify a
<code class="leafygreen-ui-1l06pbn">
bits
</code>
value between 1 and 32,
inclusive.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.<collection>.createIndex(\n { <location>: \"2d\" },\n { bits: <bit> }\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">createIndex</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- --><location field>: "2d" },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> { bits: <bit precision> }</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>
<p class="leafygreen-ui-1kp3ins">
Location precision affects query performance:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Lower precision improves performance for insert and update operations,
and uses less storage.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Higher precision improves performance for read operations because
queries scan smaller portions of the index to return results.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Location precision does not affect query accuracy. Grid coordinates are
always used in the final query processing.
</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">
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>
<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">
Create a 2d index on the
<code class="leafygreen-ui-1l06pbn">
address
</code>
field. Specify a location precision
of
<code class="leafygreen-ui-1l06pbn">
32
</code>
bits:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.contacts.createIndex(\n { address: \"2d\" },\n { bits: 32 }\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">createIndex</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">address</span>: <span class="lg-highlight-string">"2d"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">bits</span>: <span class="lg-highlight-number">32</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">
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 use the 2d index to perform calculations on location data, such
as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2d/query/proximity-flat-surface/#std-label-2d-index-proximity-query">
proximity queries
<!-- -->
.
</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">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/indexes/index-types/geospatial/2d/internals/#std-label-geospatial-indexes-geohash">
Geohash Values
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/geospatial-queries/#std-label-geospatial-geometry">
Geospatial Models
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/geospatial-queries/#std-label-geospatial-legacy">
Legacy Coordinate Pairs
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/denseRank/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$denseRank (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-denserank--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="-denserank--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>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.0
</em>
.
</p>
</div>
<dl class="group">
<dt>
<code class="leafygreen-ui-1l06pbn">
$denseRank
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-group-grp.-denseRank" 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.-denseRank">
</div>
</a>
</dt>
<dd>
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
Returns the document position (known as the rank) relative to other
documents 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
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-partitionBy">
partition
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
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
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-sortBy">
sortBy
</a>
field value determines the document rank. For
more information on how MongoDB compares fields with different types,
see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-type-comparison-order/#std-label-bson-types-comparison-order">
BSON comparison order
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
If multiple documents occupy the same rank,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-denseRank" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$denseRank
</code>
</span>
</a>
places
the document with the subsequent value at the next rank without any gaps
(see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-denseRank-behavior" target="_self">
<span>
Behavior
</span>
</a>
).
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-denseRank" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$denseRank
</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.-denseRank" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$denseRank
</code>
</span>
</a>
syntax:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $denseRank: { } }"}
</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">{ $denseRank: { } }</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.-denseRank" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$denseRank
</code>
</span>
</a>
does not accept any parameters.
</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/aggregation/rank/#mongodb-group-grp.-rank">
<code class="leafygreen-ui-1l06pbn">
$rank
</code>
</a>
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-denseRank-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>
<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/rank/#mongodb-group-grp.-rank">
<code class="leafygreen-ui-1l06pbn">
$rank
</code>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-denseRank" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$denseRank
</code>
</span>
</a>
differ in how they rank duplicate
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-sortBy">
sortBy
</a>
field values. For example, with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-sortBy">
sortBy
</a>
field values of 7, 9, 9, and 10:
</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-group-grp.-denseRank" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$denseRank
</code>
</span>
</a>
ranks the values as 1, 2, 2, and 3. The
duplicate 9 values have a rank of 2, and 10 has a rank of 3. There is
no gap in the ranks.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/rank/#mongodb-group-grp.-rank">
<code class="leafygreen-ui-1l06pbn">
$rank
</code>
</a>
ranks the values as 1, 2, 2, and 4. The duplicate 9
values have a rank of 2, and 10 has a rank of 4. There is a gap in the
ranks for 3.
</p>
</li>
</ul>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Documents with a
<code class="leafygreen-ui-1l06pbn">
null
</code>
value for a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-sortBy">
sortBy
</a>
field or documents missing the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-sortBy">
sortBy
</a>
field are assigned a rank based on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-type-comparison-order/#std-label-bson-types-comparison-order">
BSON comparison order
</a>
.
See the example in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/rank/#std-label-rank-duplicate-null-missing-values-example">
Rank Partitions Containing Duplicate Values, Nulls, or Missing Data
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Starting in MongoDB 8.0,
<code class="leafygreen-ui-1l06pbn">
null
</code>
and missing field values in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-denseRank" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$denseRank
</code>
</span>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/rank/#mongodb-group-grp.-rank">
<code class="leafygreen-ui-1l06pbn">
$rank
</code>
</a>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-sortBy">
sortBy
</a>
operations
are treated the same when calculating rankings. This change makes
the behavior of
<code class="leafygreen-ui-1l06pbn">
denseRank
</code>
and
<code class="leafygreen-ui-1l06pbn">
rank
</code>
consistent with
<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>
</ul>
<p class="leafygreen-ui-1kp3ins">
See the example in
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-denseRank-duplicate-null-missing-values-example" target="_self">
<span>
Dense Rank for Duplicate, Null, and Missing Values
<!-- -->
.
</span>
</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>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Dense Rank Partitions by an Integer Field
<a class="headerlink leafygreen-ui-1kepx47" href="#dense-rank-partitions-by-an-integer-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="dense-rank-partitions-by-an-integer-field">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Create a
<code class="leafygreen-ui-1l06pbn">
cakeSales
</code>
collection that contains cake sales in the states
of California (
<code class="leafygreen-ui-1l06pbn">
CA
</code>
) and Washington (
<code class="leafygreen-ui-1l06pbn">
WA
</code>
):
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeSales.insertMany( [\n { _id: 0, type: \"chocolate\", orderDate: new Date(\"2020-05-18T14:10:30Z\"),\n state: \"CA\", price: 13, quantity: 120 },\n { _id: 1, type: \"chocolate\", orderDate: new Date(\"2021-03-20T11:30:05Z\"),\n state: \"WA\", price: 14, quantity: 140 },\n { _id: 2, type: \"vanilla\", orderDate: new Date(\"2021-01-11T06:31:15Z\"),\n state: \"CA\", price: 12, quantity: 145 },\n { _id: 3, type: \"vanilla\", orderDate: new Date(\"2020-02-08T13:13:23Z\"),\n state: \"WA\", price: 13, quantity: 104 },\n { _id: 4, type: \"strawberry\", orderDate: new Date(\"2019-05-18T16:09:01Z\"),\n state: \"CA\", price: 41, quantity: 162 },\n { _id: 5, type: \"strawberry\", orderDate: new Date(\"2019-01-08T06:12:03Z\"),\n state: \"WA\", price: 43, quantity: 134 }\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">cakeSales</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">"chocolate"</span>, <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">"2020-05-18T14:10:30Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">13</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">120</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">"chocolate"</span>, <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">"2021-03-20T11:30:05Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">14</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">140</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">"vanilla"</span>, <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">"2021-01-11T06:31:15Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">12</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">145</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">type</span>: <span class="lg-highlight-string">"vanilla"</span>, <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">"2020-02-08T13:13:23Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">13</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">104</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">type</span>: <span class="lg-highlight-string">"strawberry"</span>, <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">"2019-05-18T16:09:01Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">41</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">162</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">type</span>: <span class="lg-highlight-string">"strawberry"</span>, <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">"2019-01-08T06:12:03Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">43</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">134</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.-denseRank" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$denseRank
</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
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
dense rank
of the cake sales for each
<code class="leafygreen-ui-1l06pbn">
state
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeSales.aggregate( [\n {\n $setWindowFields: {\n partitionBy: \"$state\",\n sortBy: { quantity: -1 },\n output: {\n denseRankQuantityForState: {\n $denseRank: {}\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">cakeSales</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">"$state"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sortBy</span>: <!-- -->{ <span class="lg-highlight-attr">quantity</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">denseRankQuantityForState</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$denseRank</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 the example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
partitionBy: "$state"
</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">
state
</code>
. There are partitions for
<code class="leafygreen-ui-1l06pbn">
CA
</code>
and
<code class="leafygreen-ui-1l06pbn">
WA
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
sortBy: { quantity: -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">
quantity
</code>
in descending order (
<code class="leafygreen-ui-1l06pbn">
-1
</code>
), so the highest
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
is first.
</p>
</li>
</ul>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
output
</code>
sets the
<code class="leafygreen-ui-1l06pbn">
denseRankOrderDateForState
</code>
field to the
<code class="leafygreen-ui-1l06pbn">
orderDate
</code>
dense rank using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-denseRank" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$denseRank
</code>
</span>
</a>
, as shown in the
following results.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 4, \"type\" : \"strawberry\", \"orderDate\" : ISODate(\"2019-05-18T16:09:01Z\"),\n \"state\" : \"CA\", \"price\" : 41, \"quantity\" : 162, \"denseRankQuantityForState\" : 1 }\n{ \"_id\" : 2, \"type\" : \"vanilla\", \"orderDate\" : ISODate(\"2021-01-11T06:31:15Z\"),\n \"state\" : \"CA\", \"price\" : 12, \"quantity\" : 145, \"denseRankQuantityForState\" : 2 }\n{ \"_id\" : 0, \"type\" : \"chocolate\", \"orderDate\" : ISODate(\"2020-05-18T14:10:30Z\"),\n \"state\" : \"CA\", \"price\" : 13, \"quantity\" : 120, \"denseRankQuantityForState\" : 3 }\n{ \"_id\" : 1, \"type\" : \"chocolate\", \"orderDate\" : ISODate(\"2021-03-20T11:30:05Z\"),\n \"state\" : \"WA\", \"price\" : 14, \"quantity\" : 140, \"denseRankQuantityForState\" : 1 }\n{ \"_id\" : 5, \"type\" : \"strawberry\", \"orderDate\" : ISODate(\"2019-01-08T06:12:03Z\"),\n \"state\" : \"WA\", \"price\" : 43, \"quantity\" : 134, \"denseRankQuantityForState\" : 2 }\n{ \"_id\" : 3, \"type\" : \"vanilla\", \"orderDate\" : ISODate(\"2020-02-08T13:13:23Z\"),\n \"state\" : \"WA\", \"price\" : 13, \"quantity\" : 104, \"denseRankQuantityForState\" : 3 }","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-number">4</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"strawberry"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2019-05-18T16:09:01Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">41</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">162</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</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">2</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"vanilla"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-01-11T06:31:15Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">12</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">145</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</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>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</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-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">13</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">120</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</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">1</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-20T11:30:05Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">14</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">140</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</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">5</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"strawberry"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2019-01-08T06:12:03Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">43</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">134</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</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>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"vanilla"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-02-08T13:13:23Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">13</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">104</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</span> <!-- -->: <span class="lg-highlight-number">3</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Dense Rank Partitions by a Date Field
<a class="headerlink leafygreen-ui-1kepx47" href="#dense-rank-partitions-by-a-date-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="dense-rank-partitions-by-a-date-field">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
This example shows how to use dates with
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-denseRank" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$denseRank
</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
<code class="leafygreen-ui-1l06pbn">
orderDate
</code>
dense
rank of the cake sales for each
<code class="leafygreen-ui-1l06pbn">
state
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeSales.aggregate( [\n {\n $setWindowFields: {\n partitionBy: \"$state\",\n sortBy: { orderDate: 1 },\n output: {\n denseRankOrderDateForState: {\n $denseRank: {}\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">cakeSales</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">"$state"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sortBy</span>: <!-- -->{ <span class="lg-highlight-attr">orderDate</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">denseRankOrderDateForState</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$denseRank</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 the example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
partitionBy: "$state"
</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">
state
</code>
. There are partitions for
<code class="leafygreen-ui-1l06pbn">
CA
</code>
and
<code class="leafygreen-ui-1l06pbn">
WA
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
sortBy: { orderDate: 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">
orderDate
</code>
in ascending order (
<code class="leafygreen-ui-1l06pbn">
1
</code>
), so the earliest
<code class="leafygreen-ui-1l06pbn">
orderDate
</code>
is first.
</p>
</li>
</ul>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
output
</code>
sets the
<code class="leafygreen-ui-1l06pbn">
denseRankOrderDateForState
</code>
field to the
<code class="leafygreen-ui-1l06pbn">
orderDate
</code>
rank using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-denseRank" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$denseRank
</code>
</span>
</a>
, as shown in the
following results.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 4, \"type\" : \"strawberry\", \"orderDate\" : ISODate(\"2019-05-18T16:09:01Z\"),\n \"state\" : \"CA\", \"price\" : 41, \"quantity\" : 162, \"denseRankOrderDateForState\" : 1 }\n{ \"_id\" : 0, \"type\" : \"chocolate\", \"orderDate\" : ISODate(\"2020-05-18T14:10:30Z\"),\n \"state\" : \"CA\", \"price\" : 13, \"quantity\" : 120, \"denseRankOrderDateForState\" : 2 }\n{ \"_id\" : 2, \"type\" : \"vanilla\", \"orderDate\" : ISODate(\"2021-01-11T06:31:15Z\"),\n \"state\" : \"CA\", \"price\" : 12, \"quantity\" : 145, \"denseRankOrderDateForState\" : 3 }\n{ \"_id\" : 5, \"type\" : \"strawberry\", \"orderDate\" : ISODate(\"2019-01-08T06:12:03Z\"),\n \"state\" : \"WA\", \"price\" : 43, \"quantity\" : 134, \"denseRankOrderDateForState\" : 1 }\n{ \"_id\" : 3, \"type\" : \"vanilla\", \"orderDate\" : ISODate(\"2020-02-08T13:13:23Z\"),\n \"state\" : \"WA\", \"price\" : 13, \"quantity\" : 104, \"denseRankOrderDateForState\" : 2 }\n{ \"_id\" : 1, \"type\" : \"chocolate\", \"orderDate\" : ISODate(\"2021-03-20T11:30:05Z\"),\n \"state\" : \"WA\", \"price\" : 14, \"quantity\" : 140, \"denseRankOrderDateForState\" : 3 }","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-number">4</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"strawberry"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2019-05-18T16:09:01Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">41</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">162</span>, <span class="lg-highlight-string">"denseRankOrderDateForState"</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>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</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-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">13</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">120</span>, <span class="lg-highlight-string">"denseRankOrderDateForState"</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>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"vanilla"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-01-11T06:31:15Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">12</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">145</span>, <span class="lg-highlight-string">"denseRankOrderDateForState"</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">5</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"strawberry"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2019-01-08T06:12:03Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">43</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">134</span>, <span class="lg-highlight-string">"denseRankOrderDateForState"</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">3</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"vanilla"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-02-08T13:13:23Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">13</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">104</span>, <span class="lg-highlight-string">"denseRankOrderDateForState"</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">1</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-20T11:30:05Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">14</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">140</span>, <span class="lg-highlight-string">"denseRankOrderDateForState"</span> <!-- -->: <span class="lg-highlight-number">3</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-denseRank-duplicate-null-missing-values-example">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Dense Rank for Duplicate, Null, and Missing Values
<a class="headerlink leafygreen-ui-1kepx47" href="#dense-rank-for-duplicate--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="dense-rank-for-duplicate--null--and-missing-values">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Create a
<code class="leafygreen-ui-1l06pbn">
cakeSalesWithDuplicates
</code>
collection where:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Cake sales are placed in the state of California (
<code class="leafygreen-ui-1l06pbn">
CA
</code>
) and
Washington (
<code class="leafygreen-ui-1l06pbn">
WA
</code>
).
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Documents 6 to 8 have the same
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
and
<code class="leafygreen-ui-1l06pbn">
state
</code>
as document
5.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Document 9 has the same
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
and
<code class="leafygreen-ui-1l06pbn">
state
</code>
as document 4.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Document 10 has a
<code class="leafygreen-ui-1l06pbn">
null
</code>
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Document 11 is missing the
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeSalesWithDuplicates.insertMany( [\n { _id: 0, type: \"chocolate\", orderDate: new Date(\"2020-05-18T14:10:30Z\"),\n state: \"CA\", price: 13, quantity: 120 },\n { _id: 1, type: \"chocolate\", orderDate: new Date(\"2021-03-20T11:30:05Z\"),\n state: \"WA\", price: 14, quantity: 140 },\n { _id: 2, type: \"vanilla\", orderDate: new Date(\"2021-01-11T06:31:15Z\"),\n state: \"CA\", price: 12, quantity: 145 },\n { _id: 3, type: \"vanilla\", orderDate: new Date(\"2020-02-08T13:13:23Z\"),\n state: \"WA\", price: 13, quantity: 104 },\n { _id: 4, type: \"strawberry\", orderDate: new Date(\"2019-05-18T16:09:01Z\"),\n state: \"CA\", price: 41, quantity: 162 },\n { _id: 5, type: \"strawberry\", orderDate: new Date(\"2019-01-08T06:12:03Z\"),\n state: \"WA\", price: 43, quantity: 134 },\n { _id: 6, type: \"strawberry\", orderDate: new Date(\"2020-01-08T06:12:03Z\"),\n state: \"WA\", price: 41, quantity: 134 },\n { _id: 7, type: \"strawberry\", orderDate: new Date(\"2020-01-01T06:12:03Z\"),\n state: \"WA\", price: 34, quantity: 134 },\n { _id: 8, type: \"strawberry\", orderDate: new Date(\"2020-01-02T06:12:03Z\"),\n state: \"WA\", price: 40, quantity: 134 },\n { _id: 9, type: \"strawberry\", orderDate: new Date(\"2020-05-11T16:09:01Z\"),\n state: \"CA\", price: 39, quantity: 162 },\n { _id: 10, type: \"strawberry\", orderDate: new Date(\"2020-05-11T16:09:01Z\"),\n state: \"CA\", price: 39, quantity: null },\n { _id: 11, type: \"strawberry\", orderDate: new Date(\"2020-05-11T16:09:01Z\"),\n state: \"CA\", price: 39 }\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">cakeSalesWithDuplicates</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">"chocolate"</span>, <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">"2020-05-18T14:10:30Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">13</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">120</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">"chocolate"</span>, <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">"2021-03-20T11:30:05Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">14</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">140</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">"vanilla"</span>, <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">"2021-01-11T06:31:15Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">12</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">145</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">type</span>: <span class="lg-highlight-string">"vanilla"</span>, <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">"2020-02-08T13:13:23Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">13</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">104</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">type</span>: <span class="lg-highlight-string">"strawberry"</span>, <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">"2019-05-18T16:09:01Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">41</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">162</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">type</span>: <span class="lg-highlight-string">"strawberry"</span>, <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">"2019-01-08T06:12:03Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">43</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">134</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <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">"strawberry"</span>, <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">"2020-01-08T06:12:03Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">41</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">134</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">7</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"strawberry"</span>, <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">"2020-01-01T06:12:03Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">34</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">134</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">8</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"strawberry"</span>, <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">"2020-01-02T06:12:03Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">40</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">134</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">9</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"strawberry"</span>, <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">"2020-05-11T16:09:01Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">39</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-number">162</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"strawberry"</span>, <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">"2020-05-11T16:09:01Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">39</span>, <span class="lg-highlight-attr">quantity</span>: <span class="lg-highlight-literal">null</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">11</span>, <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"strawberry"</span>, <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">"2020-05-11T16:09:01Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">state</span>: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">39</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.-denseRank" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$denseRank
</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
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
dense rank
from the
<code class="leafygreen-ui-1l06pbn">
cakeSalesWithDuplicates
</code>
collection for each
<code class="leafygreen-ui-1l06pbn">
state
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.cakeSalesWithDuplicates.aggregate( [\n {\n $setWindowFields: {\n partitionBy: \"$state\",\n sortBy: { quantity: -1 },\n output: {\n denseRankQuantityForState: {\n $denseRank: {}\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">cakeSalesWithDuplicates</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">"$state"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sortBy</span>: <!-- -->{ <span class="lg-highlight-attr">quantity</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">denseRankQuantityForState</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$denseRank</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 the example:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
partitionBy: "$state"
</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">
state
</code>
. There are partitions for
<code class="leafygreen-ui-1l06pbn">
CA
</code>
and
<code class="leafygreen-ui-1l06pbn">
WA
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
sortBy: { quantity: -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">
quantity
</code>
in descending order (
<code class="leafygreen-ui-1l06pbn">
-1
</code>
), so the highest
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
is first.
</p>
</li>
</ul>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
output
</code>
sets the
<code class="leafygreen-ui-1l06pbn">
denseRankQuantityForState
</code>
field to the
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
dense rank using
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-denseRank" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$denseRank
</code>
.
</span>
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
In the following example output:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The documents with the same
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
and
<code class="leafygreen-ui-1l06pbn">
state
</code>
have the same
rank and there is no gap between the ranks. This differs from
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/rank/#mongodb-group-grp.-rank">
<code class="leafygreen-ui-1l06pbn">
$rank
</code>
</a>
that has a gap between the ranks (for an example, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/rank/#std-label-rank-duplicate-null-missing-values-example">
Rank Partitions Containing Duplicate Values, Nulls, or Missing Data
</a>
).
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The document with the
<code class="leafygreen-ui-1l06pbn">
null
</code>
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
and then the document with
the missing
<code class="leafygreen-ui-1l06pbn">
quantity
</code>
are ranked the lowest in the output for the
<code class="leafygreen-ui-1l06pbn">
CA
</code>
partition. This sorting is the result of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-type-comparison-order/#std-label-bson-types-comparison-order">
BSON
comparison order
</a>
, which sorts
<code class="leafygreen-ui-1l06pbn">
null
</code>
and missing values after number values in this example.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ \"_id\" : 4, \"type\" : \"strawberry\", \"orderDate\" : ISODate(\"2019-05-18T16:09:01Z\"),\n \"state\" : \"CA\", \"price\" : 41, \"quantity\" : 162, \"denseRankQuantityForState\" : 1 }\n{ \"_id\" : 9, \"type\" : \"strawberry\", \"orderDate\" : ISODate(\"2020-05-11T16:09:01Z\"),\n \"state\" : \"CA\", \"price\" : 39, \"quantity\" : 162, \"denseRankQuantityForState\" : 1 }\n{ \"_id\" : 2, \"type\" : \"vanilla\", \"orderDate\" : ISODate(\"2021-01-11T06:31:15Z\"),\n \"state\" : \"CA\", \"price\" : 12, \"quantity\" : 145, \"denseRankQuantityForState\" : 2 }\n{ \"_id\" : 0, \"type\" : \"chocolate\", \"orderDate\" : ISODate(\"2020-05-18T14:10:30Z\"),\n \"state\" : \"CA\", \"price\" : 13, \"quantity\" : 120, \"denseRankQuantityForState\" : 3 }\n{ \"_id\" : 10, \"type\" : \"strawberry\", \"orderDate\" : ISODate(\"2020-05-11T16:09:01Z\"),\n \"state\" : \"CA\", \"price\" : 39, \"quantity\" : null, \"denseRankQuantityForState\" : 4 }\n{ \"_id\" : 11, \"type\" : \"strawberry\", \"orderDate\" : ISODate(\"2020-05-11T16:09:01Z\"),\n \"state\" : \"CA\", \"price\" : 39, \"denseRankQuantityForState\" : 5 }\n{ \"_id\" : 1, \"type\" : \"chocolate\", \"orderDate\" : ISODate(\"2021-03-20T11:30:05Z\"),\n \"state\" : \"WA\", \"price\" : 14, \"quantity\" : 140, \"denseRankQuantityForState\" : 1 }\n{ \"_id\" : 5, \"type\" : \"strawberry\", \"orderDate\" : ISODate(\"2019-01-08T06:12:03Z\"),\n \"state\" : \"WA\", \"price\" : 43, \"quantity\" : 134, \"denseRankQuantityForState\" : 2 }\n{ \"_id\" : 6, \"type\" : \"strawberry\", \"orderDate\" : ISODate(\"2020-01-08T06:12:03Z\"),\n \"state\" : \"WA\", \"price\" : 41, \"quantity\" : 134, \"denseRankQuantityForState\" : 2 }\n{ \"_id\" : 7, \"type\" : \"strawberry\", \"orderDate\" : ISODate(\"2020-01-01T06:12:03Z\"),\n \"state\" : \"WA\", \"price\" : 34, \"quantity\" : 134, \"denseRankQuantityForState\" : 2 }\n{ \"_id\" : 8, \"type\" : \"strawberry\", \"orderDate\" : ISODate(\"2020-01-02T06:12:03Z\"),\n \"state\" : \"WA\", \"price\" : 40, \"quantity\" : 134, \"denseRankQuantityForState\" : 2 }\n{ \"_id\" : 3, \"type\" : \"vanilla\", \"orderDate\" : ISODate(\"2020-02-08T13:13:23Z\"),\n \"state\" : \"WA\", \"price\" : 13, \"quantity\" : 104, \"denseRankQuantityForState\" : 3 }","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-number">4</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"strawberry"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2019-05-18T16:09:01Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">41</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">162</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</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">9</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"strawberry"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-05-11T16:09:01Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">39</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">162</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</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">2</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"vanilla"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-01-11T06:31:15Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">12</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">145</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</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>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</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-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">13</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">120</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</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">10</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"strawberry"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-05-11T16:09:01Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">39</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</span> <!-- -->: <span class="lg-highlight-number">4</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-number">11</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"strawberry"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-05-11T16:09:01Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"CA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">39</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</span> <!-- -->: <span class="lg-highlight-number">5</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">"type"</span> <!-- -->: <span class="lg-highlight-string">"chocolate"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-20T11:30:05Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">14</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">140</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</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">5</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"strawberry"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2019-01-08T06:12:03Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">43</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">134</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</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">6</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"strawberry"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-01-08T06:12:03Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">41</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">134</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</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">7</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"strawberry"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-01-01T06:12:03Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">34</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">134</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</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">8</span>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"strawberry"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-01-02T06:12:03Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">40</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">134</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</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>, <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"vanilla"</span>, <span class="lg-highlight-string">"orderDate"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2020-02-08T13:13:23Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"state"</span> <!-- -->: <span class="lg-highlight-string">"WA"</span>, <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">13</span>, <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">104</span>, <span class="lg-highlight-string">"denseRankQuantityForState"</span> <!-- -->: <span class="lg-highlight-number">3</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/add-member-to-shard/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Add a Member to a Shard
<a class="headerlink leafygreen-ui-1kepx47" href="#add-a-member-to-a-shard" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-a-member-to-a-shard">
</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-cpmeq5" href="#next-steps" target="_self">
<span>
Next Steps
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
You can add members to an existing shard in a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharded-cluster">
sharded cluster
</a>
.
You might want to add a member to a shard for the same reasons you'd
want to add a member to any
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-replica-set">
replica set
</a>
. For example,
increasing the number of members provides additional candidates to replace
a primary in the event of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/replication/#std-label-replication-auto-failover">
failover
</a>
.
Additional members also increase data redundancy and replica set
availability.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information, refer to
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-architectures/#std-label-replica-set-architecture">
Replica Set Deployment Architectures
<!-- -->
.
</a>
</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">
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>
</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">
To add a member to a shard replica set, you need:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
An active sharded cluster replica set.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A new host server for the new member. The new host server must be able
to support your sharded data set and be accessible by the active replica
set through the network.
</p>
</li>
</ol>
<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>
</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":"Prepare the new member's data directory using one of the\nfollowing strategies:Have the new member automatically sync data from an existing member.\nThis process takes time but does not require administrator intervention.Make sure the new member's data directory does not contain data. The\nnew member will copy the data from an existing member.If the new member is in a recovering state, it must exit and\nbecome a secondary before MongoDB\ncan copy all data as part of the replication process.Manually copy the data directory from an existing member. The new\nmember becomes a secondary member and will catch up to the current\nstate of the replica set. Copying the data over may shorten the\namount of time it takes for the new member to sync with the other\nreplica set members.Ensure that you can copy the data directory to the new member and\nbegin replication within the window allowed by the oplog. Otherwise, the new instance will have\nto perform an initial sync, which completely resynchronizes the\ndata, as described in Resync a Member of a Self-Managed Replica Set.To check the current state of replica set members with regards to the\noplog, use rs.printReplicationInfo().For background on replication deployment patterns, see the\nReplica Set Deployment Architectures document.","name":"Prepare the Data Directory"},{"@type":"HowToStep","text":"Specify the data directory\nand the replica set name. The following example specifies the\n/srv/mongodb/db0 data directory and the rs0 replica set: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.For more information on configuration options, see the\nmongod manual page.You can specify the data directory, replica set name, and the ip\nbinding in the mongod.conf configuration file, and start the\nmongod with the following command:","name":"Start the new mongod instance"},{"@type":"HowToStep","text":"You can only add members while connected to the primary. To connect to\nthe primary, use mongosh. Replace the host and port values\nwith relevant values for your deployment:If you do not know which member is the primary, connect to any member\nof the replica set and issue the db.hello() command.","name":"Connect to the replica set's primary"},{"@type":"HowToStep","text":"Pass the member configuration document to the\nmethod. For example, to add a member at host\nmongodb3.example.net, issue the following command:","name":"Use rs.add() to add the new member to the replica set"},{"@type":"HowToStep","text":"To check the state of the replica set members, run\nrs.status():","name":"Ensure that the new member has reached SECONDARY state"}],"name":"Steps","image":"https://webimages.mongodb.com/_com_assets/cms/kuyj2focmkbxv7gh3-stacked_default_slate_blue.svg?auto=format%252Ccompress"}
</script>
<div class="css-qum8nr eccljxs0">
<div class="css-vq8pj5 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-18jxsvu e1ckbxcu2">
1
</div>
</div>
<div class="css-clrhqf">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Prepare the Data Directory
<a class="headerlink leafygreen-ui-1kepx47" href="#prepare-the-data-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="prepare-the-data-directory">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Prepare the new member's
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-dbpath">
data directory
</a>
using one of the
following strategies:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Have the new member automatically sync data from an existing member.
This process takes time but does not require administrator intervention.
</p>
<p class="leafygreen-ui-1kp3ins">
Make sure the new member's data directory
<em>
does not
</em>
contain data. The
new member will copy the data from an existing member.
</p>
<p class="leafygreen-ui-1kp3ins">
If the new member is in a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-recovering">
recovering
</a>
state, it must exit and
become a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary">
secondary
</a>
before MongoDB
can copy all data as part of the replication process.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Manually copy the data directory from an existing member. The new
member becomes a secondary member and will catch up to the current
state of the replica set. Copying the data over may shorten the
amount of time it takes for the new member to sync with the other
replica set members.
</p>
<p class="leafygreen-ui-1kp3ins">
Ensure that you can copy the data directory to the new member and
begin replication within the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-oplog/#std-label-replica-set-oplog-sizing">
window allowed by the oplog
</a>
. Otherwise, the new instance will have
to perform an initial sync, which completely resynchronizes the
data, as described in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/resync-replica-set-member/#std-label-resync-replica-member">
Resync a Member of a Self-Managed Replica Set
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To check the current state of replica set members with regards to the
oplog, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/rs.printReplicationInfo/#mongodb-method-rs.printReplicationInfo">
<code class="leafygreen-ui-1l06pbn">
rs.printReplicationInfo()
</code>
.
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For background on replication deployment patterns, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-architectures/">
Replica Set Deployment Architectures
</a>
document.
</p>
</section>
</div>
</div>
<div class="css-vq8pj5 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-18jxsvu e1ckbxcu2">
2
</div>
</div>
<div class="css-clrhqf">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Start the new
<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 class="headerlink leafygreen-ui-1kepx47" href="#start-the-new--instance" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-new--instance">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Specify the data directory
and the replica set name. The following example specifies the
<code class="leafygreen-ui-1l06pbn">
/srv/mongodb/db0
</code>
data directory and the
<code class="leafygreen-ui-1l06pbn">
rs0
</code>
replica set:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --dbpath /srv/mongodb/db0 --replSet rs0 --bind_ip localhost,<ip>","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 --dbpath /srv/mongodb/db0 --replSet rs0 --bind_ip localhost,<ip address of the mongod host></td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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>
<p class="leafygreen-ui-1kp3ins">
For more information on configuration options, 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>
manual page.
</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">
You can specify the data directory, replica set name, and the ip
binding in the
<code class="leafygreen-ui-1l06pbn">
mongod.conf
</code>
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/">
configuration file
</a>
, and 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 following command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongod --config /etc/mongod.conf","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 /etc/mongod.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>
</div>
</div>
</section>
</div>
</div>
<div class="css-vq8pj5 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-18jxsvu e1ckbxcu2">
3
</div>
</div>
<div class="css-clrhqf">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Connect to the replica set's primary
<a class="headerlink leafygreen-ui-1kepx47" href="#connect-to-the-replica-set-s-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="connect-to-the-replica-set-s-primary">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can only add members while connected to the primary. To connect to
the primary, use
<code class="leafygreen-ui-1l06pbn">
mongosh
</code>
. Replace the
<code class="leafygreen-ui-1l06pbn">
host
</code>
and
<code class="leafygreen-ui-1l06pbn">
port
</code>
values
with relevant values for your deployment:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"mongosh --host mongodb0.example.com --port 28015","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 --host mongodb0.example.com --port 28015</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 know which member is the primary, connect to any member
of the replica set and issue the
<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>
command.
</p>
</section>
</div>
</div>
<div class="css-vq8pj5 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-18jxsvu e1ckbxcu2">
4
</div>
</div>
<div class="css-clrhqf">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
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 to the replica set
<a class="headerlink leafygreen-ui-1kepx47" href="#use--to-add-the-new-member-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="use--to-add-the-new-member-to-the-replica-set">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Pass the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members">
<code class="leafygreen-ui-1l06pbn">
member configuration document
</code>
</a>
to the
method. For example, to add a member at host
<code class="leafygreen-ui-1l06pbn">
mongodb3.example.net
</code>
, issue the following command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"rs.add( { host: \"mongodb3.example.net:27017\", priority: 0, votes: 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">rs.<span class="lg-highlight-title lg-highlight-function">add</span>( <!-- -->{ <span class="lg-highlight-attr">host</span>: <span class="lg-highlight-string">"mongodb3.example.net:27017"</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>
</section>
</div>
</div>
<div class="css-vq8pj5 e1ckbxcu1">
<div class="css-18fui5z e1ckbxcu0">
<div class="css-18jxsvu e1ckbxcu2">
5
</div>
</div>
<div class="css-clrhqf">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
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
<a class="headerlink leafygreen-ui-1kepx47" href="#ensure-that-the-new-member-has-reached--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="ensure-that-the-new-member-has-reached--state">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
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>
</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">
Once the newly added 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, 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
the newly added member's
<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>
and
<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>
if needed.
</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">
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>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" 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>
</h3>
<p class="leafygreen-ui-1kp3ins">
If
<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>
returns the configuration document for
<code class="leafygreen-ui-1l06pbn">
mongodb3.example.net:27017
</code>
as the fifth element 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, to update its priority and votes to
<code class="leafygreen-ui-1l06pbn">
1
</code>
, use the following sequence of operations:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"var cfg = rs.conf();\ncfg.members[4].priority = 1\ncfg.members[4].votes = 1\nrs.reconfig(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"><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">cfg.<span class="lg-highlight-property">members</span>[<span class="lg-highlight-number">4</span>].<span class="lg-highlight-property">priority</span> <!-- -->= <span class="lg-highlight-number">1</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">cfg.<span class="lg-highlight-property">members</span>[<span class="lg-highlight-number">4</span>].<span class="lg-highlight-property">votes</span> <!-- -->= <span class="lg-highlight-number">1</span></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>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/rotate-x509-membership-certificates/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Rolling Update of x.509 Certificates that Contain New DN on Self-Managed Clusters
<a class="headerlink leafygreen-ui-1kepx47" href="#rolling-update-of-x.509-certificates-that-contain-new-dn-on-self-managed-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="rolling-update-of-x.509-certificates-that-contain-new-dn-on-self-managed-clusters">
</div>
</a>
</h1>
<p class="leafygreen-ui-1kp3ins">
Members of a replica set or a sharded cluster can use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-internal-authentication/#std-label-internal-auth-x509">
x.509
certificates for membership authentication
</a>
to
identify each other as memers of the same deployment.
</p>
<p class="leafygreen-ui-1kp3ins">
To identify each others as members of the same deployment, the
Organization attributes (
<code class="leafygreen-ui-1l06pbn">
O
</code>
's), the Organizational Unit attributes
(
<code class="leafygreen-ui-1l06pbn">
OU
</code>
's), and the Domain Components (
<code class="leafygreen-ui-1l06pbn">
DC
</code>
's) from the
certificates must match.
</p>
<p class="leafygreen-ui-1kp3ins">
In some situations, you may need to update the member certificates to
new certificates with the new Distinguished Name (
<code class="leafygreen-ui-1l06pbn">
DN
</code>
), such as if
an organization changes its name. You can use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.tlsX509ClusterAuthDNOverride">
<code class="leafygreen-ui-1l06pbn">
tlsX509ClusterAuthDNOverride
</code>
</a>
parameter to set an alternative
<code class="leafygreen-ui-1l06pbn">
DN
</code>
to use in matching. With this parameter, you can perform a rolling update of the
certificates to new certificates with a different
<code class="leafygreen-ui-1l06pbn">
DN
</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 a rolling update, member certificates are updated one at a time, and
your deployment does not incur any downtime. To perform a rolling update
to rotate certificates on a cluster that uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.tls.clusterAuthX509">
<code class="leafygreen-ui-1l06pbn">
net.tls.clusterAuthX509
</code>
</a>
settings or on a cluster that will use
these settings after the update, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/rotate-x509-member-cert/#std-label-x509-rotate-member-certs">
Rotate X.509 Certificates on Self-Managed Clusters
<!-- -->
.
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following tutorial describes the process to update the
certificates for a replica set without downtime.
</p>
<p class="leafygreen-ui-1kp3ins">
Consider a replica set where each member's certificates
(
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.tls.clusterFile">
<code class="leafygreen-ui-1l06pbn">
clusterFile
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.tls.certificateKeyFile">
<code class="leafygreen-ui-1l06pbn">
certificateKeyFile
</code>
</a>
) with
<code class="leafygreen-ui-1l06pbn">
DN
</code>
value that contains
<code class="leafygreen-ui-1l06pbn">
"OU=10gen Server,O=10gen"
</code>
(additional attributes are not considered
as part of the comparison):
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"net.tls.mode: requireTLS\nnet.tls.certificateKeyFile: \"./mycerts/10gen-server1.pem\"\nnet.tls.CAFile: \"./mycerts/ca.pem\"\n\nsecurity.clusterAuthMode: x509\nnet.tls.clusterFile: \"./mycerts/10gen-cluster1.pem\"\nnet.tls.clusterCAFile: \"./mycerts/ca.pem\"","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.tls.mode:</span> <span class="lg-highlight-string">requireTLS</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">net.tls.certificateKeyFile:</span> <span class="lg-highlight-string">"./mycerts/10gen-server1.pem"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">net.tls.CAFile:</span> <span class="lg-highlight-string">"./mycerts/ca.pem"</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-attr">security.clusterAuthMode:</span> <span class="lg-highlight-string">x509</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">net.tls.clusterFile:</span> <span class="lg-highlight-string">"./mycerts/10gen-cluster1.pem"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">net.tls.clusterCAFile:</span> <span class="lg-highlight-string">"./mycerts/ca.pem"</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 procedure updates the members' certificates
(
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.tls.clusterFile">
<code class="leafygreen-ui-1l06pbn">
clusterFile
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.tls.certificateKeyFile">
<code class="leafygreen-ui-1l06pbn">
certificateKeyFile
</code>
</a>
) to new certificates that have a
<code class="leafygreen-ui-1l06pbn">
DN
</code>
value with
<code class="leafygreen-ui-1l06pbn">
"OU=MongoDB Server,O=MongoDB"
</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">
The following procedure assumes that the new x.509 certificates meet
membership certificate all other requirements. For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-x.509/#std-label-x509-member-certificate-requirements">
Member Certificate Requirements
</a>
</p>
</div>
</div>
<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>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
0. Optional. Set Override Parameter on Running Members
<a class="headerlink leafygreen-ui-1kepx47" href="#0.-optional.-set-override-parameter-on-running-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="0.-optional.-set-override-parameter-on-running-members">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The procedure requires that all members of the deployment be
restarted. As the settings will not take into consideration until
you restart all members, your instances may log messages until the
procedure completes.
</p>
<p class="leafygreen-ui-1kp3ins">
To avoid these messages, you can set the override parameter on
running members.
</p>
<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>
directly to each member of the
deployment and set the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.tlsX509ClusterAuthDNOverride">
<code class="leafygreen-ui-1l06pbn">
tlsX509ClusterAuthDNOverride
</code>
</a>
parameter to the new certificate
<code class="leafygreen-ui-1l06pbn">
DN
</code>
:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { setParameter: 1, tlsX509ClusterAuthDNOverride: \"OU=MongoDB Server,O=MongoDB\" } )","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">setParameter</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">tlsX509ClusterAuthDNOverride</span>: <span class="lg-highlight-string">"OU=MongoDB Server,O=MongoDB"</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">
1. Modify the Configuration of ALL Members
<a class="headerlink leafygreen-ui-1kepx47" href="#1.-modify-the-configuration-of-all-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="1.-modify-the-configuration-of-all-members">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Modify each member's configuration:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Update
<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>
to the new certificate.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Update
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-net.tls.clusterFile">
<code class="leafygreen-ui-1l06pbn">
net.tls.clusterFile
</code>
</a>
to the new certificate.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Set
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.tlsX509ClusterAuthDNOverride">
<code class="leafygreen-ui-1l06pbn">
tlsX509ClusterAuthDNOverride
</code>
</a>
parameter to the new
certificate
<code class="leafygreen-ui-1l06pbn">
DN
</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":"net.tls.mode: requireTLS\nnet.tls.certificateKeyFile: \"./mycerts/mongodb-server1.pem\"\nnet.tls.CAFile: \"./mycerts/ca.pem\"\n\nsecurity.clusterAuthMode: x509\n\nnet.tls.clusterFile: \"./mycerts/mongodb-cluster1.pem\"\nnet.tls.clusterCAFile: \"./mycerts/ca.pem\"\n\nsetParameter:\n tlsX509ClusterAuthDNOverride: \"OU=MongoDB Server,O=MongoDB\"","programmingLanguage":"YAML"}
</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 yaml leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">net.tls.mode:</span> <span class="lg-highlight-string">requireTLS</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">net.tls.certificateKeyFile:</span> <span class="lg-highlight-string">"./mycerts/mongodb-server1.pem"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">net.tls.CAFile:</span> <span class="lg-highlight-string">"./mycerts/ca.pem"</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-attr">security.clusterAuthMode:</span> <span class="lg-highlight-string">x509</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-attr">net.tls.clusterFile:</span> <span class="lg-highlight-string">"./mycerts/mongodb-cluster1.pem"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">net.tls.clusterCAFile:</span> <span class="lg-highlight-string">"./mycerts/ca.pem"</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-attr">setParameter:</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">tlsX509ClusterAuthDNOverride:</span> <span class="lg-highlight-string">"OU=MongoDB Server,O=MongoDB"</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 settings will not take into consideration until you restart each
member (see next step).
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
2. Restart Each Member
<a class="headerlink leafygreen-ui-1kepx47" href="#2.-restart-each-member" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="2.-restart-each-member">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Once you have modified the configuration for all the members,
restart each secondary and then the primary.
</p>
<p class="leafygreen-ui-1kp3ins">
<strong>
For each secondary member
</strong>
, 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
member and:
</p>
<ol class="leafygreen-ui-rioki0" type="a">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
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 to shut down the member:
</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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Restart the member.
</p>
<p class="leafygreen-ui-1kp3ins">
Ensure the 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 before
restarting the next secondary.
</p>
</li>
</ol>
<p class="leafygreen-ui-1kp3ins">
<strong>
For the primary
</strong>
, 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 member and
</p>
<ol class="leafygreen-ui-rioki0" type="a">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
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 member:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"rs.stepDown()","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>(<!-- -->)</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
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 to shut down the member:
</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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Restart the member.
</p>
</li>
</ol>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
3. Remove the
<code class="leafygreen-ui-1l06pbn">
tlsX509ClusterAuthDNOverride
</code>
Setting
<a class="headerlink leafygreen-ui-1kepx47" href="#3.-remove-the-tlsx509clusterauthdnoverride-setting" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="3.-remove-the-tlsx509clusterauthdnoverride-setting">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Once all the members have been restarted with the new certificate,
you can remove the
<code class="leafygreen-ui-1l06pbn">
tlsX509ClusterAuthDNOverride
</code>
parameter from
the configuration.
</p>
<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":"net.tls.mode: requireTLS\nnet.tls.certificateKeyFile: \"./mycerts/mongodb-server1.pem\"\nnet.tls.CAFile: \"./mycerts/ca.pem\"\n\nsecurity.clusterAuthMode: x509\n\nnet.tls.clusterFile: \"./mycerts/mongodb-cluster1.pem\"\nnet.tls.clusterCAFile: \"./mycerts/ca.pem\"","programmingLanguage":"YAML"}
</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 yaml leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">net.tls.mode:</span> <span class="lg-highlight-string">requireTLS</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">net.tls.certificateKeyFile:</span> <span class="lg-highlight-string">"./mycerts/mongodb-server1.pem"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">net.tls.CAFile:</span> <span class="lg-highlight-string">"./mycerts/ca.pem"</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-attr">security.clusterAuthMode:</span> <span class="lg-highlight-string">x509</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-attr">net.tls.clusterFile:</span> <span class="lg-highlight-string">"./mycerts/mongodb-cluster1.pem"</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">net.tls.clusterCAFile:</span> <span class="lg-highlight-string">"./mycerts/ca.pem"</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 settings will not take into consideration until you restart each
member.
</p>
</section>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/hour/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$hour (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-hour--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="-hour--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">
$hour
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-hour" 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.-hour">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns the hour portion of a date as a number between 0 and 23.
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-hour" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$hour
</code>
</span>
</a>
expression has the following
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
operator expression syntax
<!-- -->
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $hour: <dateexpression> }","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">$hour</span>: <<!-- -->dateExpression<!-- -->> <!-- -->}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
An
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-date">
Date
</a>
, a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-timestamp">
Timestamp
</a>
, or an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-object-id">
ObjectID
<!-- -->
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A document with this format:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ date: <dateexpression>, timezone: <tzexpression> }","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">date</span>: <<!-- -->dateExpression<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">timezone</span>: <<!-- -->tzExpression<!-- -->> <!-- -->}</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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>
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">
date
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The date to which the operator is applied.
<code class="leafygreen-ui-1l06pbn">
<dateExpression>
</code>
must be a valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-date">
Date
</a>
, a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-timestamp">
Timestamp
</a>
,
or an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-document-bson-type-object-id">
ObjectID
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
timezone
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Optional. The timezone of the operation result.
<code class="leafygreen-ui-1l06pbn">
<tzExpression>
</code>
must be a valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
that resolves to a string formatted as either
an
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones" rel="noopener noreferrer">
<span>
Olson Timezone Identifier
</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>
or a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/wiki/List_of_UTC_time_offsets" rel="noopener noreferrer">
<span>
UTC Offset
</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>
.
If no
<code class="leafygreen-ui-1l06pbn">
timezone
</code>
is provided, the result is in UTC.
</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>
Format
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Examples
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
Olson Timezone Identifier
</code>
</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":"\"America/New_York\"\n\"Europe/London\"\n\"GMT\""}
</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">"America/New_York"</td></tr><tr class=""><td class="leafygreen-ui-7razhx">"Europe/London"</td></tr><tr class=""><td class="leafygreen-ui-7razhx">"GMT"</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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>
<code class="leafygreen-ui-1l06pbn">
UTC Offset
</code>
</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":"+/-[hh]:[mm], e.g. \"+04:45\"\n+/-[hh][mm], e.g. \"-0530\"\n+/-[hh], e.g. \"+03\""}
</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">+/-[hh]:[mm], e.g. "+04:45"</td></tr><tr class=""><td class="leafygreen-ui-7razhx">+/-[hh][mm], e.g. "-0530"</td></tr><tr class=""><td class="leafygreen-ui-7razhx">+/-[hh], e.g. "+03"</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</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>
<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="leafygreen-ui-15dvrnp">
<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":"{ $hour: new Date(\"2016-01-01T12:00:00Z\") }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$hour</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2016-01-01T12:00:00Z"</span>) }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
12
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $hour: { date: new Date(\"Jan 7, 2003Z\") } }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$hour</span>: <!-- -->{ <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"Jan 7, 2003Z"</span>) } }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $hour: {\n date: new Date(\"August 14, 2011Z\"),\n timezone: \"America/Chicago\"\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">{ <span class="lg-highlight-attr">$hour</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-keyword">new</span> <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"August 14, 2011Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timezone</span>: <span class="lg-highlight-string">"America/Chicago"</span></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>
19
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $hour: ISODate(\"2017-10-19T00:00:00Z\") }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$hour</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2017-10-19T00:00:00Z"</span>) }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $hour: {\n date: ISODate(\"2017-10-19T00:00:00Z\"),\n timezone: \"+0530\"\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">{ <span class="lg-highlight-attr">$hour</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">date</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2017-10-19T00:00:00Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">timezone</span>: <span class="lg-highlight-string">"+0530"</span></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>
5
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $hour: \"March 28, 1976\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$hour</span>: <span class="lg-highlight-string">"March 28, 1976"</span> }</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">
error
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $hour: Date(\"2016-01-01\") }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$hour</span>: <span class="lg-highlight-title lg-highlight-class">Date</span>(<span class="lg-highlight-string">"2016-01-01"</span>) }</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">
error
</code>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ $hour: \"2009-04-09\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">$hour</span>: <span class="lg-highlight-string">"2009-04-09"</span> }</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">
error
</code>
</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>
$hour cannot take a string as an argument.
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
</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">
Consider a
<code class="leafygreen-ui-1l06pbn">
sales
</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":"{\n \"_id\" : 1,\n \"item\" : \"abc\",\n \"price\" : 10,\n \"quantity\" : 2,\n \"date\" : ISODate(\"2014-01-01T08:15:39.736Z\")\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">"item"</span> <!-- -->: <span class="lg-highlight-string">"abc"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"price"</span> <!-- -->: <span class="lg-highlight-number">10</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"quantity"</span> <!-- -->: <span class="lg-highlight-number">2</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"date"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2014-01-01T08:15:39.736Z"</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 uses the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-hour" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$hour
</code>
</span>
</a>
and other date
expressions to break down the
<code class="leafygreen-ui-1l06pbn">
date
</code>
field:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.sales.aggregate(\n [\n {\n $project:\n {\n year: { $year: \"$date\" },\n month: { $month: \"$date\" },\n day: { $dayOfMonth: \"$date\" },\n hour: { $hour: \"$date\" },\n minutes: { $minute: \"$date\" },\n seconds: { $second: \"$date\" },\n milliseconds: { $millisecond: \"$date\" },\n dayOfYear: { $dayOfYear: \"$date\" },\n dayOfWeek: { $dayOfWeek: \"$date\" }\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">sales</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">year</span>: <!-- -->{ <span class="lg-highlight-attr">$year</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">month</span>: <!-- -->{ <span class="lg-highlight-attr">$month</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">day</span>: <!-- -->{ <span class="lg-highlight-attr">$dayOfMonth</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">hour</span>: <!-- -->{ <span class="lg-highlight-attr">$hour</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">minutes</span>: <!-- -->{ <span class="lg-highlight-attr">$minute</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">seconds</span>: <!-- -->{ <span class="lg-highlight-attr">$second</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">milliseconds</span>: <!-- -->{ <span class="lg-highlight-attr">$millisecond</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">dayOfYear</span>: <!-- -->{ <span class="lg-highlight-attr">$dayOfYear</span>: <span class="lg-highlight-string">"$date"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">dayOfWeek</span>: <!-- -->{ <span class="lg-highlight-attr">$dayOfWeek</span>: <span class="lg-highlight-string">"$date"</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 result:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"_id\" : 1,\n \"year\" : 2014,\n \"month\" : 1,\n \"day\" : 1,\n \"hour\" : 8,\n \"minutes\" : 15,\n \"seconds\" : 39,\n \"milliseconds\" : 736,\n \"dayOfYear\" : 1,\n \"dayOfWeek\" : 4,\n \"week\" : 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">"_id"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"year"</span> <!-- -->: <span class="lg-highlight-number">2014</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"month"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"day"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hour"</span> <!-- -->: <span class="lg-highlight-number">8</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"minutes"</span> <!-- -->: <span class="lg-highlight-number">15</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"seconds"</span> <!-- -->: <span class="lg-highlight-number">39</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"milliseconds"</span> <!-- -->: <span class="lg-highlight-number">736</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"dayOfYear"</span> <!-- -->: <span class="lg-highlight-number">1</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"dayOfWeek"</span> <!-- -->: <span class="lg-highlight-number">4</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"week"</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>
</section>
</section>
|
https://www.mongodb.com/docs/manual/core/replica-set-priority-0-member/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Priority 0 Replica Set Members
<a class="headerlink leafygreen-ui-1kepx47" href="#priority-0-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="priority-0-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="#priority-0-members-as-standbys" target="_self">
<span>
Priority 0 Members as Standbys
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#failover-considerations" target="_self">
<span>
Failover Considerations
</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>
<p class="leafygreen-ui-1kp3ins">
A
<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>
member is a member that
<strong>
cannot
</strong>
become
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
and
<strong>
cannot
</strong>
trigger
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-election">
elections
</a>
. Priority 0 members can acknowledge write
operations issued with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-write-concern">
write concern
</a>
of
<code class="leafygreen-ui-1l06pbn">
w : <number>
</code>
. For
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
write concern, the priority 0
member must also be a voting member (i.e.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-.votes">
<code class="leafygreen-ui-1l06pbn">
members[n].votes
</code>
</a>
is
greater than
<code class="leafygreen-ui-1l06pbn">
0
</code>
) to acknowledge the write. Non-voting replica set
members (i.e.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-.votes">
<code class="leafygreen-ui-1l06pbn">
members[n].votes
</code>
</a>
is
<code class="leafygreen-ui-1l06pbn">
0
</code>
) cannot contribute to
acknowledging write operations with
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
write concern.
</p>
<p class="leafygreen-ui-1kp3ins">
Other than the aforementioned restrictions, secondaries that have
<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>
function as normal
secondaries: they maintain a copy of the data set, accept read
operations, and vote in elections.
</p>
<p class="leafygreen-ui-1kp3ins">
Configuring a replica set member 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>
might be desired if the
particular member is deployed in a data center that is distant from
the main deployment and therefore has higher latency. It may serve local
read requests well, but might not be an ideal candidate to perform the
duties of a primary due to its latency.
</p>
<p class="leafygreen-ui-1kp3ins">
For this situation, the following diagram shows a data center on the
left which hosts the primary and a secondary, and a data center on the
right which hosts a secondary that has been configured to have
<em>
priority 0
</em>
to prevent it from becoming primary. Because of this
setting, only the members in the left data center are eligible to become
primary in an election.
</p>
<div class="figure css-5vkxs0" style="width:700px">
<img alt="Diagram of a 3 member replica set distributed across two data centers. Replica set includes a priority 0 member." class="leafygreen-ui-1qnf57e" height="280" src="/docs/manual/images/replica-set-three-members-geographically-distributed.bakedsvg.svg" style="--border-color:#C1C7C6" width="740"/>
</div>
<p class="leafygreen-ui-1kp3ins">
Compare this to the default priority for replica set members,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.members-n-.priority">
<code class="leafygreen-ui-1l06pbn">
priority 1
</code>
</a>
, where either of the
secondaries in this scenario would be eligible to serve as primary. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-architecture-geographically-distributed/">
Replica Sets Distributed Across Two or More Data Centers
</a>
for
more information.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Priority 0 Members as Standbys
<a class="headerlink leafygreen-ui-1kepx47" href="#priority-0-members-as-standbys" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="priority-0-members-as-standbys">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
A secondary 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>
can
function as a standby. In some replica sets, it might not be possible
to add a new member in a reasonable amount of time. A standby member
keeps a current copy of the data to be able to replace an unavailable
member.
</p>
<p class="leafygreen-ui-1kp3ins">
In many cases, you need not set standby to
<em>
priority 0
</em>
. However, in replica
sets with varied hardware or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-architecture-geographically-distributed/#std-label-replica-set-geographical-distribution">
geographic distribution
</a>
, a
<em>
priority 0
</em>
standby
ensures that only certain members become primary.
</p>
<p class="leafygreen-ui-1kp3ins">
A
<em>
priority 0
</em>
standby may also be valuable for some members of a set
with different hardware or workload profiles. In these cases, deploy a
member with
<em>
priority 0
</em>
so it can't become primary. Also consider
using a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-hidden-member/#std-label-replica-set-hidden-members">
hidden member
</a>
for this
purpose.
</p>
<p class="leafygreen-ui-1kp3ins">
If your set already has seven voting members, also configure the
member as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-elections/#std-label-replica-set-non-voting-members">
non-voting
<!-- -->
.
</a>
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Failover Considerations
<a class="headerlink leafygreen-ui-1kepx47" href="#failover-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="failover-considerations">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
When configuring a secondary to have
<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>
, consider potential failover patterns, including
all possible network partitions. Always ensure that your main data
center contains both a quorum of voting members and members that are
eligible to be primary.
</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">
To configure a secondary to have
<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>
, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/configure-secondary-only-replica-set-member/">
Prevent a Self-Managed Secondary from Becoming Primary
<!-- -->
.
</a>
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/tutorial/manage-sharded-cluster-balancer/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Manage Sharded Cluster Balancer
<a class="headerlink leafygreen-ui-1kepx47" href="#manage-sharded-cluster-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="manage-sharded-cluster-balancer">
</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="#check-the-balancer-state" target="_self">
<span>
Check the Balancer State
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#check-if-balancer-is-running" target="_self">
<span>
Check if Balancer is Running
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#configure-default-range-size" target="_self">
<span>
Configure Default Range Size
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#schedule-the-balancing-window" target="_self">
<span>
Schedule the Balancing Window
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#check-balancing-window" target="_self">
<span>
Check Balancing Window
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#remove-a-balancing-window-schedule" target="_self">
<span>
Remove a Balancing Window Schedule
</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="#enable-the-balancer" target="_self">
<span>
Enable the Balancer
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#disable-balancing-during-backups" target="_self">
<span>
Disable Balancing During Backups
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#disable-balancing-on-a-collection" target="_self">
<span>
Disable Balancing on a Collection
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#enable-balancing-on-a-collection" target="_self">
<span>
Enable Balancing on a Collection
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#confirm-balancing-is-enabled-or-disabled" target="_self">
<span>
Confirm Balancing is Enabled or Disabled
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#change-replication-behavior-for-chunk-migration" target="_self">
<span>
Change Replication Behavior for Chunk Migration
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#change-the-maximum-storage-size-for-a-given-shard" target="_self">
<span>
Change the Maximum Storage Size for a Given Shard
</span>
</a>
</li>
</ul>
</div>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
6.1
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
This page describes common administrative procedures related
to balancing. For an introduction to balancing, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-balancer-administration/#std-label-sharding-balancing">
Sharded Cluster Balancer
</a>
. For lower level information on balancing, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-balancer-administration/#std-label-sharding-balancing-internals">
Balancer Internals
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The balancer process has moved 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>
instances
to the primary member of the config server replica set.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Check the Balancer State
<a class="headerlink leafygreen-ui-1kepx47" href="#check-the-balancer-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="check-the-balancer-state">
</div>
</a>
</h2>
<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>
checks if the balancer is enabled
(i.e. that the balancer is permitted to run).
<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>
does not check if the balancer is
actively migrating data.
</p>
<p class="leafygreen-ui-1kp3ins">
To see if the balancer is enabled in your
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharded-cluster">
sharded cluster
</a>
,
run the following command, which returns a boolean:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.getBalancerState()","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">getBalancerState</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 see if the balancer is enabled using
<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>
.
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.status/#mongodb-data-sh.status.balancer.currently-enabled">
<code class="leafygreen-ui-1l06pbn">
currently-enabled
</code>
</a>
field indicates
whether the balancer is enabled, and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.status/#mongodb-data-sh.status.balancer.currently-running">
<code class="leafygreen-ui-1l06pbn">
currently-running
</code>
</a>
field indicates if the
balancer is currently running.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-balancing-is-running">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Check if Balancer is Running
<a class="headerlink leafygreen-ui-1kepx47" href="#check-if-balancer-is-running" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-if-balancer-is-running">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To see if the balancer process is active in your
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharded-cluster">
cluster
<!-- -->
:
</a>
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Connect to any
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
in the cluster using 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>
shell.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use the following operation to determine if the balancer is running:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.isBalancerRunning()","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">isBalancerRunning</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sharded-cluster-config-default-chunk-size">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sharded-cluster-config-default-range-size">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Configure Default Range Size
<a class="headerlink leafygreen-ui-1kepx47" href="#configure-default-range-size" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-default-range-size">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
The default range size for a sharded cluster is 128 megabytes. In most
situations, the default size is appropriate for splitting and migrating
chunks. For information on how range size affects deployments, see
details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-data-partitioning/#std-label-sharding-range-size">
Range Size
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Changing the default range size affects ranges that are processes during
migrations and auto-splits but does not retroactively affect all ranges.
</p>
<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">
To configure default range size, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/modify-chunk-size-in-sharded-cluster/#std-label-tutorial-modifying-chunk-size">
Modify Range Size in a Sharded Cluster
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-schedule-balancing-window">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sharded-cluster-config-balancing-window">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Schedule the Balancing Window
<a class="headerlink leafygreen-ui-1kepx47" href="#schedule-the-balancing-window" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="schedule-the-balancing-window">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
In some situations, particularly when your data set grows slowly and a
migration can impact performance, it is useful to ensure
that the balancer is active only at certain times. By default, the
balancer process is always enabled and migrating chunks. The following
procedure specifies the
<code class="leafygreen-ui-1l06pbn">
activeWindow
</code>
, which is the timeframe during
which the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-balancer">
balancer
</a>
is able to migrate chunks:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"HowTo","steps":[{"@type":"HowToStep","text":"Connect to mongos using mongosh.You can connect to any mongos in the cluster."},{"@type":"HowToStep","text":"Switch to the Config Database.Issue the following command to switch to the config database."},{"@type":"HowToStep","text":"Ensure that the balancer is not stopped.The balancer will not activate in the stopped state.\nTo ensure that the balancer\nis not stopped, use sh.startBalancer(),\nas in the following:The balancer will not start if you are outside\nof the activeWindow timeframe.Starting in MongoDB 6.0.3, automatic chunk splitting is not performed.\nThis is because of balancing policy improvements. Auto-splitting commands\nstill exist, but do not perform an operation.In MongoDB versions earlier than 6.0.3, sh.startBalancer()\nalso enables auto-splitting for the sharded cluster."},{"@type":"HowToStep","text":"Modify the balancer's window.Set the activeWindow using updateOne(),\nas in the following:Replace <start-time> and <end-time> with time values using\ntwo digit hour and minute values (i.e. HH:MM) that specify the\nbeginning and end boundaries of the balancing window.For HH values, use hour values ranging from 00 - 23.For MM value, use minute values ranging from 00 - 59.For on-premises or self-managed sharded clusters, MongoDB evaluates the\nstart and stop times relative to the time zone of the primary member in\nthe config server replica set.For Atlas clusters, MongoDB evaluates the start and stop times\nrelative to the UTC timezone.The balancer window must be sufficient to complete the\nmigration of all data inserted during the day.As data insert rates can change based on activity and usage\npatterns, it is important to ensure that the balancing window\nyou select will be sufficient to support the needs of your\ndeployment."}],"name":"Schedule the Balancing Window","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">
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>
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>
.
<a class="headerlink leafygreen-ui-1kepx47" href="#connect-to-using" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-using">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can connect to any
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
in the cluster.
</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">
Switch to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/config-database/#std-label-config-database">
Config Database
</a>
.
<a class="headerlink leafygreen-ui-1kepx47" href="#switch-to-the" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-to-the">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Issue the following command to switch to the config database.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use 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">use 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>
</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">
Ensure that the balancer is not
<code class="leafygreen-ui-1l06pbn">
stopped
</code>
.
<a class="headerlink leafygreen-ui-1kepx47" href="#ensure-that-the-balancer-is-not-stopped" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-that-the-balancer-is-not-stopped">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The balancer will not activate in the
<code class="leafygreen-ui-1l06pbn">
stopped
</code>
state.
To ensure that the balancer
is not
<code class="leafygreen-ui-1l06pbn">
stopped
</code>
, use
<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>
,
as in the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.startBalancer()\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">sh.startBalancer()</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 balancer will not start if you are outside
of the
<code class="leafygreen-ui-1l06pbn">
activeWindow
</code>
timeframe.
</p>
<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.startBalancer/#mongodb-method-sh.startBalancer">
<code class="leafygreen-ui-1l06pbn">
sh.startBalancer()
</code>
</a>
also enables auto-splitting for the sharded cluster.
</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>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Modify the balancer's window.
<a class="headerlink leafygreen-ui-1kepx47" href="#modify-the-balancer-s-window" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-balancer-s-window">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Set the
<code class="leafygreen-ui-1l06pbn">
activeWindow
</code>
using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.updateOne/#mongodb-method-db.collection.updateOne">
<code class="leafygreen-ui-1l06pbn">
updateOne()
</code>
</a>
,
as in the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.settings.updateOne(\n { _id: \"balancer\" },\n { $set: { activeWindow : { start : \"<start-time>\", stop : \"<stop-time>\" } } },\n { upsert: true }\n)\n","programmingLanguage":"Shell"}
</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 shell leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.settings.updateOne(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> { _id: "balancer" },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> { $set: { activeWindow : { start : "<start-time>", stop : "<stop-time>" } } },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> { upsert: true }</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">
Replace
<code class="leafygreen-ui-1l06pbn">
<start-time>
</code>
and
<code class="leafygreen-ui-1l06pbn">
<end-time>
</code>
with time values using
two digit hour and minute values (i.e.
<code class="leafygreen-ui-1l06pbn">
HH:MM
</code>
) that specify the
beginning and end boundaries of the balancing window.
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For
<code class="leafygreen-ui-1l06pbn">
HH
</code>
values, use hour values ranging from
<code class="leafygreen-ui-1l06pbn">
00
</code>
-
<code class="leafygreen-ui-1l06pbn">
23
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
For
<code class="leafygreen-ui-1l06pbn">
MM
</code>
value, use minute values ranging from
<code class="leafygreen-ui-1l06pbn">
00
</code>
-
<code class="leafygreen-ui-1l06pbn">
59
</code>
.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For on-premises or self-managed sharded clusters, MongoDB evaluates the
start and stop times relative to the time zone of the primary member in
the config server replica set.
</p>
<p class="leafygreen-ui-1kp3ins">
For Atlas clusters, MongoDB evaluates the start and stop times
relative to the UTC timezone.
</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 balancer window must be sufficient to
<em>
complete
</em>
the
migration of all data inserted during the day.
</p>
<p class="leafygreen-ui-1kp3ins">
As data insert rates can change based on activity and usage
patterns, it is important to ensure that the balancing window
you select will be sufficient to support the needs of your
deployment.
</p>
</div>
</div>
</section>
</div>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-check-balancing-window">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Check Balancing Window
<a class="headerlink leafygreen-ui-1kepx47" href="#check-balancing-window" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-balancing-window">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To see the current balancing window, run the following
command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use config\ndb.settings.find( { _id: \"balancer\" } )","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 config</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">settings</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-string">"balancer"</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-sharding-balancing-remove-window">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Remove a Balancing Window Schedule
<a class="headerlink leafygreen-ui-1kepx47" href="#remove-a-balancing-window-schedule" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-balancing-window-schedule">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
If you have
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-sharding-schedule-balancing-window" target="_self">
<span>
set the balancing window
</span>
</a>
and wish to remove the schedule
so that the balancer is always running, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/update/unset/#mongodb-update-up.-unset">
<code class="leafygreen-ui-1l06pbn">
$unset
</code>
</a>
to clear
the
<code class="leafygreen-ui-1l06pbn">
activeWindow
</code>
, as in the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use config\ndb.settings.updateOne( { _id : \"balancer\" }, { $unset : { activeWindow : 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">use config</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">settings</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>( <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-string">"balancer"</span> }<!-- -->, <!-- -->{ <!-- -->$unset<!-- --> <!-- -->: <!-- -->{ <!-- -->activeWindow <!-- -->: <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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-balancing-disable-temporally">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-balancing-disable-temporarily">
</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>
<div class="leafygreen-ui-85da11" role="note">
<h2 class="leafygreen-ui-1ac4g5h">
Important
</h2>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
Leaving the balancer disabled for extended periods of time can lead to
unbalanced shards, which degrade cluster performance. Only disable the
balancer if necessary, and ensure that you
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-sharding-balancing-re-enable" target="_self">
<span>
re-enable the balancer
</span>
</a>
when maintenance is complete.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
By default, the balancer may run at any time and only moves chunks as
needed. To disable the balancer for a short period of time and prevent
all migration, use the following procedure:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Connect to any
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
in the cluster using 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>
shell.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Issue the following operation to disable the balancer:
</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">
If a migration is in progress, the system will complete the
in-progress migration before stopping.
</p>
<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>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
To verify that the balancer won't start, run the following command,
which returns
<code class="leafygreen-ui-1l06pbn">
false
</code>
if the balancer is disabled:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"sh.getBalancerState()","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">getBalancerState</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">
Optionally, to verify no migrations are in progress after disabling,
run the following operation in 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>
shell:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use config\nwhile( sh.isBalancerRunning() ) {\n print(\"waiting...\");\n sleep(1000);\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 config</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">while</span>( <!-- -->sh.<span class="lg-highlight-title lg-highlight-function">isBalancerRunning</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">"waiting..."</span>)<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-title lg-highlight-function">sleep</span>(<span class="lg-highlight-number">1000</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>
</ol>
<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 disable the balancer from a driver,
use the
<strong>
balancerStop
</strong>
command against the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database,
as in the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { balancerStop: 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">balancerStop</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-balancing-re-enable">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sharding-balancing-enable">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Enable the Balancer
<a class="headerlink leafygreen-ui-1kepx47" href="#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="enable-the-balancer">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Use this procedure if you have disabled the balancer and are ready to
re-enable it:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Connect to any
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
in the cluster using 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>
shell.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Issue one of the following operations to enable the balancer:
</p>
<p class="leafygreen-ui-1kp3ins">
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>
shell, issue:
</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>
<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 enable the balancer from a driver, use the
<strong>
balancerStart
</strong>
command against the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database, as in the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( { balancerStart: 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">balancerStart</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>
<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.startBalancer/#mongodb-method-sh.startBalancer">
<code class="leafygreen-ui-1l06pbn">
sh.startBalancer()
</code>
</a>
also enables auto-splitting for the sharded cluster.
</p>
</li>
</ol>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Disable Balancing During Backups
<a class="headerlink leafygreen-ui-1kepx47" href="#disable-balancing-during-backups" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-balancing-during-backups">
</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">
Disabling the balancer is only necessary when
<strong>
manually
</strong>
taking backups,
either by calling
<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>
or scheduling a task that calls
<code class="leafygreen-ui-1l06pbn">
mongodump
</code>
at a specific time.
</p>
<p class="leafygreen-ui-1kp3ins">
You do
<strong>
not
</strong>
have to disable the balancer when using coordinated backup
and restore processes:
</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">
If MongoDB migrates a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-chunk">
chunk
</a>
during a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/backups/">
backup
</a>
, you can end with an inconsistent snapshot
of your
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharded-cluster">
sharded cluster
</a>
. Never run a backup while the balancer is
active. To ensure that the balancer is inactive during your backup
operation:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Set the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-sharding-schedule-balancing-window" target="_self">
<span>
balancing window
</span>
</a>
so that the balancer is inactive during the backup. Ensure that the
backup can complete while you have the balancer disabled.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-sharding-balancing-disable-temporarily" target="_self">
<span>
manually disable the balancer
</span>
</a>
for the duration of the backup procedure.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
If you turn the balancer off while it is in the middle of a balancing round,
the shut down is not instantaneous. The balancer completes the chunk
move in-progress and then ceases all further balancing rounds.
</p>
<p class="leafygreen-ui-1kp3ins">
Before starting a backup operation, confirm that the balancer is not
active. You can use the following command to determine if the balancer
is active:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"!sh.getBalancerState() && !sh.isBalancerRunning()","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">getBalancerState</span>(<!-- -->) <!-- -->&<!-- -->& <!-- -->!<!-- -->sh.<span class="lg-highlight-title lg-highlight-function">isBalancerRunning</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">
When the backup procedure is complete you can reactivate
the balancer process.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Disable Balancing on a Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#disable-balancing-on-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="disable-balancing-on-a-collection">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can disable balancing for a specific collection with the
<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>
method. You may want to disable the
balancer for a specific collection to support maintenance operations or
atypical workloads, for example, during data ingestions or data exports.
</p>
<p class="leafygreen-ui-1kp3ins">
When you disable balancing on a collection, MongoDB will not interrupt in
progress migrations.
</p>
<p class="leafygreen-ui-1kp3ins">
To disable balancing on a collection, 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>
with 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>
shell and call the
<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>
method.
</p>
<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":"sh.disableBalancing(\"students.grades\")","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">disableBalancing</span>(<span class="lg-highlight-string">"students.grades"</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/sh.disableBalancing/#mongodb-method-sh.disableBalancing">
<code class="leafygreen-ui-1l06pbn">
sh.disableBalancing()
</code>
</a>
method accepts as its parameter the
full
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-namespace">
namespace
</a>
of the collection.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Enable Balancing on a Collection
<a class="headerlink leafygreen-ui-1kepx47" href="#enable-balancing-on-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="enable-balancing-on-a-collection">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can enable balancing for a specific collection with the
<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>
method.
</p>
<p class="leafygreen-ui-1kp3ins">
When you enable balancing for a collection, MongoDB will not
<em>
immediately
</em>
begin balancing data. However, if the data in your sharded collection is
not balanced, MongoDB will be able to begin distributing the data more
evenly.
</p>
<p class="leafygreen-ui-1kp3ins">
To enable balancing on a collection, 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>
with 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>
shell and call the
<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>
method.
</p>
<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":"sh.enableBalancing(\"students.grades\")","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">enableBalancing</span>(<span class="lg-highlight-string">"students.grades"</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/sh.enableBalancing/#mongodb-method-sh.enableBalancing">
<code class="leafygreen-ui-1l06pbn">
sh.enableBalancing()
</code>
</a>
method accepts as its parameter the
full
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-namespace">
namespace
</a>
of the collection.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Confirm Balancing is Enabled or Disabled
<a class="headerlink leafygreen-ui-1kepx47" href="#confirm-balancing-is-enabled-or-disabled" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="confirm-balancing-is-enabled-or-disabled">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
To confirm whether balancing for a collection is enabled or disabled,
query the
<code class="leafygreen-ui-1l06pbn">
collections
</code>
collection in the
<code class="leafygreen-ui-1l06pbn">
config
</code>
database for the
collection
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-namespace">
namespace
</a>
and check the
<code class="leafygreen-ui-1l06pbn">
noBalance
</code>
field. For
example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"config\").collections.findOne({_id : \"students.grades\"}).noBalance;","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">getSiblingDB</span>(<span class="lg-highlight-string">"config"</span>).<span class="lg-highlight-property">collections</span>.<span class="lg-highlight-title lg-highlight-function">findOne</span>(<!-- -->{<!-- -->_id <!-- -->: <span class="lg-highlight-string">"students.grades"</span>}).<span class="lg-highlight-property">noBalance</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 will return a null error,
<code class="leafygreen-ui-1l06pbn">
true
</code>
,
<code class="leafygreen-ui-1l06pbn">
false
</code>
, or no output:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A null error indicates the collection namespace is incorrect.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the result is
<code class="leafygreen-ui-1l06pbn">
true
</code>
, balancing is disabled.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the result is
<code class="leafygreen-ui-1l06pbn">
false
</code>
, balancing is enabled currently but has been
disabled in the past for the collection. Balancing of this collection
will begin the next time the balancer runs.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the operation returns no output, balancing is enabled currently and
has never been disabled in the past for this collection. Balancing of
this collection will begin the next time the balancer runs.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
You can also see if the balancer is enabled using
<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>
.
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/sh.status/#mongodb-data-sh.status.balancer.currently-enabled">
<code class="leafygreen-ui-1l06pbn">
currently-enabled
</code>
</a>
field indicates if the
balancer is enabled.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Change Replication Behavior for Chunk Migration
<a class="headerlink leafygreen-ui-1kepx47" href="#change-replication-behavior-for-chunk-migration" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-replication-behavior-for-chunk-migration">
</div>
</a>
</h2>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sharded-cluster-config-secondary-throttle">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Secondary Throttle
<a class="headerlink leafygreen-ui-1kepx47" href="#secondary-throttle" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="secondary-throttle">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
During chunk migration, the
<code class="leafygreen-ui-1l06pbn">
_secondaryThrottle
</code>
value determines
when the migration proceeds with next document in the chunk.
</p>
<p class="leafygreen-ui-1kp3ins">
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:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
_secondaryThrottle
</code>
setting for the balancer is set to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-write-concern">
write concern
</a>
, each document moved during chunk migration must receive
the requested acknowledgment before proceeding with the next
document.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the
<code class="leafygreen-ui-1l06pbn">
_secondaryThrottle
</code>
setting is unset, the migration process
does not wait for replication to a secondary and instead continues
with the next document.
</p>
<p class="leafygreen-ui-1kp3ins">
This is the default behavior for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger
<!-- -->
.
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
To change the
<code class="leafygreen-ui-1l06pbn">
_secondaryThrottle
</code>
setting, 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 directly update the
<code class="leafygreen-ui-1l06pbn">
_secondaryThrottle
</code>
value in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/config-database/#mongodb-data-config.settings">
<code class="leafygreen-ui-1l06pbn">
settings
</code>
</a>
collection
of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/config-database/#std-label-config-database">
config database
</a>
. For example, 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>
shell 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>
, run
the following command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use config\ndb.settings.updateOne(\n { \"_id\" : \"balancer\" },\n { $set : { \"_secondaryThrottle\" : { \"w\": \"majority\" } } },\n { upsert : 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">use config</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">settings</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"balancer"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->$set<!-- --> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"_secondaryThrottle"</span> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"w"</span>: <span class="lg-highlight-string">"majority"</span> } } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->upsert <!-- -->: <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">
The effects of changing the
<code class="leafygreen-ui-1l06pbn">
_secondaryThrottle
</code>
setting may not be
immediate. To ensure an immediate effect, stop and restart the balancer
to enable the selected value of
<code class="leafygreen-ui-1l06pbn">
_secondaryThrottle
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
For more information on the replication behavior during various steps
of chunk migration, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-balancer-administration/#std-label-chunk-migration-replication">
Range Migration and Replication
<!-- -->
.
</a>
</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/command/moveRange/#mongodb-dbcommand-dbcmd.moveRange">
<code class="leafygreen-ui-1l06pbn">
moveRange
</code>
</a>
command's
<code class="leafygreen-ui-1l06pbn">
secondaryThrottle
</code>
and
<code class="leafygreen-ui-1l06pbn">
writeConcern
</code>
options to specify the behavior during the command.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/moveChunk/#mongodb-dbcommand-dbcmd.moveChunk">
<code class="leafygreen-ui-1l06pbn">
moveChunk
</code>
</a>
command's
<code class="leafygreen-ui-1l06pbn">
_secondaryThrottle
</code>
and
<code class="leafygreen-ui-1l06pbn">
writeConcern
</code>
options to specify the behavior during the command.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
For details, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/moveRange/#mongodb-dbcommand-dbcmd.moveRange">
<code class="leafygreen-ui-1l06pbn">
moveRange
</code>
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/moveChunk/#mongodb-dbcommand-dbcmd.moveChunk">
<code class="leafygreen-ui-1l06pbn">
moveChunk
</code>
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-wait-for-delete-setting">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Wait for Delete
<a class="headerlink leafygreen-ui-1kepx47" href="#wait-for-delete" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="wait-for-delete">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
_waitForDelete
</code>
setting of the balancer and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/moveChunk/#mongodb-dbcommand-dbcmd.moveChunk">
<code class="leafygreen-ui-1l06pbn">
moveChunk
</code>
</a>
command affects how the balancer migrates
multiple chunks from a shard. Similarly, the
<code class="leafygreen-ui-1l06pbn">
_waitForDelete
</code>
setting
of the balancer and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/moveRange/#mongodb-dbcommand-dbcmd.moveRange">
<code class="leafygreen-ui-1l06pbn">
moveRange
</code>
</a>
command also affect how
the balancer migrates multiple chunks from a shard. By default, the
balancer does not wait for the on-going migration's delete phase to
complete before starting the next chunk migration. To have the delete
phase
<strong>
block
</strong>
the start of the next chunk migration, you can set the
<code class="leafygreen-ui-1l06pbn">
_waitForDelete
</code>
to true.
</p>
<p class="leafygreen-ui-1kp3ins">
For details on chunk migration, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-data-partitioning/#std-label-sharding-chunk-migration">
Range Migration
</a>
.
For details on the chunk migration queuing behavior, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-balancer-administration/#std-label-chunk-migration-queuing">
Asynchronous Range Migration Cleanup
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
_waitForDelete
</code>
is generally for internal testing purposes. To
change the balancer's
<code class="leafygreen-ui-1l06pbn">
_waitForDelete
</code>
value:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
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.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Update the
<code class="leafygreen-ui-1l06pbn">
_waitForDelete
</code>
value in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/config-database/#mongodb-data-config.settings">
<code class="leafygreen-ui-1l06pbn">
settings
</code>
</a>
collection of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/config-database/#std-label-config-database">
config database
</a>
. For
example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use config\ndb.settings.updateOne(\n { \"_id\" : \"balancer\" },\n { $set : { \"_waitForDelete\" : true } },\n { upsert : 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">use config</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">settings</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"balancer"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->$set<!-- --> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"_waitForDelete"</span> <!-- -->: <span class="lg-highlight-literal">true</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->upsert <!-- -->: <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>
</li>
</ol>
<p class="leafygreen-ui-1kp3ins">
Once set to
<code class="leafygreen-ui-1l06pbn">
true
</code>
, to revert to the default behavior:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
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.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Update or unset the
<code class="leafygreen-ui-1l06pbn">
_waitForDelete
</code>
field in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/config-database/#mongodb-data-config.settings">
<code class="leafygreen-ui-1l06pbn">
settings
</code>
</a>
collection of the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/config-database/#std-label-config-database">
config database
<!-- -->
:
</a>
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use config\ndb.settings.updateOne(\n { \"_id\" : \"balancer\", \"_waitForDelete\": true },\n { $unset : { \"_waitForDelete\" : \"\" } }\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 config</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">settings</span>.<span class="lg-highlight-title lg-highlight-function">updateOne</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-string">"_id"</span> <!-- -->: <span class="lg-highlight-string">"balancer"</span>, <span class="lg-highlight-string">"_waitForDelete"</span>: <span class="lg-highlight-literal">true</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <!-- -->$unset<!-- --> <!-- -->: <!-- -->{ <span class="lg-highlight-string">"_waitForDelete"</span> <!-- -->: <span class="lg-highlight-string">""</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>
</ol>
<span class="leafygreen-ui-1qnjd6d" id="std-label-balance-chunks-that-exceed-size-limit">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Balance Ranges that Exceed Size Limit
<a class="headerlink leafygreen-ui-1kepx47" href="#balance-ranges-that-exceed-size-limit" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="balance-ranges-that-exceed-size-limit">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
By default, MongoDB cannot move a range if the number of documents in
the range is greater than 2 times the result of dividing the
configured
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-data-partitioning/#std-label-sharding-range-size">
range size
</a>
by the average
document size.
</p>
<p class="leafygreen-ui-1kp3ins">
By specifying the balancer setting
<code class="leafygreen-ui-1l06pbn">
attemptToBalanceJumboChunks
</code>
to
<code class="leafygreen-ui-1l06pbn">
true
</code>
,
the balancer can migrate these large ranges as long as they have not been
labeled as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-data-partitioning/#std-label-jumbo-chunk">
jumbo
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
To set the balancer's
<code class="leafygreen-ui-1l06pbn">
attemptToBalanceJumboChunks
</code>
setting, 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 directly update 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. For example, 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>
shell 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, run the following command:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getSiblingDB(\"config\").settings.updateOne(\n { _id: \"balancer\" },\n { $set: { attemptToBalanceJumboChunks : true } },\n { upsert: 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-title lg-highlight-function">getSiblingDB</span>(<span class="lg-highlight-string">"config"</span>).<span class="lg-highlight-property">settings</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-string">"balancer"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$set</span>: <!-- -->{ <!-- -->attemptToBalanceJumboChunks <!-- -->: <span class="lg-highlight-literal">true</span> } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">upsert</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">
If the range you want to move is labeled
<code class="leafygreen-ui-1l06pbn">
jumbo
</code>
, you can
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/clear-jumbo-flag/#std-label-clear-jumbo-flag-manually">
manually clear the jumbo flag
</a>
to
have the balancer attempt to migrate the range.
</p>
<p class="leafygreen-ui-1kp3ins">
You can also manually migrate ranges that exceed the size limit
(with or without the
<code class="leafygreen-ui-1l06pbn">
jumbo
</code>
label) using either:
</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/command/moveRange/#mongodb-dbcommand-dbcmd.moveRange">
<code class="leafygreen-ui-1l06pbn">
moveRange
</code>
</a>
command with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/moveRange/#std-label-moverange-forceJumbo">
forceJumbo: true
</a>
option
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/moveChunk/#mongodb-dbcommand-dbcmd.moveChunk">
<code class="leafygreen-ui-1l06pbn">
moveChunk
</code>
</a>
command with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/moveRange/#std-label-moverange-forceJumbo">
forceJumbo: true
</a>
option
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
However, when you run
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/moveRange/#mongodb-dbcommand-dbcmd.moveRange">
<code class="leafygreen-ui-1l06pbn">
moveRange
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/moveChunk/#mongodb-dbcommand-dbcmd.moveChunk">
<code class="leafygreen-ui-1l06pbn">
moveChunk
</code>
</a>
with
<code class="leafygreen-ui-1l06pbn">
forceJumbo: true
</code>
, write operations to the collection may block
for a long period of time during the migration.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-sharded-cluster-config-max-shard-size">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Change the Maximum Storage Size for a Given Shard
<a class="headerlink leafygreen-ui-1kepx47" href="#change-the-maximum-storage-size-for-a-given-shard" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-the-maximum-storage-size-for-a-given-shard">
</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">
Changing a shard's maximum storage size is deprecated. MongoDB uses the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger">
WiredTiger Storage Engine
</a>
to manage how data is stored. WiredTiger compresses
data to minimize storage use. To learn more, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/wiredtiger/#std-label-storage-wiredtiger-compression">
Compression
<!-- -->
.
</a>
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
For MongoDB versions 6.1 and earlier, you can still set a
maximum storage size for a given shard in the sharded cluster by
using the
<code class="leafygreen-ui-1l06pbn">
maxSize
</code>
field. You can find configuration
data related to shards in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/config-database/#mongodb-data-config.shards">
<code class="leafygreen-ui-1l06pbn">
config.shards
</code>
</a>
collection.
</p>
<p class="leafygreen-ui-1kp3ins">
Starting in version 6.2, MongoDB removes the
<code class="leafygreen-ui-1l06pbn">
maxSize
</code>
field
from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/addShard/#mongodb-dbcommand-dbcmd.addShard">
<code class="leafygreen-ui-1l06pbn">
addShard
</code>
</a>
command. As a result:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Running
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/addShard/#mongodb-dbcommand-dbcmd.addShard">
<code class="leafygreen-ui-1l06pbn">
addShard
</code>
</a>
with the
<code class="leafygreen-ui-1l06pbn">
maxSize
</code>
field returns
an
<code class="leafygreen-ui-1l06pbn">
InvalidOptions
</code>
error.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
New documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/config-database/#mongodb-data-config.shards">
<code class="leafygreen-ui-1l06pbn">
shards
</code>
</a>
collection no longer
include the
<code class="leafygreen-ui-1l06pbn">
maxSize
</code>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Any pre-existing
<code class="leafygreen-ui-1l06pbn">
maxSize
</code>
field entries are ignored.
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/db.hostInfo/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
db.hostInfo()
<a class="headerlink leafygreen-ui-1kepx47" href="#db.hostinfo--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.hostinfo--">
</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>
</ul>
</div>
<dl class="method">
<dt>
<code class="leafygreen-ui-1l06pbn">
db.hostInfo()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-db.hostInfo" 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.hostInfo">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Returns a document with information about the underlying system
that 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>
runs on. Some of the
returned fields are only included on some platforms.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.hostInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.hostInfo()
</code>
</span>
</a>
provides 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>
helper
method around the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/hostInfo/#mongodb-dbcommand-dbcmd.hostInfo">
<code class="leafygreen-ui-1l06pbn">
hostInfo
</code>
</a>
command. The output of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.hostInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.hostInfo()
</code>
</span>
</a>
on a Linux system will resemble the
following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"system\" : {\n \"currentTime\" : ISODate(\"<timestamp>\"),\n \"hostname\" : \"<hostname>\",\n \"cpuAddrSize\" : <number>,\n \"memSizeMB\" : <number>,\n \"memLimitMB\" : <number>,\n \"numCores\" : <number>,\n \"cpuArch\" : \"<identifier>\",\n \"numaEnabled\" : <boolean>\n },\n \"os\" : {\n \"type\" : \"<string>\",\n \"name\" : \"<string>\",\n \"version\" : \"<string>\"\n },\n \"extra\" : {\n \"versionString\" : \"<string>\",\n \"libcVersion\" : \"<string>\",\n \"kernelVersion\" : \"<string>\",\n \"cpuFrequencyMHz\" : \"<string>\",\n \"cpuFeatures\" : \"<string>\",\n \"pageSize\" : <number>,\n \"numPages\" : <number>,\n \"maxOpenFiles\" : <number>\n },\n \"ok\" : <return>\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">"system"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"currentTime"</span> <!-- -->: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"<timestamp>"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"hostname"</span> <!-- -->: <span class="lg-highlight-string">"<hostname>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"cpuAddrSize"</span> <!-- -->: <<!-- -->number<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"memSizeMB"</span> <!-- -->: <<!-- -->number<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"memLimitMB"</span> <!-- -->: <<!-- -->number<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numCores"</span> <!-- -->: <<!-- -->number<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"cpuArch"</span> <!-- -->: <span class="lg-highlight-string">"<identifier>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numaEnabled"</span> <!-- -->: <<!-- -->boolean<!-- -->></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"os"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"type"</span> <!-- -->: <span class="lg-highlight-string">"<string>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"<string>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"version"</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"> <span class="lg-highlight-string">"extra"</span> <!-- -->: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"versionString"</span> <!-- -->: <span class="lg-highlight-string">"<string>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"libcVersion"</span> <!-- -->: <span class="lg-highlight-string">"<string>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"kernelVersion"</span> <!-- -->: <span class="lg-highlight-string">"<string>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"cpuFrequencyMHz"</span> <!-- -->: <span class="lg-highlight-string">"<string>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"cpuFeatures"</span> <!-- -->: <span class="lg-highlight-string">"<string>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"pageSize"</span> <!-- -->: <<!-- -->number<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"numPages"</span> <!-- -->: <<!-- -->number<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"maxOpenFiles"</span> <!-- -->: <<!-- -->number<!-- -->></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-keyword">return</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">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/hostInfo/#mongodb-data-hostInfo">
<code class="leafygreen-ui-1l06pbn">
hostInfo
</code>
</a>
for full documentation of the output of
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-method-db.hostInfo" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
db.hostInfo()
</code>
.
</span>
</a>
</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">
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>
|
https://www.mongodb.com/docs/manual/tutorial/operation-rejection-filters/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Block Slow Queries with Operation Rejection Filters
<a class="headerlink leafygreen-ui-1kepx47" href="#block-slow-queries-with-operation-rejection-filters" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="block-slow-queries-with-operation-rejection-filters">
</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-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>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
8.0
</em>
.
</p>
</div>
<p class="leafygreen-ui-1kp3ins">
To prevent an operation from causing excessive workload, you can
temporarily reject all operations associated with that
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-shapes/#std-label-query-shapes">
query shape
</a>
. To do that, use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setQuerySettings/#mongodb-dbcommand-dbcmd.setQuerySettings">
<code class="leafygreen-ui-1l06pbn">
setQuerySettings
</code>
</a>
to set the
<code class="leafygreen-ui-1l06pbn">
reject
</code>
field to
<code class="leafygreen-ui-1l06pbn">
true
</code>
for the operation's query shape.
A rejected query shape is also known as an
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-operation-rejection-filter">
operation rejection filter
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-query-optimizer">
query optimizer
</a>
uses the query settings as an additional
input during query planning, which affects the plan selected to run the
query.
</p>
<p class="leafygreen-ui-1kp3ins">
The steps on this page create an example collection and use an operation
rejection filter to block a query shape.
</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">
Assume a cluster has excessive workload because an application has an
inefficient query. To prevent the query from consuming excessive
cluster resources, use an operation rejection filter to block the query
and similar queries from running.
</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">
To identify an inefficient query, use various methods that include:
</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/atlasAtlas monitoring/" target="_self">
<span>
Atlas monitoring
</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/docs/atlas/alerts/" target="_self">
<span>
Atlas alerts
</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/docs/atlas/tutorial/profile-database/" target="_self">
<span>
Atlas Query Profiler
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/log-messages/#std-label-log-message-slow-ops">
Slow query logs
</a>
</p>
</li>
</ul>
</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":"Run:","name":"Create the example collection"},{"@type":"HowToStep","text":"Run the following setQuerySettings command to add an operation\nrejection filter with these fields:find with a filter and a sort, which defines the query\nshape.$db with the database for the query settings.settings that reject the query shape.The following truncated output shows the queryShapeHash field\nvalue and the settings reject field is true:","name":"Add an operation rejection filter"},{"@type":"HowToStep","text":"Run explain:The following truncated output shows the querySettings reject\nfield is true:","name":"(Optional) Use the explain command to confirm settings"},{"@type":"HowToStep","text":"Attempt to run this query:The following output verifies the query is rejected:","name":"(Optional) Verify the query is rejected"},{"@type":"HowToStep","text":"The following example uses removeQuerySettings to remove the\noperation rejection filter, which are identified using the\nqueryShapeHash value from the output shown in step 2 earlier:You can also get the queryShapeHash value from:Atlas Query ProfilerSlow query logsYou can also remove an operation rejection filter using a query\nshape. For example:","name":"(Optional) Remove the operation rejection filter"},{"@type":"HowToStep","text":"Because the operation rejection filter is removed, the following\nquery is unblocked and now runs:","name":"(Optional) Verify the query runs"}],"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 the example collection
<a class="headerlink leafygreen-ui-1kepx47" href="#create-the-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-example-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":" // Create pizzaOrders collection\n db.pizzaOrders.insertMany( [\n { _id: 0, type: \"pepperoni\", size: \"small\", price: 19,\n totalNumber: 10, orderDate: ISODate( \"2023-03-13T08:14:30Z\" ) },\n { _id: 1, type: \"pepperoni\", size: \"medium\", price: 20,\n totalNumber: 20, orderDate: ISODate( \"2023-03-13T09:13:24Z\" ) },\n { _id: 2, type: \"pepperoni\", size: \"large\", price: 21,\n totalNumber: 30, orderDate: ISODate( \"2023-03-17T09:22:12Z\" ) },\n { _id: 3, type: \"cheese\", size: \"small\", price: 12,\n totalNumber: 15, orderDate: ISODate( \"2023-03-13T11:21:39.736Z\" ) },\n { _id: 4, type: \"cheese\", size: \"medium\", price: 13,\n totalNumber: 50, orderDate: ISODate( \"2024-01-12T21:23:13.331Z\" ) },\n { _id: 5, type: \"cheese\", size: \"large\", price: 14,\n totalNumber: 10, orderDate: ISODate( \"2024-01-12T05:08:13Z\" ) },\n { _id: 6, type: \"vegan\", size: \"small\", price: 17,\n totalNumber: 10, orderDate: ISODate( \"2023-01-13T05:08:13Z\" ) },\n { _id: 7, type: \"vegan\", size: \"medium\", price: 18,\n totalNumber: 10, orderDate: ISODate( \"2023-01-13T05:10:13Z\" ) }\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">// Create pizzaOrders collection</span></td></tr><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">size</span>: <span class="lg-highlight-string">"small"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">19</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">totalNumber</span>: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-attr">orderDate</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2023-03-13T08:14:30Z"</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">"pepperoni"</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">20</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">totalNumber</span>: <span class="lg-highlight-number">20</span>, <span class="lg-highlight-attr">orderDate</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2023-03-13T09:13:24Z"</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">"pepperoni"</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">21</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">totalNumber</span>: <span class="lg-highlight-number">30</span>, <span class="lg-highlight-attr">orderDate</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2023-03-17T09:22:12Z"</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">type</span>: <span class="lg-highlight-string">"cheese"</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">12</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">totalNumber</span>: <span class="lg-highlight-number">15</span>, <span class="lg-highlight-attr">orderDate</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2023-03-13T11:21:39.736Z"</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">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">13</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">totalNumber</span>: <span class="lg-highlight-number">50</span>, <span class="lg-highlight-attr">orderDate</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2024-01-12T21:23:13.331Z"</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">type</span>: <span class="lg-highlight-string">"cheese"</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">14</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">totalNumber</span>: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-attr">orderDate</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2024-01-12T05:08:13Z"</span> ) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <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"</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">17</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">totalNumber</span>: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-attr">orderDate</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2023-01-13T05:08:13Z"</span> ) }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">7</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">"medium"</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">18</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">totalNumber</span>: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-attr">orderDate</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2023-01-13T05:10:13Z"</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">
Add an operation rejection filter
<a class="headerlink leafygreen-ui-1kepx47" href="#add-an-operation-rejection-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="add-an-operation-rejection-filter">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Run the following
<code class="leafygreen-ui-1l06pbn">
setQuerySettings
</code>
command to add an operation
rejection filter with these fields:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
find
</code>
with a filter and a sort, which defines the query
shape.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
$db
</code>
with the database for the query settings.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
settings
</code>
that reject the query shape.
</p>
</li>
</ul>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( {\n setQuerySettings: {\n find: \"pizzaOrders\",\n filter: {\n orderDate: { $gt: ISODate( \"2023-01-20T00:00:00Z\" ) }\n },\n sort: {\n totalNumber: 1\n },\n $db: \"test\"\n },\n settings: {\n reject: 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"> <span class="lg-highlight-attr">setQuerySettings</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">find</span>: <span class="lg-highlight-string">"pizzaOrders"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">filter</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">orderDate</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2023-01-20T00:00:00Z"</span> ) }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sort</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">totalNumber</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">$db</span>: <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-attr">settings</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">reject</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>
<p class="leafygreen-ui-1kp3ins">
The following truncated output shows the
<code class="leafygreen-ui-1l06pbn">
queryShapeHash
</code>
field
value and the
<code class="leafygreen-ui-1l06pbn">
settings reject
</code>
field is
<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":"{\n queryShapeHash: 'AB8ECADEE8F0EB0F447A30744EB4813AE7E0BFEF523B0870CA10FCBC87F5D8F1',\n settings: { reject: true },\n representativeQuery: {\n find: 'pizzaOrders',\n filter: { orderDate: { '$gt': ISODate('2023-01-20T00:00:00.000Z') } },\n sort: { totalNumber: 1 },\n '$db': 'test'\n },\n ok: 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="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">queryShapeHash</span>: <span class="lg-highlight-string">'AB8ECADEE8F0EB0F447A30744EB4813AE7E0BFEF523B0870CA10FCBC87F5D8F1'</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">settings</span>: <!-- -->{ <span class="lg-highlight-attr">reject</span>: <span class="lg-highlight-literal">true</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">representativeQuery</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">find</span>: <span class="lg-highlight-string">'pizzaOrders'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">filter</span>: <!-- -->{ <span class="lg-highlight-attr">orderDate</span>: <!-- -->{ <span class="lg-highlight-string">'$gt'</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2023-01-20T00:00:00.000Z'</span>) } }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sort</span>: <!-- -->{ <span class="lg-highlight-attr">totalNumber</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">'$db'</span>: <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-attr">ok</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>
</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">
(Optional) Use the explain command to confirm settings
<a class="headerlink leafygreen-ui-1kepx47" href="#-optional--use-the-explain-command-to-confirm-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="-optional--use-the-explain-command-to-confirm-settings">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.pizzaOrders.explain().find( { orderDate: { $gt: ISODate(\n\"2023-01-20T00:00:00Z\" ) } } ).sort( { totalNumber: 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">pizzaOrders</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">orderDate</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">"2023-01-20T00:00:00Z"</span> ) } } ).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">totalNumber</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 following truncated output shows the
<code class="leafygreen-ui-1l06pbn">
querySettings reject
</code>
field is
<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":"{\n queryPlanner: {\n winningPlan: {\n stage: 'SINGLE_SHARD',\n shards: [\n {\n explainVersion: '1',\n ...\n namespace: 'test.pizzaOrders',\n parsedQuery: { orderDate: { '$gt': ISODate('2023-01-20T00:00:00.000Z') } },\n querySettings: { reject: true },\n ...\n }\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">queryPlanner</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">winningPlan</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">stage</span>: <span class="lg-highlight-string">'SINGLE_SHARD'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">shards</span>: <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">explainVersion</span>: <span class="lg-highlight-string">'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">namespace</span>: <span class="lg-highlight-string">'test.pizzaOrders'</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">parsedQuery</span>: <!-- -->{ <span class="lg-highlight-attr">orderDate</span>: <!-- -->{ <span class="lg-highlight-string">'$gt'</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">'2023-01-20T00:00:00.000Z'</span>) } }<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">querySettings</span>: <!-- -->{ <span class="lg-highlight-attr">reject</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><tr 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>
</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">
(Optional) Verify the query is rejected
<a class="headerlink leafygreen-ui-1kepx47" href="#-optional--verify-the-query-is-rejected" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-optional--verify-the-query-is-rejected">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Attempt to run this query:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.pizzaOrders.find( { orderDate: {\n$gt: ISODate(\"2023-01-20T00:00:00Z\" ) } } ).sort( { totalNumber: 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">pizzaOrders</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">orderDate</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2023-01-20T00:00:00Z"</span> ) } } ).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">totalNumber</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 following output verifies the query is rejected:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"MongoServerError: Query rejected by admin query settings","programmingLanguage":"JavaScript"}
</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 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">MongoServerError</span>: <span class="lg-highlight-title lg-highlight-class">Query</span> rejected by admin query settings</td></tr></tbody></table></code></pre>
</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">
(Optional) Remove the operation rejection filter
<a class="headerlink leafygreen-ui-1kepx47" href="#-optional--remove-the-operation-rejection-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="-optional--remove-the-operation-rejection-filter">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following example uses
<code class="leafygreen-ui-1l06pbn">
removeQuerySettings
</code>
to remove the
operation rejection filter, which are identified using the
<code class="leafygreen-ui-1l06pbn">
queryShapeHash
</code>
value from the output shown in step 2 earlier:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( {\n removeQuerySettings: \"AB8ECADEE8F0EB0F447A30744EB4813AE7E0BFEF523B0870CA10FCBC87F5D8F1\"\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="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">removeQuerySettings</span>: <span class="lg-highlight-string">"AB8ECADEE8F0EB0F447A30744EB4813AE7E0BFEF523B0870CA10FCBC87F5D8F1"</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 also get the
<code class="leafygreen-ui-1l06pbn">
queryShapeHash
</code>
value from:
</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/tutorial/profile-database/" target="_self">
<span>
Atlas Query Profiler
</span>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/log-messages/#std-label-log-message-slow-ops">
Slow query logs
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
You can also remove an operation rejection filter using a query
shape. For example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.adminCommand( {\n removeQuerySettings: {\n find: \"pizzaOrders\",\n filter: {\n orderDate: { $gt: ISODate( \"2023-01-20T00:00:00Z\" ) }\n },\n sort: {\n totalNumber: 1\n },\n $db: \"test\"\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">removeQuerySettings</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">find</span>: <span class="lg-highlight-string">"pizzaOrders"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">filter</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">orderDate</span>: <!-- -->{ <span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2023-01-20T00:00:00Z"</span> ) }</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">sort</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">totalNumber</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">$db</span>: <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">} )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
6
</div>
</div>
<div class="css-1ed38mk">
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
(Optional) Verify the query runs
<a class="headerlink leafygreen-ui-1kepx47" href="#-optional--verify-the-query-runs" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-optional--verify-the-query-runs">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Because the operation rejection filter is removed, the following
query is unblocked and now runs:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.pizzaOrders.find( { orderDate: {\n$gt: ISODate(\"2023-01-20T00:00:00Z\" ) } } ).sort( { totalNumber: 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">pizzaOrders</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">orderDate</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">$gt</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2023-01-20T00:00:00Z"</span> ) } } ).<span class="lg-highlight-title lg-highlight-function">sort</span>( <!-- -->{ <span class="lg-highlight-attr">totalNumber</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>
</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">
After you block an inefficient operation using an operation rejection
filter, your cluster performance should return to how it was before the
inefficient query was introduced. Next steps:
</p>
<ol class="leafygreen-ui-rioki0" type="1">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Resolve the performance problem with the query. This may require an
index or a query rewrite.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Deploy the updated application.
</p>
</li>
</ol>
<p class="leafygreen-ui-1kp3ins">
To re-enable query settings that aren't deleted, use
<code class="leafygreen-ui-1l06pbn">
setQuerySettings
</code>
and set the
<code class="leafygreen-ui-1l06pbn">
reject
</code>
field to
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</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/command/setQuerySettings/#mongodb-dbcommand-dbcmd.setQuerySettings">
<code class="leafygreen-ui-1l06pbn">
setQuerySettings
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/removeQuerySettings/#mongodb-dbcommand-dbcmd.removeQuerySettings">
<code class="leafygreen-ui-1l06pbn">
removeQuerySettings
</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/querySettings/#mongodb-pipeline-pipe.-querySettings">
<code class="leafygreen-ui-1l06pbn">
$querySettings
</code>
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/query-shapes/#std-label-query-shapes">
Query Shapes
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/queryStats/#std-label-queryStats-queryShape">
Query statistics for query shapes
</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/core/query-plans/#std-label-query-plans-query-optimization">
Query Plans
</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/log-messages/#std-label-log-message-slow-ops">
Slow queries
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-the-database-profiler/#std-label-database-profiling-specify-slowms-threshold">
Database profiler
</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/atlas/performance-advisor/" target="_self">
<span>
Atlas monitoring
</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/docs/atlas/alerts/" target="_self">
<span>
Atlas alerts
</span>
</a>
</p>
</li>
</ul>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/aggregation/convert/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$convert (aggregation)
<a class="headerlink leafygreen-ui-1kepx47" href="#-convert--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="-convert--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>
</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">
$convert
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-expression-exp.-convert" 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.-convert">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Converts a value to a specified type.
</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">
$convert
</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>
<div>
<p>
<em>
Changed
<!-- -->
in version
<!-- -->
8.0
</em>
.
</p>
</div>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n $convert:\n {\n input: <expression>,\n to: <type> || {\n type: <type>,\n subtype: <int>\n },\n format: <string>,\n onError: <expression>,\n onNull: <expression>\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">$convert</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">input</span>: <<!-- -->expression<!-- -->><!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <!-- --><type expression> || {</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> type: <type expression>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> subtype: <int></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> },</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> format: <string>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> onError: <expression>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> onNull: <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 class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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-expression-exp.-convert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$convert
</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: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>
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">
input
</code>
</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>
The argument can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
</a>
. For more information on
expressions, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
Expression Operators
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
to
</code>
</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>
<p class="leafygreen-ui-1kp3ins">
Specifies the type to convert the
<code class="leafygreen-ui-1l06pbn">
input
</code>
expression to. You
can set
<code class="leafygreen-ui-1l06pbn">
to
</code>
to one of these values:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
A string or numeric identifier for the target type. See
<code class="leafygreen-ui-1l06pbn">
to.type
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
An object containing the fields
<code class="leafygreen-ui-1l06pbn">
to.type
</code>
and
<code class="leafygreen-ui-1l06pbn">
to.subtype
</code>
.
Use this format to convert to binData and specify a binData
subtype.
</p>
</li>
</ul>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
to.type
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Required if specifying
<code class="leafygreen-ui-1l06pbn">
to
</code>
as an object
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<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>
that resolves to one of the following
numeric or string identifiers:
</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>
String Identifier
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Numeric Identifier
</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>
"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>
For more information on the conversion to double, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-convert-to-double" target="_self">
<span>
Convert to Double
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<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>
For more information on the conversion to string, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-convert-to-string" target="_self">
<span>
Convert to String
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<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>
5
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
For more information on the conversion to binData, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-convert-to-binData" target="_self">
<span>
Convert to BinData
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<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>
For more information on the conversion to objectId, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-convert-to-objectId" target="_self">
<span>
Convert to ObjectId
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<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>
8
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
For more information on the conversion to boolean, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-convert-to-bool" target="_self">
<span>
Convert to Boolean
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<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>
For more information on the conversion to date, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-convert-to-date" target="_self">
<span>
Convert to Date
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<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>
16
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
For more information on the conversion to integer, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-convert-to-int" target="_self">
<span>
Convert to Integer
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<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>
18
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
For more information on the conversion to long, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-convert-to-long" target="_self">
<span>
Convert to Long
<!-- -->
.
</span>
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<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>
19
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
For more information on the conversion to decimal, see
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-convert-to-decimal" target="_self">
<span>
Convert to Decimal
<!-- -->
.
</span>
</a>
</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">
to.subtype
</code>
</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">
to.type
</code>
is
<code class="leafygreen-ui-1l06pbn">
binData
</code>
,
<code class="leafygreen-ui-1l06pbn">
to.subtype
</code>
specifies the
binData subtype to convert to. You can specify one of these
values for
<code class="leafygreen-ui-1l06pbn">
to.subtype
</code>
:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:10%"/>
<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>
Number
</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>
0
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Generic binary subtype
</div>
</div>
</td>
</tr>
<tr class="">
<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>
Function data
</div>
</div>
</td>
</tr>
<tr class="">
<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>
Binary (old)
</div>
</div>
</td>
</tr>
<tr class="">
<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>
UUID (old)
</div>
</div>
</td>
</tr>
<tr class="">
<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>
UUID
</div>
</div>
</td>
</tr>
<tr class="">
<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>
MD5
</div>
</div>
</td>
</tr>
<tr class="">
<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>
Encrypted BSON value
</div>
</div>
</td>
</tr>
<tr class="">
<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>
<p class="leafygreen-ui-1kp3ins">
Compressed time series data
</p>
<div>
<p>
<em>
New
<!-- -->
in version
<!-- -->
5.2
</em>
.
</p>
</div>
</div>
</div>
</td>
</tr>
<tr class="">
<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>
Sensitive data, such as a key or secret. MongoDB does not log
literal values for binary data with subtype 8. Instead, MongoDB
logs a placeholder value of
<code class="leafygreen-ui-1l06pbn">
###
</code>
.
</div>
</div>
</td>
</tr>
<tr class="">
<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>
Vector data, which is densely packed arrays of numbers of the
same type.
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
128
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Custom data
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
<em>
Default:
</em>
0 (Generic binary subtype)
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
format
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Required when converting to or from binData.
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
Specifies the binData format of the input or output. Can be one
of these values:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
base64
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
base64url
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
utf8
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
hex
</code>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<code class="leafygreen-ui-1l06pbn">
uuid
</code>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
If
<code class="leafygreen-ui-1l06pbn">
format
</code>
is
<code class="leafygreen-ui-1l06pbn">
uuid
</code>
,
<code class="leafygreen-ui-1l06pbn">
to.subtype
</code>
must be
<code class="leafygreen-ui-1l06pbn">
4
</code>
.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
onError
</code>
</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">
Value to return on encountering an error during conversion,
including unsupported type conversions. The arguments can be any
valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
If unspecified, the operation throws an error upon
encountering an error and stops.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
onNull
</code>
</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">
Value to return if the
<code class="leafygreen-ui-1l06pbn">
input
</code>
is null or missing. The
arguments can be any valid
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
expression
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
If unspecified,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-convert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$convert
</code>
</span>
</a>
returns null if the
<code class="leafygreen-ui-1l06pbn">
input
</code>
is null or missing.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
In addition to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-expression-exp.-convert" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$convert
</code>
</span>
</a>
, MongoDB provides the
following aggregation operators as shorthand when the default
"onError" and "onNull" behavior is acceptable:
</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/toBool/#mongodb-expression-exp.-toBool">
<code class="leafygreen-ui-1l06pbn">
$toBool
</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/toDate/#mongodb-expression-exp.-toDate">
<code class="leafygreen-ui-1l06pbn">
$toDate
</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/toDecimal/#mongodb-expression-exp.-toDecimal">
<code class="leafygreen-ui-1l06pbn">
$toDecimal
</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/toDouble/#mongodb-expression-exp.-toDouble">
<code class="leafygreen-ui-1l06pbn">
$toDouble
</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/toInt/#mongodb-expression-exp.-toInt">
<code class="leafygreen-ui-1l06pbn">
$toInt
</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/toLong/#mongodb-expression-exp.-toLong">
<code class="leafygreen-ui-1l06pbn">
$toLong
</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/toObjectId/#mongodb-expression-exp.-toObjectId">
<code class="leafygreen-ui-1l06pbn">
$toObjectId
</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/toString/#mongodb-expression-exp.-toString">
<code class="leafygreen-ui-1l06pbn">
$toString
</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/toUUID/#mongodb-expression-exp.-toUUID">
<code class="leafygreen-ui-1l06pbn">
$toUUID
</code>
</a>
</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>
<span class="leafygreen-ui-1qnjd6d" id="std-label-convert-to-binData">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Convert to BinData
<a class="headerlink leafygreen-ui-1kepx47" href="#convert-to-bindata" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-to-bindata">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
You can only convert strings to binData. All other input types result in
an error.
</p>
<p class="leafygreen-ui-1kp3ins">
See the following example conversions to binData:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:20%"/>
<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>
Result
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<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 input: \"hn3uUsMxSE6S0cVkebjmfg==\",\n to: {\n type: \"binData\",\n subtype: 0\n },\n format: \"base64\"\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">input</span>: <span class="lg-highlight-string">"hn3uUsMxSE6S0cVkebjmfg=="</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"binData"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">subtype</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">format</span>: <span class="lg-highlight-string">"base64"</span></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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"Binary.createFromBase64('hn3uUsMxSE6S0cVkebjmfg==', 0)","programmingLanguage":"JavaScript"}
</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 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">Binary</span>.<span class="lg-highlight-title lg-highlight-function">createFromBase64</span>(<span class="lg-highlight-string">'hn3uUsMxSE6S0cVkebjmfg=='</span>, <span class="lg-highlight-number">0</span>)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: \"hn3uUsMxSE6S0cVkebjmfg==\",\n to: \"binData\",\n format: \"base64\"\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">input</span>: <span class="lg-highlight-string">"hn3uUsMxSE6S0cVkebjmfg=="</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"binData"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"base64"</span></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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"Binary.createFromBase64('hn3uUsMxSE6S0cVkebjmfg==', 0)","programmingLanguage":"JavaScript"}
</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 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">Binary</span>.<span class="lg-highlight-title lg-highlight-function">createFromBase64</span>(<span class="lg-highlight-string">'hn3uUsMxSE6S0cVkebjmfg=='</span>, <span class="lg-highlight-number">0</span>)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: \"867dee52-c331-484e-92d1-c56479b8e67e\",\n to: {\n type: \"binData\",\n subtype: 0\n },\n format: \"base64\",\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">input</span>: <span class="lg-highlight-string">"867dee52-c331-484e-92d1-c56479b8e67e"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"binData"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">subtype</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">format</span>: <span class="lg-highlight-string">"base64"</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">}</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":"Failed to parse BinData '867dee52-c331-484e-92d1-c56479b8e67e'\nin $convert with no onError value: Input is not a valid base64\nstring.","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-title lg-highlight-class">Failed</span> to parse <span class="lg-highlight-title lg-highlight-class">BinData</span> <span class="lg-highlight-string">'867dee52-c331-484e-92d1-c56479b8e67e'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">in</span> <!-- -->$convert<!-- --> <span class="lg-highlight-keyword">with</span> no onError <span class="lg-highlight-attr">value</span>: <span class="lg-highlight-title lg-highlight-class">Input</span> is not a valid base64</td></tr><tr class=""><td class="leafygreen-ui-7razhx">string.</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: \"hn3uUsMxSE6S0cVkebjmfg==\",\n to: {\n type: \"binData\",\n subtype: 4\n },\n format: \"base64\"\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">input</span>: <span class="lg-highlight-string">"hn3uUsMxSE6S0cVkebjmfg=="</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"binData"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">subtype</span>: <span class="lg-highlight-number">4</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"base64"</span></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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"Failed to parse BinData 'hn3uUsMxSE6S0cVkebjmfg==' in $convert\nwith no onError value: Input is not a valid base64 string.","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-title lg-highlight-class">Failed</span> to parse <span class="lg-highlight-title lg-highlight-class">BinData</span> <span class="lg-highlight-string">'hn3uUsMxSE6S0cVkebjmfg=='</span> <span class="lg-highlight-keyword">in</span> <!-- -->$convert</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">with</span> no onError <span class="lg-highlight-attr">value</span>: <span class="lg-highlight-title lg-highlight-class">Input</span> is not a valid base64 string.</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: \"867dee52-c331-484e-92d1-c56479b8e67e\",\n to: {\n type: \"binData\",\n subtype: 4\n },\n format: \"uuid\"\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">input</span>: <span class="lg-highlight-string">"867dee52-c331-484e-92d1-c56479b8e67e"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"binData"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">subtype</span>: <span class="lg-highlight-number">4</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"uuid"</span></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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"UUID('867dee52-c331-484e-92d1-c56479b8e67e')","programmingLanguage":"JavaScript"}
</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 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">UUID</span>(<span class="lg-highlight-string">'867dee52-c331-484e-92d1-c56479b8e67e'</span>)</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: \"äöäöä\",\n to: {\n type: \"binData\",\n subtype: 4\n },\n format: \"uuid\"\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">input</span>: <span class="lg-highlight-string">"äöäöä"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"binData"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">subtype</span>: <span class="lg-highlight-number">4</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"uuid"</span></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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"Failed to parse BinData 'äöäöä' in $convert with no onError\nvalue: Input is not a valid UUID string.","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-title lg-highlight-class">Failed</span> to parse <span class="lg-highlight-title lg-highlight-class">BinData</span> <span class="lg-highlight-string">'äöäöä'</span> <span class="lg-highlight-keyword">in</span> <!-- -->$convert<!-- --> <span class="lg-highlight-keyword">with</span> no onError</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-attr">value</span>: <span class="lg-highlight-title lg-highlight-class">Input</span> is not a valid <span class="lg-highlight-variable lg-highlight-constant">UUID</span> string.</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: \"867dee52-c331-484e-92d1-c56479b8e67e\",\n to: { type: \"binData\" },\n format: \"uuid\"\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">input</span>: <span class="lg-highlight-string">"867dee52-c331-484e-92d1-c56479b8e67e"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"binData"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"uuid"</span></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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"Failed to parse BinData '867dee52-c331-484e-92d1-c56479b8e67e'\nin $convert with no onError value: Only the UUID subtype (4)\nis allowed with the 'uuid' format.","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-title lg-highlight-class">Failed</span> to parse <span class="lg-highlight-title lg-highlight-class">BinData</span> <span class="lg-highlight-string">'867dee52-c331-484e-92d1-c56479b8e67e'</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-keyword">in</span> <!-- -->$convert<!-- --> <span class="lg-highlight-keyword">with</span> no onError <span class="lg-highlight-attr">value</span>: <span class="lg-highlight-title lg-highlight-class">Only</span> the <span class="lg-highlight-variable lg-highlight-constant">UUID</span> subtype <!-- -->(<span class="lg-highlight-number">4</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">is allowed <span class="lg-highlight-keyword">with</span> the <span class="lg-highlight-string">'uuid'</span> format.</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: 123,\n to: { type: \"binData\", subtype: 0 }\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">input</span>: <span class="lg-highlight-number">123</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <!-- -->{ <span class="lg-highlight-attr">type</span>: <span class="lg-highlight-string">"binData"</span>, <span class="lg-highlight-attr">subtype</span>: <span class="lg-highlight-number">0</span> }</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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"Unsupported conversion from int to binData in $convert with no onError value","programmingLanguage":"JavaScript"}
</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 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">Unsupported</span> conversion <span class="lg-highlight-keyword">from</span> int to binData <span class="lg-highlight-keyword">in</span> <!-- -->$convert<!-- --> <span class="lg-highlight-keyword">with</span> no onError value</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-convert-to-bool">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Convert to Boolean
<a class="headerlink leafygreen-ui-1kepx47" href="#convert-to-boolean" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-to-boolean">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following table lists the input types that can be converted to a
boolean:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:55%"/>
<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>
Input Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Behavior
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<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>
Returns true
</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>
Returns true
</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>
No-op. Returns the boolean value.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Code
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Returns true
</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>
Returns true
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 class="line-block">
<div class="line">
Returns true if not zero
</div>
<div class="line">
Return false if zero
</div>
</div>
</div>
</div>
</td>
</tr>
<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>
<div class="line-block">
<div class="line">
Returns true if not zero
</div>
<div class="line">
Return false if zero
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
<div class="line-block">
<div class="line">
Returns true if not zero
</div>
<div class="line">
Return false if zero
</div>
</div>
</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>
Returns true
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 class="line-block">
<div class="line">
Returns true if not zero
</div>
<div class="line">
Return false if zero
</div>
</div>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
Returns true
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
Returns true
</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>
<p class="leafygreen-ui-1kp3ins">
Returns the value specified for the
<code class="leafygreen-ui-1l06pbn">
onNull
</code>
option. By default, returns null.
</p>
</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>
Returns true
</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>
Returns true
</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>
Returns true
</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>
Returns true
</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>
Returns true
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
To learn more about data types in MongoDB, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-bson-types">
BSON Types
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
The following table lists some conversion to boolean examples:
</p>
<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="leafygreen-ui-15dvrnp">
<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":"{ input: true, to: \"bool\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-literal">true</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"bool"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
true
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: false, to: \"bool\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-literal">false</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"bool"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
false
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: 1.99999, to: \"bool\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-number">1.99999</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"bool"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
true
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: Decimal128( \"5\" ), to: \"bool\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>( <span class="lg-highlight-string">"5"</span> )<!-- -->, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"bool"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
true
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: Decimal128( \"0\" ), to: \"bool\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>( <span class="lg-highlight-string">"0"</span> )<!-- -->, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"bool"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
false
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: 100, to: \"bool\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-number">100</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"bool"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
true
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: ISODate( \"2018-03-26T04:38:28.044Z\" ),\n to: \"bool\"\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">input</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2018-03-26T04:38:28.044Z"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"bool"</span></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>
true
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: \"hello\", to: \"bool\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"hello"</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"bool"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
true
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: \"false\", to: \"bool\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"false"</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"bool"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
true
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: \"\", to: \"bool\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">""</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"bool"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
true
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: null, to: \"bool\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"bool"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
null
</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/reference/operator/aggregation/toBool/#mongodb-expression-exp.-toBool">
<code class="leafygreen-ui-1l06pbn">
$toBool
</code>
</a>
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-convert-to-int">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Convert to Integer
<a class="headerlink leafygreen-ui-1kepx47" href="#convert-to-integer" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-to-integer">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following table lists the input types that can be converted to an
integer:
</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>
Input Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Behavior
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<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>
<div class="line-block">
<div class="line">
Returns
<code class="leafygreen-ui-1l06pbn">
0
</code>
for
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</div>
<div class="line">
Returns
<code class="leafygreen-ui-1l06pbn">
1
</code>
for
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</div>
</div>
</div>
</div>
</td>
</tr>
<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>
<p class="leafygreen-ui-1kp3ins">
Returns truncated value.
</p>
<p class="leafygreen-ui-1kp3ins">
The truncated double value must fall within the minimum and
maximum value for an integer.
</p>
<p class="leafygreen-ui-1kp3ins">
You cannot convert a double value whose truncated value is less
than the minimum integer value or is greater than the maximum
integer value.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
<p class="leafygreen-ui-1kp3ins">
Returns truncated value.
</p>
<p class="leafygreen-ui-1kp3ins">
The truncated decimal value must fall within the minimum and
maximum value for an integer.
</p>
<p class="leafygreen-ui-1kp3ins">
You cannot convert a decimal value whose truncated value is less
than the minimum integer value or is greater than the maximum
integer value.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
No-op. Returns the integer value.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
<p class="leafygreen-ui-1kp3ins">
Returns the long value as an integer.
</p>
<p class="leafygreen-ui-1kp3ins">
The long value must fall within the minimum and maximum value
for an integer.
</p>
<p class="leafygreen-ui-1kp3ins">
You cannot convert a long value that is less than the minimum
integer value or is greater than the maximum integer value.
</p>
</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>
<p class="leafygreen-ui-1kp3ins">
Returns the numerical value of the string as an integer.
</p>
<p class="leafygreen-ui-1kp3ins">
The string value must be a base
<sub>
10
</sub>
integer (e.g.
<code class="leafygreen-ui-1l06pbn">
"-5"
</code>
,
<code class="leafygreen-ui-1l06pbn">
"123456"
</code>
) and fall within the minimum and maximum
value for an integer.
</p>
<p class="leafygreen-ui-1kp3ins">
You cannot convert a string value of a float or decimal or
non-base
<sub>
10
</sub>
number (e.g.
<code class="leafygreen-ui-1l06pbn">
"-5.0"
</code>
,
<code class="leafygreen-ui-1l06pbn">
"0x6400"
</code>
) or a
value that falls outside the minimum and maximum value for an
integer.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The following table lists some conversion to integer examples:
</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="leafygreen-ui-15dvrnp">
<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":"{ input: true, to: \"int\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-literal">true</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"int"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
1
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: false, to: \"int\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-literal">false</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"int"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: 1.99999, to: \"int\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-number">1.99999</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"int"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
1
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: Decimal128( \"5.5000\" ), to: \"int\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>( <span class="lg-highlight-string">"5.5000"</span> )<!-- -->, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"int"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
5
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: Decimal128( \"9223372036000.000\" ),\n to: \"int\"\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">input</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>( <span class="lg-highlight-string">"9223372036000.000"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"int"</span></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>
Error
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: Long( \"5000\" ), to: \"int\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>( <span class="lg-highlight-string">"5000"</span> )<!-- -->, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"int"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
5000
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: Long( \"922337203600\" ), to: \"int\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>( <span class="lg-highlight-string">"922337203600"</span> )<!-- -->, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"int"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Error
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: \"-2\", to: \"int\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"-2"</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"int"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
-2
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: \"2.5\", to: \"int\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"2.5"</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"int"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Error
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: null, to: \"int\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"int"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
null
</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/reference/operator/aggregation/toInt/#mongodb-expression-exp.-toInt">
<code class="leafygreen-ui-1l06pbn">
$toInt
</code>
</a>
operator.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-convert-to-decimal">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Convert to Decimal
<a class="headerlink leafygreen-ui-1kepx47" href="#convert-to-decimal" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-to-decimal">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following table lists the input types that can be converted to a
decimal:
</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>
Input Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Behavior
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<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>
<div class="line-block">
<div class="line">
Returns
<code class="leafygreen-ui-1l06pbn">
Decimal128( "0" )
</code>
for
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</div>
<div class="line">
Returns
<code class="leafygreen-ui-1l06pbn">
Decimal128( "1" )
</code>
for
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</div>
</div>
</div>
</div>
</td>
</tr>
<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>
Returns double value as a decimal.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
No-op. Returns the decimal.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
Returns the int value as a decimal.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
Returns the long value as a decimal.
</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>
<p class="leafygreen-ui-1kp3ins">
Returns the numerical value of the string as a decimal.
</p>
<p class="leafygreen-ui-1kp3ins">
The string value must be of a base
<sub>
10
</sub>
numeric value (e.g.
<code class="leafygreen-ui-1l06pbn">
"-5.5"
</code>
,
<code class="leafygreen-ui-1l06pbn">
"123456"
</code>
).
</p>
<p class="leafygreen-ui-1kp3ins">
You cannot convert a string value of a non-base
<sub>
10
</sub>
number (e.g.
<code class="leafygreen-ui-1l06pbn">
"0x6400"
</code>
)
</p>
</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>
Returns the number of milliseconds since the epoch that
corresponds to the date value.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The following table lists some conversion to decimal examples:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:60%"/>
<col style="width:40%"/>
</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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ input: true, to: \"decimal\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-literal">true</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"decimal"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Decimal128("1")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: false, to: \"decimal\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-literal">false</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"decimal"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Decimal128("0")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: 2.5, to: \"decimal\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-number">2.5</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"decimal"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Decimal128( "2.50000000000000" )
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: Int32( 5 ), to: \"decimal\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-title lg-highlight-class">Int32</span>( <span class="lg-highlight-number">5</span> )<!-- -->, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"decimal"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Decimal128("5")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: Long( 10000 ), to: \"decimal\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>( <span class="lg-highlight-number">10000</span> )<!-- -->, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"decimal"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Decimal128("10000")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: \"-5.5\", to: \"decimal\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"-5.5"</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"decimal"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Decimal128("-5.5")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: ISODate( \"2018-03-26T04:38:28.044Z\" ),\n to: \"decimal\"\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">input</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2018-03-26T04:38:28.044Z"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"decimal"</span></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>
Decimal128("1522039108044")
</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/reference/operator/aggregation/toDecimal/#mongodb-expression-exp.-toDecimal">
<code class="leafygreen-ui-1l06pbn">
$toDecimal
</code>
</a>
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-convert-to-double">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Convert to Double
<a class="headerlink leafygreen-ui-1kepx47" href="#convert-to-double" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-to-double">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following table lists the input types that can be converted to a
double:
</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>
Input Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Behavior
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<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>
<div class="line-block">
<div class="line">
Returns NumberDouble(0) for
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</div>
<div class="line">
Returns NumberDouble(1) for
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</div>
</div>
</div>
</div>
</td>
</tr>
<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>
No-op. Returns the double.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
<p class="leafygreen-ui-1kp3ins">
Returns the decimal value as a double.
</p>
<p class="leafygreen-ui-1kp3ins">
The decimal value must fall within the minimum and
maximum value for a double.
</p>
<p class="leafygreen-ui-1kp3ins">
You cannot convert a decimal value whose value is less
than the minimum double value or is greater than the maximum
double value.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
Returns the int value as a double.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
Returns the long value as a double.
</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>
<p class="leafygreen-ui-1kp3ins">
Returns the numerical value of the string as a double.
</p>
<p class="leafygreen-ui-1kp3ins">
The string value must be of a base
<sub>
10
</sub>
numeric value (e.g.
<code class="leafygreen-ui-1l06pbn">
"-5.5"
</code>
,
<code class="leafygreen-ui-1l06pbn">
"123456"
</code>
) and fall within the minimum and
maximum value for a double.
</p>
<p class="leafygreen-ui-1kp3ins">
You cannot convert a string value of a non-base
<sub>
10
</sub>
number (e.g.
<code class="leafygreen-ui-1l06pbn">
"0x6400"
</code>
) or a value that falls
outside the minimum and maximum value for a double.
</p>
</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>
Returns the number of milliseconds since the epoch that
corresponds to the date value.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The following table lists some conversion to double examples:
</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="leafygreen-ui-15dvrnp">
<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":"{ input: true, to: \"double\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-literal">true</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"double"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
1
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: false, to: \"double\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-literal">false</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"double"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
0
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: 2.5, to: \"double\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-number">2.5</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"double"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
2.5
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: Int32( 5 ), to: \"double\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-title lg-highlight-class">Int32</span>( <span class="lg-highlight-number">5</span> )<!-- -->, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"double"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
5
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: Long( \"10000\" ), to: \"double\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>( <span class="lg-highlight-string">"10000"</span> )<!-- -->, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"double"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
10000
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: \"-5.5\", to: \"double\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"-5.5"</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"double"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
-5.5
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: \"5e10\", to: \"double\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"5e10"</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"double"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
50000000000
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: ISODate( \"2018-03-26T04:38:28.044Z\" ),\n to: \"double\"\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">input</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2018-03-26T04:38:28.044Z"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"double"</span></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>
1522039108044
</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/reference/operator/aggregation/toDouble/#mongodb-expression-exp.-toDouble">
<code class="leafygreen-ui-1l06pbn">
$toDouble
</code>
</a>
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-convert-to-long">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Convert to Long
<a class="headerlink leafygreen-ui-1kepx47" href="#convert-to-long" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-to-long">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following table lists the input types that can be converted to a
long:
</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>
Input Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Behavior
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<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>
<div class="line-block">
<div class="line">
Returns
<code class="leafygreen-ui-1l06pbn">
0
</code>
for
<code class="leafygreen-ui-1l06pbn">
false
</code>
.
</div>
<div class="line">
Returns
<code class="leafygreen-ui-1l06pbn">
1
</code>
for
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</div>
</div>
</div>
</div>
</td>
</tr>
<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>
<p class="leafygreen-ui-1kp3ins">
Returns truncated value.
</p>
<p class="leafygreen-ui-1kp3ins">
The truncated double value must fall within the minimum and
maximum value for a long.
</p>
<p class="leafygreen-ui-1kp3ins">
You cannot convert a double value whose truncated value is less
than the minimum long value or is greater than the maximum
long value.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
<p class="leafygreen-ui-1kp3ins">
Returns truncated value.
</p>
<p class="leafygreen-ui-1kp3ins">
The truncated decimal value must fall within the minimum and
maximum value for a long.
</p>
<p class="leafygreen-ui-1kp3ins">
You cannot convert a decimal value whose truncated value is less
than the minimum long value or is greater than the maximum
long value.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
Returns the int value as a long.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
No-op. Returns the long value.
</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>
<p class="leafygreen-ui-1kp3ins">
Returns the numerical value of the string.
</p>
<p class="leafygreen-ui-1kp3ins">
The string value must be of a base
<sub>
10
</sub>
long (e.g.
<code class="leafygreen-ui-1l06pbn">
"-5"
</code>
,
<code class="leafygreen-ui-1l06pbn">
"123456"
</code>
) and fall within the minimum and maximum
value for a long.
</p>
<p class="leafygreen-ui-1kp3ins">
You cannot convert a string value of a float or decimal or
non-base
<sub>
10
</sub>
number (e.g.
<code class="leafygreen-ui-1l06pbn">
"-5.0"
</code>
,
<code class="leafygreen-ui-1l06pbn">
"0x6400"
</code>
) or a
value that falls outside the minimum and maximum value
for a long.
</p>
</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>
Converts the Date into the number of milliseconds since the
epoch.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The following table lists some conversion to long examples:
</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="leafygreen-ui-15dvrnp">
<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":"{ input: true, to: \"long\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-literal">true</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"long"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Long("1")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: false, to: \"long\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-literal">false</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"long"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Long("0")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: 2.5, to: \"long\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-number">2.5</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"long"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Long("2")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: Decimal128( \"5.5000\" ), to: \"long\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>( <span class="lg-highlight-string">"5.5000"</span> )<!-- -->, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"long"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Long("5")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: Decimal128( \"9223372036854775808.0\" ),\n to: \"long\"\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">input</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>( <span class="lg-highlight-string">"9223372036854775808.0"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"long"</span></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>
Error
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: Int32( 8 ), to: \"long\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-title lg-highlight-class">Int32</span>( <span class="lg-highlight-number">8</span> )<!-- -->, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"long"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Long("8")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: ISODate( \"2018-03-26T04:38:28.044Z\" ),\n to: \"long\"\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">input</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2018-03-26T04:38:28.044Z"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"long"</span></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>
Long("1522039108044")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: \"-2\", to: \"long\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"-2"</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"long"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Long("-2")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: \"2.5\", to: \"long\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"2.5"</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"long"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Error
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: null, to: \"long\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-literal">null</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"long"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
null
</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/reference/operator/aggregation/toLong/#mongodb-expression-exp.-toLong">
<code class="leafygreen-ui-1l06pbn">
$toLong
</code>
</a>
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-convert-to-date">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Convert to Date
<a class="headerlink leafygreen-ui-1kepx47" href="#convert-to-date" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-to-date">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following table lists the input types that can be converted to a
date:
</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>
Input Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Behavior
</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>
<p class="leafygreen-ui-1kp3ins">
Returns a date that corresponds to the number of milliseconds
represented by the truncated double value.
</p>
<p class="leafygreen-ui-1kp3ins">
Positive number corresponds to the number of milliseconds since
Jan 1, 1970.
</p>
<p class="leafygreen-ui-1kp3ins">
Negative number corresponds to the number of milliseconds before
Jan 1, 1970.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
<p class="leafygreen-ui-1kp3ins">
Returns a date that corresponds to the number of milliseconds
represented by the truncated decimal value.
</p>
<p class="leafygreen-ui-1kp3ins">
Positive number corresponds to the number of milliseconds since
Jan 1, 1970.
</p>
<p class="leafygreen-ui-1kp3ins">
Negative number corresponds to the number of milliseconds before
Jan 1, 1970.
</p>
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
<p class="leafygreen-ui-1kp3ins">
Returns a date that corresponds to the number of milliseconds
represented by the long value.
</p>
<p class="leafygreen-ui-1kp3ins">
Positive number corresponds to the number of milliseconds since
Jan 1, 1970.
</p>
<p class="leafygreen-ui-1kp3ins">
Negative number corresponds to the number of milliseconds before
Jan 1, 1970.
</p>
</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>
<p class="leafygreen-ui-1kp3ins">
Returns a date that corresponds to the date string.
</p>
<p class="leafygreen-ui-1kp3ins">
The string must be a valid date string, such as:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
"2018-03-03"
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
"2018-03-03T12:00:00Z"
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
"2018-03-03T12:00:00+0500"
</p>
</li>
</ul>
</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>
Returns a date that corresponds to the timestamp of the
ObjectId.
</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>
Returns a date that corresponds to the timestamp.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The following table lists some conversion to date examples:
</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="leafygreen-ui-15dvrnp">
<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 input: 120000000000.5,\n to: \"date\"\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">input</span>: <span class="lg-highlight-number">120000000000.5</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"date"</span></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>
ISODate("1973-10-20T21:20:00.000Z")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: Decimal128( \"1253372036000.50\" ),\n to: \"date\"\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">input</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>( <span class="lg-highlight-string">"1253372036000.50"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"date"</span></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>
ISODate("2009-09-19T14:53:56.000Z")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: Long( \"1100000000000\" ),\n to: \"date\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">input</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>( <span class="lg-highlight-string">"1100000000000"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"date</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-string">}</span></td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
ISODate("2004-11-09T11:33:20.000Z")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: Long( \"-1100000000000\" ),\n to: \"date\"\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">input</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>( <span class="lg-highlight-string">"-1100000000000"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"date"</span></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>
ISODate("1935-02-22T12:26:40.000Z")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: ObjectId( \"5ab9c3da31c2ab715d421285\" ),\n to: \"date\"\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">input</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>( <span class="lg-highlight-string">"5ab9c3da31c2ab715d421285"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"date"</span></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>
ISODate("2018-03-27T04:08:58.000Z")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: \"2018-03-03\", to: \"date\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"2018-03-03"</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"date"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
ISODate("2018-03-03T00:00:00.000Z")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: \"2018-03-20 11:00:06 +0500\",\n to: \"date\"\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">input</span>: <span class="lg-highlight-string">"2018-03-20 11:00:06 +0500"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"date"</span></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>
ISODate("2018-03-20T06:00:06.000Z")
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: \"Friday\", to: \"date\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"Friday"</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"date"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Error
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: Timestamp( { t: 1637688118, i: 1 } ),\n to: \"date\"\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">input</span>: <span class="lg-highlight-title lg-highlight-class">Timestamp</span>( <!-- -->{ <span class="lg-highlight-attr">t</span>: <span class="lg-highlight-number">1637688118</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">to</span>: <span class="lg-highlight-string">"date"</span></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>
ISODate("2021-11-23T17:21:58.000Z")
</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/operator/aggregation/toDate/#mongodb-expression-exp.-toDate">
<code class="leafygreen-ui-1l06pbn">
$toDate
</code>
</a>
operator
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/dateFromString/#mongodb-expression-exp.-dateFromString">
<code class="leafygreen-ui-1l06pbn">
$dateFromString
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-convert-to-objectId">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Convert to ObjectId
<a class="headerlink leafygreen-ui-1kepx47" href="#convert-to-objectid" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-to-objectid">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following table lists the input types that can be converted to an
ObjectId:
</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>
Input Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Behavior
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="">
<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">
Returns an ObjectId for the hexadecimal string of length 24.
</p>
<p class="leafygreen-ui-1kp3ins">
You cannot convert a string value that is not a hexadecimal
string of length 24.
</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The following table lists some conversion to date examples:
</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":"{\n input: \"5ab9cbfa31c2ab715d42129e\",\n to: \"objectId\"\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">input</span>: <span class="lg-highlight-string">"5ab9cbfa31c2ab715d42129e"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"objectId"</span></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>
ObjectId("5ab9cbfa31c2ab715d42129e")
</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 input: \"5ab9cbfa31c2ab715d42129\",\n to: \"objectId\"\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">input</span>: <span class="lg-highlight-string">"5ab9cbfa31c2ab715d42129"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"objectId"</span></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>
Error
</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/reference/operator/aggregation/toObjectId/#mongodb-expression-exp.-toObjectId">
<code class="leafygreen-ui-1l06pbn">
$toObjectId
</code>
</a>
operator.
</p>
</div>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-convert-to-string">
</span>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Convert to String
<a class="headerlink leafygreen-ui-1kepx47" href="#convert-to-string" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-to-string">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The following table lists the input types that can be converted to a
string:
</p>
<div class="leafygreen-ui-skkc39" tabindex="0">
<table class="leafygreen-ui-hr8lsv">
<colgroup>
<col style="width:55%"/>
<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>
Input Type
</div>
</div>
</th>
<th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
<div class="leafygreen-ui-ui0te9">
<div>
Behavior
</div>
</div>
</th>
</tr>
</thead>
<tbody>
<tr class="leafygreen-ui-15dvrnp">
<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>
Returns the binary data value as a string.
</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>
Returns the boolean value as a string.
</div>
</div>
</td>
</tr>
<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>
Returns the double value as a string.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
Returns the decimal value as a string.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
Returns the integer value as a string.
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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>
Returns the long value as a string.
</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>
Returns the ObjectId value as a hexadecimal string..
</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>
No-op. Returns the string value.
</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>
Returns the date as a string.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The following table lists some conversion to string examples:
</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>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ input: true, to: \"string\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-literal">true</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"string"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"true"
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: false, to: \"string\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-literal">false</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"string"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"false"
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: 2.5, to: \"string\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-number">2.5</span>, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"string"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"2.5"
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: Int32( 2 ), to: \"string\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-title lg-highlight-class">Int32</span>( <span class="lg-highlight-number">2</span> )<!-- -->, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"string"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"2"
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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":"{ input: Long( 1000 ), to: \"string\" }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-title lg-highlight-class">Long</span>( <span class="lg-highlight-number">1000</span> )<!-- -->, <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"string"</span> }</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
"1000"
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: ObjectId( \"5ab9c3da31c2ab715d421285\" ),\n to: \"string\"\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">input</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>( <span class="lg-highlight-string">"5ab9c3da31c2ab715d421285"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"string"</span></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>
"5ab9c3da31c2ab715d421285"
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: ISODate( \"2018-03-27T16:58:51.538Z\" ),\n to: \"string\"\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">input</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>( <span class="lg-highlight-string">"2018-03-27T16:58:51.538Z"</span> )<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"string"</span></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>
"2018-03-27T16:58:51.538Z"
</div>
</div>
</td>
</tr>
<tr class="leafygreen-ui-15dvrnp">
<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 input: BinData(4, \"hn3f\"),\n to: \"string\",\n format: \"base64\"\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">input</span>: <span class="lg-highlight-title lg-highlight-class">BinData</span>(<span class="lg-highlight-number">4</span>, <span class="lg-highlight-string">"hn3f"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"string"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">format</span>: <span class="lg-highlight-string">"base64"</span></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>
'hn3f'
</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/operator/aggregation/toString/#mongodb-expression-exp.-toString">
<code class="leafygreen-ui-1l06pbn">
$toString
</code>
</a>
operator
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/dateToString/#mongodb-expression-exp.-dateToString">
<code class="leafygreen-ui-1l06pbn">
$dateToString
</code>
</a>
</p>
</li>
</ul>
</div>
</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>
<p class="leafygreen-ui-1kp3ins">
Create a collection
<code class="leafygreen-ui-1l06pbn">
orders
</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.orders.insertMany( [\n { _id: 1, item: \"apple\", qty: 5, price: 10 },\n { _id: 2, item: \"pie\", qty: 10, price: Decimal128(\"20.0\") },\n { _id: 3, item: \"ice cream\", qty: 2, price: \"4.99\" },\n { _id: 4, item: \"almonds\" },\n { _id: 5, item: \"bananas\", qty: 5000000000, price: Decimal128(\"1.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">orders</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">item</span>: <span class="lg-highlight-string">"apple"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">5</span>, <span class="lg-highlight-attr">price</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-number">2</span>, <span class="lg-highlight-attr">item</span>: <span class="lg-highlight-string">"pie"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">10</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</span>(<span class="lg-highlight-string">"20.0"</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">item</span>: <span class="lg-highlight-string">"ice cream"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-string">"4.99"</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">item</span>: <span class="lg-highlight-string">"almonds"</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">item</span>: <span class="lg-highlight-string">"bananas"</span>, <span class="lg-highlight-attr">qty</span>: <span class="lg-highlight-number">5000000000</span>, <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">] )</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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 on the
<code class="leafygreen-ui-1l06pbn">
orders
</code>
collection
converts the
<code class="leafygreen-ui-1l06pbn">
price
</code>
to a decimal:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"// Define stage to add convertedPrice and convertedQty fields with\n// the converted price and qty values.\n// If price or qty values are missing, the conversion returns a\n// value of decimal value or int value of 0.\n// If price or qty values cannot be converted, the conversion returns\n// a string\n\npriceQtyConversionStage = {\n $addFields: {\n convertedPrice: { $convert:\n {\n input: \"$price\",\n to: \"decimal\",\n onError: \"Error\",\n onNull: Decimal128(\"0\")\n } },\n convertedQty: { $convert:\n {\n input: \"$qty\",\n to: \"int\",\n onError:{ $concat:\n [\n \"Could not convert \",\n { $toString:\"$qty\" },\n \" to type integer.\"\n ]\n },\n onNull: Int32(\"0\")\n } },\n }\n};\n\ntotalPriceCalculationStage = {\n $project: { totalPrice: {\n $switch: {\n branches: [\n { case:\n { $eq: [ { $type: \"$convertedPrice\" }, \"string\" ] },\n then: \"NaN\"\n },\n { case:\n { $eq: [ { $type: \"$convertedQty\" }, \"string\" ] },\n then: \"NaN\"\n },\n ],\n default: { $multiply: [ \"$convertedPrice\", \"$convertedQty\" ] }\n }\n} } };\n\ndb.orders.aggregate( [\n priceQtyConversionStage,\n totalPriceCalculationStage\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">//</span><span class="lg-highlight-comment"> Define stage to </span><span class="lg-highlight-comment">add convertedPrice and convertedQty fields with</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">//</span><span class="lg-highlight-comment"> the converted price </span><span class="lg-highlight-comment">and qty values.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// If price or</span><span class="lg-highlight-comment"> qty values are </span><span class="lg-highlight-comment">missing,</span><span class="lg-highlight-comment"> the conversion returns </span><span class="lg-highlight-comment">a</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// value of decimal value or int value of 0.</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// If price or</span><span class="lg-highlight-comment"> qty values cannot </span><span class="lg-highlight-comment">be converted, the conversion returns</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx"><span class="lg-highlight-comment">// a 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">priceQtyConversionStage <!-- -->= <!-- -->{</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"> <span class="lg-highlight-attr">convertedPrice</span>: <!-- -->{ <span class="lg-highlight-attr">$convert</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"$price"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"decimal"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">onError</span>: <span class="lg-highlight-string">"Error"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">onNull</span>: <span class="lg-highlight-title lg-highlight-class">Decimal128</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"> <span class="lg-highlight-attr">convertedQty</span>: <!-- -->{ <span class="lg-highlight-attr">$convert</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">input</span>: <span class="lg-highlight-string">"$qty"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">to</span>: <span class="lg-highlight-string">"int"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">onError</span>:<!-- -->{ <span class="lg-highlight-attr">$concat</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"Could not convert "</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$toString</span>:<span class="lg-highlight-string">"$qty"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">" to type integer."</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">onNull</span>: <span class="lg-highlight-title lg-highlight-class">Int32</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">}<!-- -->;</td></tr><tr class=""><td class="leafygreen-ui-7razhx"><div class="leafygreen-ui-ihxujy"></div></td></tr><tr class=""><td class="leafygreen-ui-7razhx">totalPriceCalculationStage <!-- -->= <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">$project</span>: <!-- -->{ <span class="lg-highlight-attr">totalPrice</span>: <!-- -->{</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>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$eq</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$type</span>: <span class="lg-highlight-string">"$convertedPrice"</span> }<!-- -->, <span class="lg-highlight-string">"string"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"NaN"</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>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->{ <span class="lg-highlight-attr">$eq</span>: <!-- -->[ <!-- -->{ <span class="lg-highlight-attr">$type</span>: <span class="lg-highlight-string">"$convertedQty"</span> }<!-- -->, <span class="lg-highlight-string">"string"</span> ] }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">then</span>: <span class="lg-highlight-string">"NaN"</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-attr">$multiply</span>: <!-- -->[ <span class="lg-highlight-string">"$convertedPrice"</span>, <span class="lg-highlight-string">"$convertedQty"</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">db.<span class="lg-highlight-property">orders</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->priceQtyConversionStage<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->totalPriceCalculationStage</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 documents:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ _id: 1, totalPrice: Decimal128(\"50\") },\n{ _id: 2, totalPrice: Decimal128(\"200.0\") },\n{ _id: 3, totalPrice: Decimal128(\"9.98\") },\n{ _id: 4, totalPrice: Decimal128(\"0\") },\n{ _id: 5, totalPrice: 'NaN' }"}
</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">{ _id: 1, totalPrice: Decimal128("50") },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ _id: 2, totalPrice: Decimal128("200.0") },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ _id: 3, totalPrice: Decimal128("9.98") },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ _id: 4, totalPrice: Decimal128("0") },</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ _id: 5, totalPrice: 'NaN' }</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
These examples 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>
. The default types are
different in the legacy
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/mongo/#mongodb-binary-bin.mongo">
<code class="leafygreen-ui-1l06pbn">
mongo
</code>
</a>
shell.
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/read-concern-linearizable/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Read Concern
<code class="leafygreen-ui-1l06pbn">
"linearizable"
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#read-concern--linearizable-" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-concern--linearizable-">
</div>
</a>
</h1>
<dl class="readconcern">
<dt>
<code class="leafygreen-ui-1l06pbn">
"linearizable"
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-readconcern-readconcern.-linearizable-" 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-readconcern-readconcern.-linearizable-">
</div>
</a>
</dt>
<dd>
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
The query returns data that reflects all successful
majority-acknowledged writes that completed prior to the start of the
read operation. The query may wait for concurrently executing writes to
propagate to a majority of replica set members before returning results.
</p>
<p class="leafygreen-ui-1kp3ins">
If a majority of your replica set members crash and restart after the
read operation, documents returned by the read operation are durable if
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.writeConcernMajorityJournalDefault">
<code class="leafygreen-ui-1l06pbn">
writeConcernMajorityJournalDefault
</code>
</a>
is set to the default
state of
<code class="leafygreen-ui-1l06pbn">
true
</code>
.
</p>
<p class="leafygreen-ui-1kp3ins">
With
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-configuration/#mongodb-rsconf-rsconf.writeConcernMajorityJournalDefault">
<code class="leafygreen-ui-1l06pbn">
writeConcernMajorityJournalDefault
</code>
</a>
set to
<code class="leafygreen-ui-1l06pbn">
false
</code>
,
MongoDB does not wait for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
w: "majority"
</code>
</a>
writes to be written to the on-disk journal before acknowledging the
writes. As such,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
write operations could
possibly roll back in the event of a transient loss (e.g. crash and
restart) of a majority of nodes in a given replica set.
</p>
<p class="leafygreen-ui-1kp3ins">
You can specify linearizable read concern for read operations on
the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/replica-states/#mongodb-replstate-replstate.PRIMARY">
<code class="leafygreen-ui-1l06pbn">
primary
</code>
</a>
only.
</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">
Always use
<code class="leafygreen-ui-1l06pbn">
maxTimeMS
</code>
with linearizable read concern in case a
majority of data bearing members are unavailable.
<code class="leafygreen-ui-1l06pbn">
maxTimeMS
</code>
ensures that the operation does not block indefinitely and instead
ensures that the operation returns an error if the read concern
cannot be fulfilled.
</p>
</div>
</div>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Requirements
<a class="headerlink leafygreen-ui-1kepx47" href="#requirements" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="requirements">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Linearizable read concern guarantees only apply if read operations
specify a query filter that uniquely identifies a single document.
Additionally if none of the following criteria are met, linearizable
read concern might not read from a consistent snapshot, resulting in a
document matching the filter not being returned:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The query uses an immutable field as the search key of the query. For
example, searching on the
<code class="leafygreen-ui-1l06pbn">
_id
</code>
field or using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/meta/natural/#mongodb-operator-metaOp.-natural">
<code class="leafygreen-ui-1l06pbn">
$natural
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
No concurrent updates mutate the search key of the query.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The search key has a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/index-unique/#std-label-index-type-unique">
unique index
</a>
and the
query uses that index.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
If any of the preceding criteria are met, the query reads from a
consistent snapshot to return the single matching document.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Causally Consistent Sessions
<a class="headerlink leafygreen-ui-1kepx47" href="#causally-consistent-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="causally-consistent-sessions">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Read concern
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-readconcern-readconcern.-linearizable-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
"linearizable"
</code>
</span>
</a>
is unavailable for use with
causally consistent sessions.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Aggregation Restriction
<a class="headerlink leafygreen-ui-1kepx47" href="#aggregation-restriction" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-restriction">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You cannot use 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>
or the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/merge/#mongodb-pipeline-pipe.-merge">
<code class="leafygreen-ui-1l06pbn">
$merge
</code>
</a>
stage
in conjunction with read concern
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-readconcern-readconcern.-linearizable-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
"linearizable"
</code>
</span>
</a>
. That
is, if you specify
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-readconcern-readconcern.-linearizable-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
"linearizable"
</code>
</span>
</a>
read concern for
<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>
, you cannot include either
stages in the pipeline.
</p>
</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">
Combined 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,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-readconcern-readconcern.-linearizable-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
"linearizable"
</code>
</span>
</a>
read 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>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Read Your Own Writes
<a class="headerlink leafygreen-ui-1kepx47" href="#read-your-own-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="read-your-own-writes">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
You can use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-isolation-consistency-recency/#std-label-sessions">
causally consistent sessions
</a>
to read your own
writes, if the writes request acknowledgment.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Performance Comparisons
<a class="headerlink leafygreen-ui-1kepx47" href="#performance-comparisons" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-comparisons">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Unlike
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-majority/#mongodb-readconcern-readconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
"majority"
</code>
</a>
,
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-readconcern-readconcern.-linearizable-" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
"linearizable"
</code>
</span>
</a>
read
concern confirms with secondary members that the read operation is
reading from a primary that is capable of confirming writes with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
{ w: "majority" }
</code>
</a>
write concern.
<a class="footnote-reference header-buffer" href="#footnote-edge-cases-2-primaries" id="ref-edge-cases-2-primaries-id1">
[
<!-- -->
1
<!-- -->
]
</a>
As such, reads with linearizable read
concern may be significantly slower than 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>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/read-concern-local/#mongodb-readconcern-readconcern.-local-">
<code class="leafygreen-ui-1l06pbn">
"local"
</code>
</a>
read concerns.
</p>
<p class="leafygreen-ui-1kp3ins">
Always use
<code class="leafygreen-ui-1l06pbn">
maxTimeMS
</code>
with linearizable read concern in case a
majority of data bearing members are unavailable.
<code class="leafygreen-ui-1l06pbn">
maxTimeMS
</code>
ensures
that the operation does not block indefinitely and instead ensures that
the operation returns an error if the read concern cannot be fulfilled.
</p>
<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.restaurants.find( { _id: 5 } ).readConcern(\"linearizable\").maxTimeMS(10000)\n\ndb.runCommand( {\n find: \"restaurants\",\n filter: { _id: 5 },\n readConcern: { level: \"linearizable\" },\n maxTimeMS: 10000\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">restaurants</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-number">5</span> } ).<span class="lg-highlight-title lg-highlight-function">readConcern</span>(<span class="lg-highlight-string">"linearizable"</span>).<span class="lg-highlight-title lg-highlight-function">maxTimeMS</span>(<span class="lg-highlight-number">10000</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">runCommand</span>( <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">find</span>: <span class="lg-highlight-string">"restaurants"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">filter</span>: <!-- -->{ <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">readConcern</span>: <!-- -->{ <span class="lg-highlight-attr">level</span>: <span class="lg-highlight-string">"linearizable"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">maxTimeMS</span>: <span class="lg-highlight-number">10000</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>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-edge-cases-2-primaries" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<a href="#ref-edge-cases-2-primaries-id1">
1
</a>
]
</td>
<td class="leafygreen-ui-17r2xs0">
<!-- -->
In
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference-use-cases/#std-label-edge-cases">
some circumstances
</a>
, two nodes in a replica set
may
<em>
transiently
</em>
believe that they are the primary, but at most, one
of them will be able to complete writes with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
{ w:
"majority" }
</code>
</a>
write concern. The node that can complete
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#mongodb-writeconcern-writeconcern.-majority-">
<code class="leafygreen-ui-1l06pbn">
{ w: "majority" }
</code>
</a>
writes is the current
primary, and the other node is a former primary that has not yet
recognized its demotion, typically due to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-network-partition">
network partition
</a>
.
When this occurs, clients that connect to the former primary may
observe stale data despite having requested read preference
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/read-preference/#mongodb-readmode-primary">
<code class="leafygreen-ui-1l06pbn">
primary
</code>
</a>
, and new writes to the former primary will
eventually roll back.
</td>
</tr>
</tbody>
</table>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/command/addShard/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
addShard
<a class="headerlink leafygreen-ui-1kepx47" href="#addshard" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="addshard">
</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="#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>
</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">
addShard
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-dbcommand-dbcmd.addShard" 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.addShard">
</div>
</a>
</dt>
<dd>
<p class="leafygreen-ui-1kp3ins">
Adds a shard replica set to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharded-cluster">
sharded cluster
<!-- -->
.
</a>
</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/sh.addShard/#mongodb-method-sh.addShard">
<code class="leafygreen-ui-1l06pbn">
sh.addShard()
</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 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.adminCommand(\n {\n addShard: \"<replica_set>/<hostname><:port>\",\n name: \"<shard_name>\"\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">addShard</span>: <span class="lg-highlight-string">"<replica_set>/<hostname><:port>"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"<shard_name>"</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">
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>
<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 version 6.2, MongoDB removes the
<code class="leafygreen-ui-1l06pbn">
maxSize
</code>
field
from the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.addShard" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
addShard
</code>
</span>
</a>
command. As a result:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Running
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.addShard" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
addShard
</code>
</span>
</a>
with the
<code class="leafygreen-ui-1l06pbn">
maxSize
</code>
field returns
an
<code class="leafygreen-ui-1l06pbn">
InvalidOptions
</code>
error.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
New documents in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/config-database/#mongodb-data-config.shards">
<code class="leafygreen-ui-1l06pbn">
shards
</code>
</a>
collection no longer
include the
<code class="leafygreen-ui-1l06pbn">
maxSize
</code>
field.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Any pre-existing
<code class="leafygreen-ui-1l06pbn">
maxSize
</code>
field entries are ignored.
</p>
</li>
</ul>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The command contains 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">
addShard
</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">
The replica set name, hostname, and port of at least one member of
the shard's replica set. Any additional replica set member hostnames
must be comma separated. For example:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"<replica_set>/<hostname><:port>,<hostname><:port>, ...","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"><replica_set>/<hostname><:port>,<hostname><:port>, ...</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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>
<code class="leafygreen-ui-1l06pbn">
name
</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. A name for the shard. If this is not specified, MongoDB
automatically provides a unique name.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<p class="leafygreen-ui-1kp3ins">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.addShard" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
addShard
</code>
</span>
</a>
command stores shard configuration
information in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-config-database">
config database
</a>
. Always run
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.addShard" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
addShard
</code>
</span>
</a>
when using the
<code class="leafygreen-ui-1l06pbn">
admin
</code>
database.
</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">
Balancing
<a class="headerlink leafygreen-ui-1kepx47" href="#balancing" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="balancing">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
When you add a shard to a sharded cluster, you affect the balance of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-chunk">
chunks
</a>
among the shards of a cluster for all existing sharded
collections. The balancer will begin migrating chunks so that the
cluster will achieve balance. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/sharding-balancer-administration/#std-label-sharding-internals-balancing">
Balancer Internals
</a>
for
more information.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Hidden Members
<a class="headerlink leafygreen-ui-1kepx47" href="#hidden-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="hidden-members">
</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">
You cannot include a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-hidden-member/#std-label-replica-set-hidden-configuration">
hidden member
</a>
in the seed list provided to
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-dbcommand-dbcmd.addShard" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
addShard
</code>
</span>
</a>
.
</p>
</div>
</div>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
DDL Operations
<a class="headerlink leafygreen-ui-1kepx47" href="#ddl-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="ddl-operations">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
If you add a shard while your cluster executes a DDL operation
(operation that modifies a collection such as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/reshardCollection/#mongodb-dbcommand-dbcmd.reshardCollection">
<code class="leafygreen-ui-1l06pbn">
reshardCollection
</code>
</a>
),
<code class="leafygreen-ui-1l06pbn">
addShard
</code>
only executes after the
concurrent DDL operation finishes.
</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 command adds a replica set as a shard:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use admin\ndb.runCommand( { addShard: \"repl0/mongodb3.example.net:27327\"} )","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>( <!-- -->{ <span class="lg-highlight-attr">addShard</span>: <span class="lg-highlight-string">"repl0/mongodb3.example.net:27327"</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">
Do not use
<code class="leafygreen-ui-1l06pbn">
localhost
</code>
for the hostname unless your
config server is also running on
<code class="leafygreen-ui-1l06pbn">
localhost
</code>
.
</p>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/method/Mongo.getWriteConcern/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Mongo.getWriteConcern()
<a class="headerlink leafygreen-ui-1kepx47" href="#mongo.getwriteconcern--" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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.getwriteconcern--">
</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="#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.getWriteConcern()
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-method-Mongo.getWriteConcern" 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.getWriteConcern">
</div>
</a>
</dt>
<dd>
<table class="css-11h39yh e1nex8bq0">
<colgroup>
<col class="field-name"/>
<col class="field-body"/>
</colgroup>
<tbody>
<tr class="css-x8snli">
<th>
Returns
<!-- -->
:
</th>
<td>
The current
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-write-concern">
write concern
</a>
for the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.getMongo/#mongodb-method-db.getMongo">
<code class="leafygreen-ui-1l06pbn">
Mongo()
</code>
</a>
connection object.
</td>
</tr>
</tbody>
</table>
<p class="leafygreen-ui-1kp3ins">
See the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-write-concern">
Write Concern
</a>
for an introduction to write concerns
in MongoDB.
</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 takes the following form:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getMongo().getWriteConcern()","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">getWriteConcern</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 returns a document with the following values:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ w: <value>, wtimeout: <number>, j: <boolean> }","programmingLanguage":"JavaScript"}
</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 javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">w</span>: <<!-- -->value<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">wtimeout</span>: <<!-- -->number<!-- -->><!-- -->,<!-- --> <span class="lg-highlight-attr">j</span>: <<!-- -->boolean<!-- -->> <!-- -->}</td></tr></tbody></table></code></pre>
</div>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The fields are:
</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>
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">
w
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<p class="leafygreen-ui-1kp3ins">
The number of
<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/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
instances that must acknowledge a write. Possible values are:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
"majority". A majority of the target instances must
acknowledge the write.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<number>. The specified number of target instances must
acknowledge the write.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<custom write concern name>. A user defined write concern, the
tagged instances must acknowledge the write.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/write-concern/#std-label-wc-w">
write concern specification
</a>
for details.
</p>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
j
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
A boolean value.
<code class="leafygreen-ui-1l06pbn">
j: true
</code>
requests acknowledgment that the
write operation has been written to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/journaling/#std-label-journaling-internals">
on-disk journal
<!-- -->
.
</a>
</div>
</div>
</td>
</tr>
<tr class="">
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<code class="leafygreen-ui-1l06pbn">
wtimeout
</code>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
The number of milliseconds to wait for acknowledgment of the
write concern.
<code class="leafygreen-ui-1l06pbn">
wtimeout
</code>
is only applicable when
<code class="leafygreen-ui-1l06pbn">
w
</code>
has a
value greater than
<code class="leafygreen-ui-1l06pbn">
1
</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">
To return the current write concern, enter the following:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.getMongo().getWriteConcern()","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">getWriteConcern</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">
When a write concern is specified using
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/Mongo.setWriteConcern/#mongodb-method-Mongo.setWriteConcern">
<code class="leafygreen-ui-1l06pbn">
Mongo.setWriteConcern()
</code>
</a>
, the output of
<code class="leafygreen-ui-1l06pbn">
Mongo.getWriteConcern()
</code>
is similar to:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"WriteConcern { w: 2, wtimeout: 1000, j: 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"><span class="lg-highlight-title lg-highlight-class">WriteConcern</span> <!-- -->{ <span class="lg-highlight-attr">w</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">wtimeout</span>: <span class="lg-highlight-number">1000</span>, <span class="lg-highlight-attr">j</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">
Mongo.getWriteConcern()
</code>
command returns an empty line if no
write concern has been specified.
</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/Mongo.setWriteConcern/#mongodb-method-Mongo.setWriteConcern">
<code class="leafygreen-ui-1l06pbn">
setWriteConcern()
</code>
</a>
</p>
</li>
</ul>
</div>
</div>
</section>
</section>
|
https://www.mongodb.com/docs/manual/administration/monitoring/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
Monitoring a Self-Managed MongoDB Deployment
<a class="headerlink leafygreen-ui-1kepx47" href="#monitoring-a-self-managed-mongodb-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="monitoring-a-self-managed-mongodb-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="#monitoring-strategies" target="_self">
<span>
Monitoring Strategies
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#mongodb-reporting-tools" target="_self">
<span>
MongoDB Reporting Tools
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#process-logging" target="_self">
<span>
Process Logging
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#diagnosing-performance-issues" target="_self">
<span>
Diagnosing Performance Issues
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#replication-and-monitoring" target="_self">
<span>
Replication and Monitoring
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#sharding-and-monitoring" target="_self">
<span>
Sharding and Monitoring
</span>
</a>
</li>
<li class="leafygreen-ui-1jqtqlb">
<a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#storage-node-watchdog" target="_self">
<span>
Storage Node Watchdog
</span>
</a>
</li>
</ul>
</div>
<p class="leafygreen-ui-1kp3ins">
Monitoring is a critical component of all database administration. A
firm grasp of MongoDB's reporting will allow you to assess the state
of your database and maintain your deployment without crisis.
Additionally, a sense of MongoDB's normal operational parameters will
allow you to diagnose problems before they escalate to failures.
</p>
<p class="leafygreen-ui-1kp3ins">
This document presents an overview of the available monitoring utilities
and the reporting statistics
available in MongoDB. It also introduces diagnostic strategies
and suggestions for monitoring replica sets and
sharded clusters.
</p>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Monitoring Strategies
<a class="headerlink leafygreen-ui-1kepx47" href="#monitoring-strategies" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="monitoring-strategies">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
MongoDB provides various methods for collecting data about the state of
a running MongoDB instance:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
MongoDB distributes a set of utilities that provides real-time
reporting of database activities.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
MongoDB provides various
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/#std-label-database-commands">
database commands
</a>
that return statistics regarding the current
database state with greater fidelity.
</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/atlas/database?tck=docs_server" target="_self">
<span>
MongoDB Atlas
</span>
</a>
is a cloud-hosted database-as-a-service for running, monitoring, and
maintaining MongoDB deployments.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://cloud.mongodb.com/?tck=docs_server" target="_self">
<span>
MongoDB Cloud Manager
</span>
</a>
is a hosted service that monitors running MongoDB
deployments to collect data and provide visualization and alerts
based on that data.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
MongoDB Ops Manager is
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/products/mongodb-enterprise-advanced?tck=docs_server" target="_self">
<span>
an on-premise solution available in
MongoDB Enterprise Advanced
</span>
</a>
that monitors running MongoDB deployments to collect data and provide
visualization and alerts based on that data.
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
Each strategy can help answer different questions and is useful in
different contexts. These methods are complementary.
</p>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
MongoDB Reporting Tools
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-reporting-tools" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-reporting-tools">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
This section provides an overview of the reporting methods distributed
with MongoDB. It also offers examples of the kinds of questions that
each method is best suited to help you address.
</p>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Utilities
<a class="headerlink leafygreen-ui-1kepx47" href="#utilities" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="utilities">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The MongoDB distribution includes a number of utilities that quickly
return statistics about instances' performance and activity. Typically,
these are most useful for diagnosing issues and assessing normal
operation.
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
<code class="leafygreen-ui-1l06pbn">
mongostat
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongostat" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongostat">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
<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>
captures and returns the counts of database
operations by type (e.g. insert, query, update, delete, etc.). These
counts report on the load distribution on the server.
</p>
<p class="leafygreen-ui-1kp3ins">
Use
<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>
to understand the distribution of operation
types and to inform capacity planning. See 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>
reference page for details.
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
<code class="leafygreen-ui-1l06pbn">
mongotop
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongotop" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="mongotop">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongotop/#mongodb-binary-bin.mongotop" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongotop
</code>
</span>
</a>
tracks and reports the current read and write
activity of a MongoDB instance, and reports these statistics on a per
collection basis.
</p>
<p class="leafygreen-ui-1kp3ins">
Use
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongotop/#mongodb-binary-bin.mongotop" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongotop
</code>
</span>
</a>
to check if your database activity and use
match your expectations. See the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/database-tools/mongotop/#mongodb-binary-bin.mongotop" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
mongotop
</code>
</span>
</a>
reference page
for details.
</p>
</section>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Commands
<a class="headerlink leafygreen-ui-1kepx47" href="#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="commands">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
MongoDB includes a number of commands that report on the state of the
database.
</p>
<p class="leafygreen-ui-1kp3ins">
These data may provide a finer level of granularity than the utilities
discussed above. Consider using their output in scripts and programs to
develop custom alerts, or to modify the behavior of your application in
response to the activity of your instance. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.currentOp/#mongodb-method-db.currentOp">
<code class="leafygreen-ui-1l06pbn">
db.currentOp()
</code>
</a>
method is another useful tool for identifying the database instance's
in-progress operations.
</p>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
<code class="leafygreen-ui-1l06pbn">
serverStatus
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#serverstatus" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="serverstatus">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/serverStatus/#mongodb-dbcommand-dbcmd.serverStatus">
<code class="leafygreen-ui-1l06pbn">
serverStatus
</code>
</a>
command, or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.serverStatus/#mongodb-method-db.serverStatus">
<code class="leafygreen-ui-1l06pbn">
db.serverStatus()
</code>
</a>
from the shell, returns a general overview of the status of the
database, detailing disk usage, memory use, connection, journaling,
and index access. The command returns quickly and does not impact
MongoDB performance.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/serverStatus/#mongodb-dbcommand-dbcmd.serverStatus">
<code class="leafygreen-ui-1l06pbn">
serverStatus
</code>
</a>
outputs an account of the state of a MongoDB
instance. This command is rarely run directly. In most cases, the data
is more meaningful when aggregated, as one would see with monitoring
tools including
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://cloud.mongodb.com/?tck=docs_server" target="_self">
<span>
MongoDB Cloud Manager
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/products/mongodb-enterprise-advanced?tck=docs_server" target="_self">
<span>
Ops Manager
</span>
</a>
. Nevertheless, all
administrators should be familiar with the data provided by
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/serverStatus/#mongodb-dbcommand-dbcmd.serverStatus">
<code class="leafygreen-ui-1l06pbn">
serverStatus
</code>
.
</a>
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
<code class="leafygreen-ui-1l06pbn">
dbStats
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#dbstats" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="dbstats">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dbStats/#mongodb-dbcommand-dbcmd.dbStats">
<code class="leafygreen-ui-1l06pbn">
dbStats
</code>
</a>
command, or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.stats/#mongodb-method-db.stats">
<code class="leafygreen-ui-1l06pbn">
db.stats()
</code>
</a>
from the shell,
returns a document that addresses storage use and data volumes. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dbStats/#mongodb-dbcommand-dbcmd.dbStats">
<code class="leafygreen-ui-1l06pbn">
dbStats
</code>
</a>
reflect the amount of
storage used, the quantity of data contained in the database, and
object, collection, and index counters.
</p>
<p class="leafygreen-ui-1kp3ins">
Use this data to monitor the state and storage capacity
of a specific database. This output also allows you to compare
use between databases and to determine the average
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-document">
document
</a>
size in a database.
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
<code class="leafygreen-ui-1l06pbn">
collStats
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#collstats" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="collstats">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/collStats/#mongodb-dbcommand-dbcmd.collStats">
<code class="leafygreen-ui-1l06pbn">
collStats
</code>
</a>
or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.collection.stats/#mongodb-method-db.collection.stats">
<code class="leafygreen-ui-1l06pbn">
db.collection.stats()
</code>
</a>
from the
shell that provides statistics that resemble
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/dbStats/#mongodb-dbcommand-dbcmd.dbStats">
<code class="leafygreen-ui-1l06pbn">
dbStats
</code>
</a>
on
the collection level, including a count of the objects in the
collection, the size of the collection, the amount of disk space used
by the collection, and information about its indexes.
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
<code class="leafygreen-ui-1l06pbn">
replSetGetStatus
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#replsetgetstatus" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="replsetgetstatus">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetGetStatus/#mongodb-dbcommand-dbcmd.replSetGetStatus">
<code class="leafygreen-ui-1l06pbn">
replSetGetStatus
</code>
</a>
command (
<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>
from
the shell) returns an overview of your replica set's status. The
<code class="leafygreen-ui-1l06pbn">
replSetGetStatus
</code>
document details the state and configuration of the
replica set and statistics about its members.
</p>
<p class="leafygreen-ui-1kp3ins">
Use this data to ensure that replication is properly configured,
and to check the connections between the current host and the other members
of the replica set.
</p>
</section>
<section>
<h4 class="contains-headerlink leafygreen-ui-1vzlt5f">
Hosted (SaaS) Monitoring Tools
<a class="headerlink leafygreen-ui-1kepx47" href="#hosted--saas--monitoring-tools" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="hosted--saas--monitoring-tools">
</div>
</a>
</h4>
<p class="leafygreen-ui-1kp3ins">
These are monitoring tools provided as a hosted service, usually through
a paid subscription.
</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>
Name
</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>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://cloud.mongodb.com/?tck=docs_server" target="_self">
<span>
MongoDB Cloud Manager
</span>
</a>
</p>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
MongoDB Cloud Manager
<!-- -->
is a cloud-based suite of services for managing MongoDB
deployments.
<!-- -->
MongoDB Cloud Manager
<!-- -->
provides monitoring, backup, and automation
functionality. For an on-premise solution, see also
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/products/mongodb-enterprise-advanced?tck=docs_server" target="_self">
<span>
Ops Manager, available in MongoDB Enterprise Advanced
<!-- -->
.
</span>
</a>
</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.vividcortex.com/" rel="noopener noreferrer">
<span>
VividCortex
</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>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
VividCortex provides deep insights into MongoDB
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.vividcortex.com/product/how-it-works" rel="noopener noreferrer">
<span>
production
database workload and query performance
</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>
-- in
one-second resolution. Track latency, throughput, errors, and
more to ensure scalability and exceptional performance of your
application on MongoDB.
</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="http://www.serverdensity.com" rel="noopener noreferrer">
<span>
Server Density
</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>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="http://www.serverdensity.com/mongodb-monitoring/" rel="noopener noreferrer">
<span>
Dashboard for MongoDB
</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>
, MongoDB
specific alerts, replication failover timeline and iPhone, iPad
and Android mobile apps.
</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="http://ibmserviceengage.com" rel="noopener noreferrer">
<span>
Application Performance Management
</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>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
IBM has an Application Performance Management SaaS offering that
includes monitor for MongoDB and other applications and middleware.
</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="http://newrelic.com/" rel="noopener noreferrer">
<span>
New Relic
</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>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
New Relic offers full support for application performance
management. In addition, New Relic Plugins and Insights enable you to view
monitoring metrics from Cloud Manager in New Relic.
</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.datadoghq.com/" rel="noopener noreferrer">
<span>
Datadog
</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>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="http://docs.datadoghq.com/integrations/mongodb/" rel="noopener noreferrer">
<span>
Infrastructure monitoring
</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 visualize
the performance of your MongoDB deployments.
</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://sematext.com/spm" rel="noopener noreferrer">
<span>
SPM Performance Monitoring
</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>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://sematext.com/spm/integrations/mongodb-monitoring/" rel="noopener noreferrer">
<span>
Monitoring, Anomaly Detection and Alerting
</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>
SPM monitors all key MongoDB metrics together with infrastructure incl. Docker and other application metrics, e.g. Node.js, Java, NGINX, Apache, HAProxy or Elasticsearch. SPM provides correlation of metrics and logs.
</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="http://www.pandorafms.com/" rel="noopener noreferrer">
<span>
Pandora FMS
</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>
</div>
</div>
</td>
<td class="leafygreen-ui-1drjk7h">
<div class="leafygreen-ui-1n4j8xf">
<div>
Pandora FMS provides the
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="http://blog.pandorafms.org/how-to-monitor-mongodb-or-how-to-keep-your-users-happy/" rel="noopener noreferrer">
<span>
PandoraFMS-mongodb-monitoring
</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>
plugin to monitor MongoDB.
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<span class="leafygreen-ui-1qnjd6d" id="std-label-stdout">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-standard-output">
</span>
<span class="leafygreen-ui-1qnjd6d" id="std-label-monitoring-standard-loggging">
</span>
</section>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Process Logging
<a class="headerlink leafygreen-ui-1kepx47" href="#process-logging" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="process-logging">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
During normal operation,
<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 report a live account of all server activity and operations
to either
standard output or a log file. The following runtime settings
control these options.
</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-systemLog.quiet">
<code class="leafygreen-ui-1l06pbn">
quiet
</code>
</a>
. Limits the amount of information written to the
log or output.
</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.verbosity">
<code class="leafygreen-ui-1l06pbn">
verbosity
</code>
</a>
. Increases the amount of information written to
the log or output. You can also modify the logging verbosity during
runtime with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.logLevel">
<code class="leafygreen-ui-1l06pbn">
logLevel
</code>
</a>
parameter or the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/db.setLogLevel/#mongodb-method-db.setLogLevel">
<code class="leafygreen-ui-1l06pbn">
db.setLogLevel()
</code>
</a>
method in the shell.
</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">
path
</code>
</a>
. Enables logging to a file, rather than the standard
output. You must specify the full path to the log file when adjusting
this setting.
</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.logAppend">
<code class="leafygreen-ui-1l06pbn">
logAppend
</code>
</a>
. Adds information to a log
file instead of overwriting the file.
</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">
You can specify these configuration operations as the command line
arguments to
<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>
</p>
<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":"mongod -v --logpath /var/log/mongodb/server1.log --logappend","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">mongod <!-- -->-<!-- -->v <!-- -->-<!-- -->-<!-- -->logpath <!-- -->/<span class="lg-highlight-keyword">var</span>/<!-- -->log<!-- -->/<!-- -->mongodb<!-- -->/<!-- -->server1.<span class="lg-highlight-property">log</span> <!-- -->-<!-- -->-<!-- -->logappend</td></tr></tbody></table></code></pre>
<div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
<button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
<div class="leafygreen-ui-xhlipt">
<svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
<path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
Starts 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 in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-systemLog.verbosity">
<code class="leafygreen-ui-1l06pbn">
verbose
</code>
</a>
mode, appending data to the log file at
<code class="leafygreen-ui-1l06pbn">
/var/log/mongodb/server1.log/
</code>
.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The following
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-database-command">
database commands
</a>
also
affect logging:
</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/command/getLog/#mongodb-dbcommand-dbcmd.getLog">
<code class="leafygreen-ui-1l06pbn">
getLog
</code>
</a>
. Displays recent messages 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>
process log.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/logRotate/#mongodb-dbcommand-dbcmd.logRotate">
<code class="leafygreen-ui-1l06pbn">
logRotate
</code>
</a>
. Rotates the log files for
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
processes only. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/rotate-log-files/">
Rotate Log Files
<!-- -->
.
</a>
</p>
</li>
</ul>
<span class="leafygreen-ui-1qnjd6d" id="std-label-monitoring-log-redaction">
</span>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Log Redaction
<a class="headerlink leafygreen-ui-1kepx47" href="#log-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="log-redaction">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
<em>
Available in MongoDB Enterprise only
</em>
</p>
<p class="leafygreen-ui-1kp3ins">
A
<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>
running with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.redactClientLogData">
<code class="leafygreen-ui-1l06pbn">
redactClientLogData
</code>
</a>
redacts any message accompanying a given log
event before logging, leaving only metadata, source files, or line numbers
related to the event.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.redactClientLogData">
<code class="leafygreen-ui-1l06pbn">
redactClientLogData
</code>
</a>
prevents
potentially sensitive information from entering the system log at the cost of
diagnostic detail.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, the following operation inserts a document into a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
running without log redaction. The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
has the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/log-messages/#std-label-log-messages-configure-verbosity">
log verbosity level
</a>
set to
<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.clients.insertOne( { \"name\" : \"Joe\", \"PII\" : \"Sensitive Information\" } )","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">clients</span>.<span class="lg-highlight-title lg-highlight-function">insertOne</span>( <!-- -->{ <span class="lg-highlight-string">"name"</span> <!-- -->: <span class="lg-highlight-string">"Joe"</span>, <span class="lg-highlight-string">"PII"</span> <!-- -->: <span class="lg-highlight-string">"Sensitive Information"</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 produces the following log event:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": { \"$date\": \"2024-07-19T15:36:55.024-07:00\" },\n \"s\": \"I\",\n \"c\": \"COMMAND\",\n ...\n \"attr\": {\n \"type\": \"command\",\n ...\n \"appName\": \"mongosh 2.2.10\",\n \"command\": {\n \"insert\": \"clients\",\n \"documents\": [\n {\n \"name\": \"Joe\",\n \"PII\": \"Sensitive Information\",\n \"_id\": { \"$oid\": \"669aea8792c7fd822d3e1d8c\" }\n }\n ],\n \"ordered\": true,\n ...\n }\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">"t"</span>: <!-- -->{ <span class="lg-highlight-string">"$date"</span>: <span class="lg-highlight-string">"2024-07-19T15:36:55.024-07:00"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>: <span class="lg-highlight-string">"I"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"COMMAND"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"command"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"appName"</span>: <span class="lg-highlight-string">"mongosh 2.2.10"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"command"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"insert"</span>: <span class="lg-highlight-string">"clients"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"documents"</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-string">"Joe"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"PII"</span>: <span class="lg-highlight-string">"Sensitive Information"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</span>: <!-- -->{ <span class="lg-highlight-string">"$oid"</span>: <span class="lg-highlight-string">"669aea8792c7fd822d3e1d8c"</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">"ordered"</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><tr 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">
When
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
runs with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.redactClientLogData">
<code class="leafygreen-ui-1l06pbn">
redactClientLogData
</code>
</a>
and
performs the same insert operation, it produces the following log event:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n \"t\": { \"$date\": \"2024-07-19T15:36:55.024-07:00\" },\n \"s\": \"I\",\n \"c\": \"COMMAND\",\n ...\n \"attr\": {\n \"type\": \"command\",\n ...\n \"appName\": \"mongosh 2.2.10\",\n \"command\": {\n \"insert\": \"###\",\n \"documents\": [\n {\n \"name\": \"###\",\n \"PII\": \"###\",\n \"_id\": \"###\"\n }\n ],\n \"ordered\": \"###\",\n ...\n }\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">"t"</span>: <!-- -->{ <span class="lg-highlight-string">"$date"</span>: <span class="lg-highlight-string">"2024-07-19T15:36:55.024-07:00"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"s"</span>: <span class="lg-highlight-string">"I"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"c"</span>: <span class="lg-highlight-string">"COMMAND"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"attr"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"type"</span>: <span class="lg-highlight-string">"command"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <!-- -->...</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"appName"</span>: <span class="lg-highlight-string">"mongosh 2.2.10"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"command"</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"insert"</span>: <span class="lg-highlight-string">"###"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"documents"</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-string">"###"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"PII"</span>: <span class="lg-highlight-string">"###"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx"> <span class="lg-highlight-string">"_id"</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"> <span class="lg-highlight-string">"ordered"</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>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
Use
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.redactClientLogData">
<code class="leafygreen-ui-1l06pbn">
redactClientLogData
</code>
</a>
in conjunction with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-encryption-at-rest/#std-label-security-encryption-at-rest">
Encryption at Rest
</a>
and
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/security-transport-encryption/#std-label-transport-encryption">
TLS/SSL (Transport Encryption)
</a>
to assist
compliance with regulatory requirements.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Diagnosing Performance Issues
<a class="headerlink leafygreen-ui-1kepx47" href="#diagnosing-performance-issues" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="diagnosing-performance-issues">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
As you develop and operate applications with MongoDB, you may want to
analyze the performance of the database as the application.
<a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/analyzing-mongodb-performance/#std-label-performance">
MongoDB Performance
</a>
discusses some of the operational factors that can
influence performance.
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-replica-set-monitoring">
</span>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Replication and Monitoring
<a class="headerlink leafygreen-ui-1kepx47" href="#replication-and-monitoring" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-and-monitoring">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
Beyond the basic monitoring requirements for any MongoDB instance, for
replica sets, administrators must monitor
<em>
replication
lag
</em>
. "Replication lag" refers to the amount of time that it takes to
copy (i.e. replicate) a write operation on the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
to a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary">
secondary
</a>
. Some small delay period may be acceptable, but
significant problems emerge as replication lag grows, including:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Growing cache pressure on the primary.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Operations that occurred during the period of lag are not
replicated to one or more secondaries. If you're using replication
to ensure data persistence, exceptionally long delays may impact the
integrity of your data set.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
If the replication lag exceeds the length of the operation
log (
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-oplog">
oplog
</a>
) then MongoDB will have to perform an initial
sync on the secondary, copying all data from the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
and
rebuilding all indexes.
<a class="footnote-reference header-buffer" href="#footnote-oplog" id="ref-oplog-id1">
[
<!-- -->
1
<!-- -->
]
</a>
This is uncommon under normal circumstances,
but if you configure the oplog to be smaller than the default,
the issue can arise.
</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 size of the oplog is only configurable during the first
run using the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--oplogSize">
<code class="leafygreen-ui-1l06pbn">
--oplogSize
</code>
</a>
argument 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>
command, or preferably, the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-replication.oplogSizeMB">
<code class="leafygreen-ui-1l06pbn">
oplogSizeMB
</code>
</a>
setting
in the MongoDB configuration file. If you do not specify this on the
command line before running with the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--replSet">
<code class="leafygreen-ui-1l06pbn">
--replSet
</code>
</a>
option,
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
will create a default sized oplog.
</p>
<p class="leafygreen-ui-1kp3ins">
By default, the oplog is 5 percent of total available disk space
on 64-bit systems. For more information about changing the oplog
size, see the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/change-oplog-size/">
Change the Oplog Size of Self-Managed Replica Set Members
<!-- -->
.
</a>
</p>
</div>
</div>
</li>
</ul>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Flow Control
<a class="headerlink leafygreen-ui-1kepx47" href="#flow-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="flow-control">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Administrators can limit the rate at which
the primary applies its writes with the goal of keeping the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetGetStatus/#mongodb-data-replSetGetStatus.optimes.lastCommittedOpTime">
<code class="leafygreen-ui-1l06pbn">
majority
committed
</code>
</a>
lag under
a configurable maximum value
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.flowControlTargetLagSeconds">
<code class="leafygreen-ui-1l06pbn">
flowControlTargetLagSeconds
</code>
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
By default, flow control is
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.enableFlowControl">
<code class="leafygreen-ui-1l06pbn">
enabled
</code>
.
</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">
For flow control to engage, the replica set/sharded cluster must
have:
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/setFeatureCompatibilityVersion/#std-label-view-fcv">
featureCompatibilityVersion (fCV)
</a>
of
<code class="leafygreen-ui-1l06pbn">
4.2
</code>
and read concern
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-replication.enableMajorityReadConcern">
<code class="leafygreen-ui-1l06pbn">
majority enabled
</code>
</a>
. That is, enabled flow
control has no effect if fCV is not
<code class="leafygreen-ui-1l06pbn">
4.2
</code>
or if read concern
majority is disabled.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
See also:
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/troubleshoot-replica-sets/#std-label-replica-set-replication-lag">
Check the Replication Lag
<!-- -->
.
</a>
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Replica Set Status
<a class="headerlink leafygreen-ui-1kepx47" href="#replica-set-status" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-status">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Replication issues are most often the result of network connectivity
issues between members, or the result of a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
that does not
have the resources to support application and replication traffic. To
check the status of a replica, use the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetGetStatus/#mongodb-dbcommand-dbcmd.replSetGetStatus">
<code class="leafygreen-ui-1l06pbn">
replSetGetStatus
</code>
</a>
or
the following helper in the shell:
</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>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetGetStatus/#mongodb-dbcommand-dbcmd.replSetGetStatus">
<code class="leafygreen-ui-1l06pbn">
replSetGetStatus
</code>
</a>
reference provides a more in-depth
overview view of this output. In general, watch the value of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetGetStatus/#mongodb-data-replSetGetStatus.members-n-.optimeDate">
<code class="leafygreen-ui-1l06pbn">
optimeDate
</code>
</a>
, and pay particular attention
to the time difference between the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
and the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-secondary">
secondary
</a>
members.
</p>
<table class="header-buffer leafygreen-ui-rbqgrl" frame="void" id="footnote-oplog" rules="none">
<colgroup>
<col/>
</colgroup>
<tbody valign="top">
<tr>
<td class="leafygreen-ui-17r2xs0">
[
<a href="#ref-oplog-id1">
1
</a>
]
</td>
<td class="leafygreen-ui-17r2xs0">
<!-- -->
The oplog can grow past its configured size
limit to avoid deleting the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/replSetGetStatus/#mongodb-data-replSetGetStatus.optimes.lastCommittedOpTime">
<code class="leafygreen-ui-1l06pbn">
majority commit point
</code>
.
</a>
</td>
</tr>
</tbody>
</table>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Slow Application of Oplog Entries
<a class="headerlink leafygreen-ui-1kepx47" href="#slow-application-of-oplog-entries" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="slow-application-of-oplog-entries">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
Secondary members of a replica set now
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/replica-set-oplog/#std-label-slow-oplog-application">
log oplog entries
</a>
that take longer than the slow operation
threshold to apply. These slow oplog messages:
</p>
<ul class="leafygreen-ui-rioki0">
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Are logged for the secondaries in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--logpath">
<code class="leafygreen-ui-1l06pbn">
diagnostic log
</code>
.
</a>
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Are logged under the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/log-messages/#mongodb-data-REPL">
<code class="leafygreen-ui-1l06pbn">
REPL
</code>
</a>
component with the text
<code class="leafygreen-ui-1l06pbn">
applied op: <oplog entry> took <num>ms
</code>
.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Do not depend on the log levels (either at the system or component
level)
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Do not depend on the profiling level.
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
Are affected by
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-operationProfiling.slowOpSampleRate">
<code class="leafygreen-ui-1l06pbn">
slowOpSampleRate
</code>
.
</a>
</p>
</li>
</ul>
<p class="leafygreen-ui-1kp3ins">
The profiler does not capture slow oplog entries.
</p>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Sharding and Monitoring
<a class="headerlink leafygreen-ui-1kepx47" href="#sharding-and-monitoring" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-and-monitoring">
</div>
</a>
</h2>
<p class="leafygreen-ui-1kp3ins">
In most cases, the components of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharded-cluster">
sharded clusters
</a>
benefit from the same monitoring and analysis as all other MongoDB
instances. In addition, clusters require further monitoring to ensure
that data is effectively distributed among nodes and that sharding
operations are functioning appropriately.
</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">
See the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/sharding/">
Sharding
</a>
documentation for more
information.
</p>
</div>
</div>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Config Servers
<a class="headerlink leafygreen-ui-1kepx47" href="#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="config-servers">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-config-database">
config database
</a>
maintains a map identifying which
documents are on which shards. The cluster updates this map as
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-chunk">
chunks
</a>
move between shards. When a configuration
server becomes inaccessible, certain sharding operations become
unavailable, such as moving chunks and starting
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances. However, clusters remain accessible from already-running
<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">
Because inaccessible configuration servers can seriously impact
the availability of a sharded cluster, you should monitor your
configuration servers to ensure that the cluster remains well
balanced and that
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
instances can restart.
</p>
<p class="leafygreen-ui-1kp3ins">
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://cloud.mongodb.com/?tck=docs_server" target="_self">
<span>
MongoDB Cloud Manager
</span>
</a>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/products/mongodb-enterprise-advanced?tck=docs_server" target="_self">
<span>
Ops Manager
</span>
</a>
monitor config servers and can
create notifications if a config server becomes inaccessible. 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>
and
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/ops-manager/current/application/" target="_self">
<span>
Ops Manager documentation
</span>
</a>
for more information.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Balancing and Chunk Distribution
<a class="headerlink leafygreen-ui-1kepx47" href="#balancing-and-chunk-distribution" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="balancing-and-chunk-distribution">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
The most effective
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-sharded-cluster">
sharded cluster
</a>
deployments evenly balance
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-chunk">
chunks
</a>
among the shards. To facilitate this, MongoDB
has a background
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-balancer">
balancer
</a>
process that distributes data to ensure that
chunks are always optimally distributed among the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-shard">
shards
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
Issue the
<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>
or
<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>
command to the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#mongodb-binary-bin.mongos">
<code class="leafygreen-ui-1l06pbn">
mongos
</code>
</a>
from within
<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>
.
This returns an overview of the entire cluster including the
database name, and a list of the chunks.
</p>
</section>
<section>
<h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
Stale Locks
<a class="headerlink leafygreen-ui-1kepx47" href="#stale-locks" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="stale-locks">
</div>
</a>
</h3>
<p class="leafygreen-ui-1kp3ins">
To check the lock status of the database, 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 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>
. Issue the
following command sequence to switch to the
<code class="leafygreen-ui-1l06pbn">
config
</code>
database and
display all outstanding locks on the shard database:
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"use config\ndb.locks.find()","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 config</td></tr><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">locks</span>.<span class="lg-highlight-title lg-highlight-function">find</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 balancing process takes a special "balancer" lock that prevents
other balancing activity from transpiring. In the
<code class="leafygreen-ui-1l06pbn">
config
</code>
database,
use the following command to view the "balancer" lock.
</p>
<script class="structured_data" type="application/ld+json">
{"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.locks.find( { _id : \"balancer\" } )","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">locks</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{ <!-- -->_id <!-- -->: <span class="lg-highlight-string">"balancer"</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 primary of the CSRS config server holds the "balancer" lock, using a process
ID named "ConfigServer". This lock is never released. To determine if the
balancer is running, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/tutorial/manage-sharded-cluster-balancer/#std-label-sharding-balancing-is-running">
Check if Balancer is Running
<!-- -->
.
</a>
</p>
<span class="leafygreen-ui-1qnjd6d" id="std-label-storage-node-watchdog">
</span>
</section>
</section>
<section>
<h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
Storage Node Watchdog
<a class="headerlink leafygreen-ui-1kepx47" href="#storage-node-watchdog" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="storage-node-watchdog">
</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">
The
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-storage-node-watchdog" target="_self">
<span>
Storage Node Watchdog
</span>
</a>
is available in
both the Community and MongoDB Enterprise editions.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The Storage Node Watchdog monitors the following MongoDB directories to
detect filesystem unresponsiveness:
</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/mongod/#std-option-mongod.--dbpath">
<code class="leafygreen-ui-1l06pbn">
--dbpath
</code>
</a>
directory
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The
<code class="leafygreen-ui-1l06pbn">
journal
</code>
directory inside the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--dbpath">
<code class="leafygreen-ui-1l06pbn">
--dbpath
</code>
</a>
directory
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The directory of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--logpath">
<code class="leafygreen-ui-1l06pbn">
--logpath
</code>
</a>
file
</p>
</li>
<li class="leafygreen-ui-d7p7b6">
<p class="leafygreen-ui-1kp3ins">
The directory of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--auditPath">
<code class="leafygreen-ui-1l06pbn">
--auditPath
</code>
</a>
file
</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">
Starting in MongoDB 6.1, journaling is always enabled. As a result, MongoDB
removes the
<code class="leafygreen-ui-1l06pbn">
storage.journal.enabled
</code>
option and the corresponding
<code class="leafygreen-ui-1l06pbn">
--journal
</code>
and
<code class="leafygreen-ui-1l06pbn">
--nojournal
</code>
command-line options.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
By default, the Storage Node Watchdog is disabled. You can only enable
the Storage Node Watchdog 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>
at startup time by
setting the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.watchdogPeriodSeconds">
<code class="leafygreen-ui-1l06pbn">
watchdogPeriodSeconds
</code>
</a>
parameter to an integer
greater than or equal to 60. However, once enabled, you can pause the
Storage Node Watchdog and restart during runtime. See
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.watchdogPeriodSeconds">
<code class="leafygreen-ui-1l06pbn">
watchdogPeriodSeconds
</code>
</a>
parameter for details.
</p>
<p class="leafygreen-ui-1kp3ins">
If any of the filesystems containing the monitored directories become
unresponsive, the Storage Node Watchdog terminates the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
and exits with a status code of 61. 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 the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-primary">
primary
</a>
of a replica set, the
termination initiates a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-failover">
failover
</a>
, allowing another member to
become primary.
</p>
<p class="leafygreen-ui-1kp3ins">
Once a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#mongodb-binary-bin.mongod">
<code class="leafygreen-ui-1l06pbn">
mongod
</code>
</a>
has terminated, it may not be possible to cleanly
restart it on the
<em>
same
</em>
machine.
</p>
<div class="leafygreen-ui-15mdk4" role="note">
<h2 class="leafygreen-ui-6n7b7d">
Note
</h2>
<h3 class="leafygreen-ui-144d5kf">
<strong>
Symlinks
</strong>
</h3>
<div class="leafygreen-ui-9mycqc">
<p class="leafygreen-ui-1kp3ins">
If any of its monitored directories is a symlink to other volumes, the
Storage Node Watchdog does not monitor the symlink target.
</p>
<p class="leafygreen-ui-1kp3ins">
For example, 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>
uses
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-storage.directoryPerDB">
<code class="leafygreen-ui-1l06pbn">
storage.directoryPerDB: true
</code>
</a>
(or
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--directoryperdb">
<code class="leafygreen-ui-1l06pbn">
--directoryperdb
</code>
</a>
) and symlinks a
database directory to another volume, the Storage Node Watchdog does
not follow the symlink to monitor the target.
</p>
</div>
</div>
<p class="leafygreen-ui-1kp3ins">
The maximum time the Storage Node Watchdog can
take to detect an unresponsive filesystem and terminate is nearly
<em>
twice
</em>
the
value of
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/parameters/#mongodb-parameter-param.watchdogPeriodSeconds">
<code class="leafygreen-ui-1l06pbn">
watchdogPeriodSeconds
</code>
.
</a>
</p>
</section>
</section>
|
https://www.mongodb.com/docs/manual/reference/operator/meta/natural/ | <section>
<h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
$natural
<a class="headerlink leafygreen-ui-1kepx47" href="#-natural" title="Permalink to this heading">
<svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
<path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
</path>
<path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
</path>
</svg>
<div class="css-1btu0w3 evvkmar0" id="-natural">
</div>
</a>
</h1>
<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="operator">
<dt>
<code class="leafygreen-ui-1l06pbn">
$natural
</code>
<a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-operator-metaOp.-natural" 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-operator-metaOp.-natural">
</div>
</a>
</dt>
<dd>
</dd>
</dl>
<p class="leafygreen-ui-1kp3ins">
Use in conjunction with
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.hint/#mongodb-method-cursor.hint">
<code class="leafygreen-ui-1l06pbn">
cursor.hint()
</code>
</a>
to perform a
collection scan to return documents in
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-natural-order">
natural order
<!-- -->
.
</a>
</p>
<p class="leafygreen-ui-1kp3ins">
For usage, see
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.hint/#std-label-hint-collection-scans">
Force Collection Scans
</a>
example in the
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/method/cursor.hint/#mongodb-method-cursor.hint">
<code class="leafygreen-ui-1l06pbn">
cursor.hint()
</code>
</a>
reference page.
</p>
<p class="leafygreen-ui-1kp3ins">
You can specify a
<a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-operator-metaOp.-natural" target="_self">
<span>
<code class="leafygreen-ui-1l06pbn">
$natural
</code>
</span>
</a>
sort when running a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/find/#mongodb-dbcommand-dbcmd.find">
<code class="leafygreen-ui-1l06pbn">
find
</code>
</a>
operation against a
<a class="leafygreen-ui-tqgtui" href="/docs/manual/core/views/#std-label-views-landing-page">
view
<!-- -->
.
</a>
</p>
</section>
</section>
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.