<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  $where
  <a class="headerlink leafygreen-ui-1kepx47" href="#-where" title="Permalink to this heading">
   <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
    <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
    </path>
    <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
    </path>
   </svg>
   <div class="css-1btu0w3 evvkmar0" id="-where">
   </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-x9koo9" href="#available-javascript-properties-and-functions" target="_self">
     <span>
      Available JavaScript Properties and Functions
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#elemmatch" target="_self">
     <span>
      <code class="css-1kekw28 e1wawog0">
       elemMatch
      </code>
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#considerations" target="_self">
     <span>
      Considerations
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#javascript-enablement" target="_self">
     <span>
      JavaScript Enablement
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-x9koo9" href="#unsupported-array-and-string-functions" target="_self">
     <span>
      Unsupported Array and String Functions
     </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="query">
   <dt>
    <code class="leafygreen-ui-1l06pbn">
     $where
    </code>
    <a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-query-op.-where" 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.-where">
     </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="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/accumulator/#mongodb-group-grp.-accumulator">
        <code class="leafygreen-ui-1l06pbn">
         $accumulator
        </code>
       </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="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
        <span>
         <code class="leafygreen-ui-1l06pbn">
          $where
         </code>
        </span>
       </a>
       ) are
deprecated. MongoDB logs a warning when you run these functions.
      </p>
     </div>
    </div>
    <p class="leafygreen-ui-1kp3ins">
     Use the
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        $where
       </code>
      </span>
     </a>
     operator to pass either a string
containing a JavaScript expression or a full JavaScript function to
the query system. The
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        $where
       </code>
      </span>
     </a>
     provides greater
flexibility, but requires that the database processes the
JavaScript expression or function for
     <em>
      each
     </em>
     document in the
collection. Reference the document in the JavaScript expression or
function using either
     <code class="leafygreen-ui-1l06pbn">
      this
     </code>
     or
     <code class="leafygreen-ui-1l06pbn">
      obj
     </code>
     .
    </p>
   </dd>
  </dl>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Compatibility
   <a class="headerlink leafygreen-ui-1kepx47" href="#compatibility" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="compatibility">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   You can use
   <code class="leafygreen-ui-1l06pbn">
    $where
   </code>
   for deployments hosted in the following
environments:
  </p>
  <ul class="leafygreen-ui-rioki0">
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/atlas?tck=docs_server" target="_self">
      <span>
       MongoDB Atlas
      </span>
     </a>
     : The fully
managed service for MongoDB deployments in the cloud
    </p>
   </li>
  </ul>
  <ul class="leafygreen-ui-rioki0">
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-enterprise/#std-label-install-mdb-enterprise">
      MongoDB Enterprise
     </a>
     : The
subscription-based, self-managed version of MongoDB
    </p>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/administration/install-community/#std-label-install-mdb-community-edition">
      MongoDB Community
     </a>
     : The
source-available, free-to-use, and self-managed version of MongoDB
    </p>
   </li>
  </ul>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Syntax
   <a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="syntax">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   The
   <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
    <span>
     <code class="leafygreen-ui-1l06pbn">
      $where
     </code>
    </span>
   </a>
   operator has the following form:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $where: &lt;string|javascript&gt; }","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">$where</span>: <span class="lg-highlight-xml">&lt;string|JavaScript Code&gt; }</span></td></tr></tbody></table></code></pre>
     <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
      <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
       <div class="leafygreen-ui-xhlipt">
        <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
         <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.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">
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        $where
       </code>
      </span>
     </a>
     no longer supports the deprecated
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/bson-types/#std-label-bson-types">
      BSON type
     </a>
     JavaScript code with scope (BSON Type 15).
The
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        $where
       </code>
      </span>
     </a>
     operator only supports BSON type String (BSON Type 2) or
BSON type JavaScript (BSON Type 13). The use of BSON type JavaScript with
scope for
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        $where
       </code>
      </span>
     </a>
     has been deprecated since MongoDB 4.2.1.
    </p>
   </div>
  </div>
  <div class="leafygreen-ui-15mdk4" role="note">
   <h2 class="leafygreen-ui-6n7b7d">
    Note
   </h2>
   <h3 class="leafygreen-ui-144d5kf">
    <strong>
     Aggregation Alternatives Preferred
    </strong>
   </h3>
   <div class="leafygreen-ui-9mycqc">
    <p class="leafygreen-ui-1kp3ins">
     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
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/#std-label-aggregation-expressions">
      aggregation expressions
     </a>
     within
the query language. The
     <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/aggregation/accumulator/#mongodb-group-grp.-accumulator">
      <code class="leafygreen-ui-1l06pbn">
       $accumulator
      </code>
     </a>
     allows users to define custom aggregation expressions
in JavaScript 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>
    <p class="leafygreen-ui-1kp3ins">
     Given the available aggregation operators:
    </p>
    <ul class="leafygreen-ui-rioki0">
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       The use of
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
        <code class="leafygreen-ui-1l06pbn">
         $expr
        </code>
       </a>
       with aggregation operators that do not
use JavaScript (i.e. non-
       <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
non-
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/accumulator/#mongodb-group-grp.-accumulator">
        <code class="leafygreen-ui-1l06pbn">
         $accumulator
        </code>
       </a>
       operators) is faster than
       <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
        <span>
         <code class="leafygreen-ui-1l06pbn">
          $where
         </code>
        </span>
       </a>
       because it does not execute JavaScript and should
be preferred if possible.
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       However, if you must create custom expressions,
       <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/function/#mongodb-expression-exp.-function">
        <code class="leafygreen-ui-1l06pbn">
         $function
        </code>
       </a>
       is preferred over
       <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
        <span>
         <code class="leafygreen-ui-1l06pbn">
          $where
         </code>
         .
        </span>
       </a>
      </p>
     </li>
    </ul>
   </div>
  </div>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Behavior
   <a class="headerlink leafygreen-ui-1kepx47" href="#behavior" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="behavior">
    </div>
   </a>
  </h2>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Available JavaScript Properties and Functions
    <a class="headerlink leafygreen-ui-1kepx47" href="#available-javascript-properties-and-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="available-javascript-properties-and-functions">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mapReduce/#mongodb-dbcommand-dbcmd.mapReduce">
     <code class="leafygreen-ui-1l06pbn">
      map-reduce operations
     </code>
    </a>
    and
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $where
      </code>
     </span>
    </a>
    operator expressions
    <strong>
     cannot
    </strong>
    access certain global functions or
properties, such as
    <code class="leafygreen-ui-1l06pbn">
     db
    </code>
    , that are 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>
   </p>
   <p class="leafygreen-ui-1kp3ins">
    The following JavaScript functions and properties
    <strong>
     are available
    </strong>
    to
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/command/mapReduce/#mongodb-dbcommand-dbcmd.mapReduce">
     <code class="leafygreen-ui-1l06pbn">
      map-reduce operations
     </code>
    </a>
    and
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $where
      </code>
     </span>
    </a>
    operator expressions:
   </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>
          Available Properties
         </div>
        </div>
       </th>
       <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
        <div class="leafygreen-ui-ui0te9">
         <div>
          Available Functions
         </div>
        </div>
       </th>
       <th class="leafygreen-ui-13uteka" role="columnheader" scope="col">
        <div class="leafygreen-ui-ui0te9">
         <div>
         </div>
        </div>
       </th>
      </tr>
     </thead>
     <tbody>
      <tr class="">
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <div class="line-block">
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             args
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             MaxKey
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             MinKey
            </code>
           </div>
          </div>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <div class="line-block">
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             assert()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             BinData()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             DBPointer()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             DBRef()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             doassert()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             emit()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             gc()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             HexData()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             hex_md5()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             isNumber()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             isObject()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             ISODate()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             isString()
            </code>
           </div>
          </div>
         </div>
        </div>
       </td>
       <td class="leafygreen-ui-1drjk7h">
        <div class="leafygreen-ui-1n4j8xf">
         <div>
          <div class="line-block">
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             Map()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             MD5()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             NumberInt()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             NumberLong()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             ObjectId()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             print()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             printjson()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             printjsononeline()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             sleep()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             Timestamp()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             tojson()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             tojsononeline()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             tojsonObject()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             UUID()
            </code>
           </div>
           <div class="line">
            <code class="leafygreen-ui-1l06pbn">
             version()
            </code>
           </div>
          </div>
         </div>
        </div>
       </td>
      </tr>
     </tbody>
    </table>
   </div>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    <code class="leafygreen-ui-1l06pbn">
     elemMatch
    </code>
    <a class="headerlink leafygreen-ui-1kepx47" href="#elemmatch" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="elemmatch">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    Only apply the
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $where
      </code>
     </span>
    </a>
    query operator to top-level
documents. The
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $where
      </code>
     </span>
    </a>
    query operator will not work inside a
nested document, for instance, in an
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/elemMatch/#mongodb-query-op.-elemMatch">
     <code class="leafygreen-ui-1l06pbn">
      $elemMatch
     </code>
    </a>
    query.
   </p>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" 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>
   </h3>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      Do not use global variables.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         $where
        </code>
       </span>
      </a>
      evaluates JavaScript and cannot take
advantage of indexes. Therefore, query performance improves
when you express your query using the standard MongoDB
operators (e.g.,
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/gt/#mongodb-query-op.-gt">
       <code class="leafygreen-ui-1l06pbn">
        $gt
       </code>
      </a>
      ,
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/in/#mongodb-query-op.-in">
       <code class="leafygreen-ui-1l06pbn">
        $in
       </code>
      </a>
      ).
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      In general, you should use
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         $where
        </code>
       </span>
      </a>
      only when you
cannot express your query using another operator. If you must
use
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         $where
        </code>
       </span>
      </a>
      , try to include at least one other
standard query operator to filter the result set. Using
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         $where
        </code>
       </span>
      </a>
      alone requires a collection scan.
     </p>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    Using normal non-
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $where
      </code>
     </span>
    </a>
    query statements provides the
following performance advantages:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      MongoDB will evaluate non-
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         $where
        </code>
       </span>
      </a>
      components of query
before
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         $where
        </code>
       </span>
      </a>
      statements. If the
non-
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         $where
        </code>
       </span>
      </a>
      statements match no documents, MongoDB
will not perform any query evaluation using
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         $where
        </code>
        .
       </span>
      </a>
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The non-
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         $where
        </code>
       </span>
      </a>
      query statements may use an
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/glossary/#std-term-index">
       index
       <!-- -->
       .
      </a>
     </p>
    </li>
   </ul>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    JavaScript Enablement
    <a class="headerlink leafygreen-ui-1kepx47" href="#javascript-enablement" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-enablement">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    To use
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $where
      </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/aggregation/accumulator/#mongodb-group-grp.-accumulator">
     <code class="leafygreen-ui-1l06pbn">
      $accumulator
     </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>
    ), you must have
server-side scripting enabled (default).
   </p>
   <p class="leafygreen-ui-1kp3ins">
    However, if you do not use these operations, 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>
    </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="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/accumulator/#mongodb-group-grp.-accumulator">
     <code class="leafygreen-ui-1l06pbn">
      $accumulator
     </code>
    </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="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $where
      </code>
     </span>
    </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">
   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 the following documents in the
   <code class="leafygreen-ui-1l06pbn">
    players
   </code>
   collection:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.players.insertMany([\n   { _id: 12378, name: \"Steve\", username: \"steveisawesome\", first_login: \"2017-01-01\" },\n   { _id: 2, name: \"Anya\", username: \"anya\", first_login: \"2001-02-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">players</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">12378</span>, <span class="lg-highlight-attr">name</span>: <span class="lg-highlight-string">"Steve"</span>, <span class="lg-highlight-attr">username</span>: <span class="lg-highlight-string">"steveisawesome"</span>, <span class="lg-highlight-attr">first_login</span>: <span class="lg-highlight-string">"2017-01-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">name</span>: <span class="lg-highlight-string">"Anya"</span>, <span class="lg-highlight-attr">username</span>: <span class="lg-highlight-string">"anya"</span>, <span class="lg-highlight-attr">first_login</span>: <span class="lg-highlight-string">"2001-02-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">
   The following example uses
   <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
    <span>
     <code class="leafygreen-ui-1l06pbn">
      $where
     </code>
    </span>
   </a>
   and the
   <code class="leafygreen-ui-1l06pbn">
    hex_md5()
   </code>
   JavaScript function to compare the value of the
   <code class="leafygreen-ui-1l06pbn">
    name
   </code>
   field to an
MD5 hash and returns any matching document.
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.players.find( { $where: function() {\n   return (hex_md5(this.name) == \"9b53e667f30cd329dca1ec9e6a83e994\")\n} } );","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">db.players.find( { <span class="lg-highlight-variable">$where</span>: <span class="lg-highlight-title lg-highlight-function lg-highlight-function">function</span>() {</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-built_in">return</span> (hex_md5(this.name) == <span class="lg-highlight-string">"9b53e667f30cd329dca1ec9e6a83e994"</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 result:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{\n   \"_id\" : 2,\n   \"name\" : \"Anya\",\n   \"username\" : \"anya\",\n   \"first_login\" : \"2001-02-02\"\n}","programmingLanguage":"Bash"}
  </script>
  <div class="css-16lmvu9">
   <div class="leafygreen-ui-196mwvg">
    <div class="leafygreen-ui-yzcs8i">
     <pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light bash leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"_id"</span> : 2,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"name"</span> : <span class="lg-highlight-string">"Anya"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"username"</span> : <span class="lg-highlight-string">"anya"</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <span class="lg-highlight-string">"first_login"</span> : <span class="lg-highlight-string">"2001-02-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">
   As an alternative, the previous example can be rewritten using
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/expr/#mongodb-query-op.-expr">
    <code class="leafygreen-ui-1l06pbn">
     $expr
    </code>
   </a>
   and
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/function/#mongodb-expression-exp.-function">
    <code class="leafygreen-ui-1l06pbn">
     $function
    </code>
   </a>
   . You can define custom aggregation
expression in JavaScript with the aggregation operator
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/function/#mongodb-expression-exp.-function">
    <code class="leafygreen-ui-1l06pbn">
     $function
    </code>
   </a>
   .
To access
   <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 other aggregation operators in
   <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>
   , use with
   <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>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.players.find( {$expr: { $function: {\n      body: function(name) { return hex_md5(name) == \"9b53e667f30cd329dca1ec9e6a83e994\"; },\n      args: [ \"$name\" ],\n      lang: \"js\"\n} } } )","programmingLanguage":"JavaScript"}
  </script>
  <div class="css-16lmvu9">
   <div class="leafygreen-ui-196mwvg">
    <div class="leafygreen-ui-1b1bvay">
     <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">players</span>.<span class="lg-highlight-title lg-highlight-function">find</span>( <!-- -->{<span class="lg-highlight-attr">$expr</span>: <!-- -->{ <span class="lg-highlight-attr">$function</span>: <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">body</span>: <span class="lg-highlight-keyword lg-highlight-function">function</span>(<span class="lg-highlight-params">name</span>)<!-- --> <!-- -->{ <span class="lg-highlight-keyword">return</span> <span class="lg-highlight-title lg-highlight-function">hex_md5</span>(<!-- -->name) <!-- -->== <span class="lg-highlight-string">"9b53e667f30cd329dca1ec9e6a83e994"</span>; <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">args</span>: <!-- -->[ <span class="lg-highlight-string">"$name"</span> ]<!-- -->,</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></tbody></table></code></pre>
     <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
      <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
       <div class="leafygreen-ui-xhlipt">
        <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
         <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 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 must create custom expressions,
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/function/#mongodb-expression-exp.-function">
    <code class="leafygreen-ui-1l06pbn">
     $function
    </code>
   </a>
   is
preferred over
   <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-query-op.-where" target="_self">
    <span>
     <code class="leafygreen-ui-1l06pbn">
      $where
     </code>
     .
    </span>
   </a>
  </p>
 </section>
</section>
