<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  Server-side JavaScript
  <a class="headerlink leafygreen-ui-1kepx47" href="#server-side-javascript" title="Permalink to this heading">
   <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
    <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
    </path>
    <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-side-javascript">
   </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="#running-.js-files-via-a-mongosh-instance-on-the-server" target="_self">
     <span>
      Running
      <code class="css-1kekw28 e1wawog0">
       .js
      </code>
      files via a
      <code class="css-1kekw28 e1wawog0">
       mongosh
      </code>
      Instance on the Server
     </span>
    </a>
   </li>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#disable-server-side-execution-of-javascript" target="_self">
     <span>
      Disable Server-Side Execution of JavaScript
     </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>
 <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="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>
   <p class="leafygreen-ui-1kp3ins">
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/map-reduce/#std-label-map-reduce">
     Map-reduce
    </a>
    is deprecated starting in MongoDB 5.0.
   </p>
  </div>
 </div>
 <p class="leafygreen-ui-1kp3ins">
  MongoDB provides the following commands, methods, and operator that
perform server-side execution of JavaScript code:
 </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/mapReduce/#mongodb-dbcommand-dbcmd.mapReduce">
     <code class="leafygreen-ui-1l06pbn">
      mapReduce
     </code>
    </a>
    and the corresponding
    <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.mapReduce/#mongodb-method-db.collection.mapReduce">
     <code class="leafygreen-ui-1l06pbn">
      db.collection.mapReduce()
     </code>
    </a>
    .  For more
information, see
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/core/map-reduce/">
     Map-Reduce
     <!-- -->
     .
    </a>
   </p>
  </li>
  <li class="leafygreen-ui-d7p7b6">
   <p class="leafygreen-ui-1kp3ins">
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/query/where/#mongodb-query-op.-where">
     <code class="leafygreen-ui-1l06pbn">
      $where
     </code>
    </a>
    operator that evaluates a JavaScript expression or a
function in order to query for documents.
   </p>
  </li>
  <li class="leafygreen-ui-d7p7b6">
   <p class="leafygreen-ui-1kp3ins">
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/accumulator/#mongodb-group-grp.-accumulator">
     <code class="leafygreen-ui-1l06pbn">
      $accumulator
     </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>
    aggregation
operations that allows users to define custom aggregation expressions.
   </p>
  </li>
 </ul>
 <p class="leafygreen-ui-1kp3ins">
  You can also specify a JavaScript file to
  <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
   <span>
    <code class="leafygreen-ui-1l06pbn">
     mongosh
    </code>
   </span>
  </a>
  to
run on the server. For more information, see
  <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-running-js-scripts-in-mongo-on-mongod-host" target="_self">
   <span>
    Running
    <code class="leafygreen-ui-1l06pbn">
     .js
    </code>
    files via a
    <code class="leafygreen-ui-1l06pbn">
     mongosh
    </code>
    Instance on the Server
   </span>
  </a>
 </p>
 <div class="leafygreen-ui-15mdk4" role="note">
  <h2 class="leafygreen-ui-6n7b7d">
   Note
  </h2>
  <h3 class="leafygreen-ui-144d5kf">
   <strong>
    JavaScript in MongoDB
   </strong>
  </h3>
  <div class="leafygreen-ui-9mycqc">
   <p class="leafygreen-ui-1kp3ins">
    Although these methods use JavaScript, most
interactions with MongoDB do not use JavaScript but use an
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/drivers/" target="_self">
     <span>
      idiomatic driver
     </span>
    </a>
    in the language
of the interacting application.
   </p>
  </div>
 </div>
 <p class="leafygreen-ui-1kp3ins">
  If you do not need to perform server-side execution of JavaScript code,
see
  <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-disable-server-side-js" target="_self">
   <span>
    Disable Server-Side Execution of JavaScript
    <!-- -->
    .
   </span>
  </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 you are using SELinux, any MongoDB operation that requires
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-server-side-javascript" target="_self">
     <span>
      server-side JavaScript
     </span>
    </a>
    will
result in segfault errors.
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-disable-server-side-js" target="_self">
     <span>
      Disable Server-Side Execution of JavaScript
     </span>
    </a>
    describes
how to disable execution of server-side JavaScript.
   </p>
  </div>
 </div>
 <span class="leafygreen-ui-1qnjd6d" id="std-label-running-js-scripts-in-mongo-on-mongod-host">
 </span>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Running
   <code class="leafygreen-ui-1l06pbn">
    .js
   </code>
   files via a
   <code class="leafygreen-ui-1l06pbn">
    mongosh
   </code>
   Instance on the Server
   <a class="headerlink leafygreen-ui-1kepx47" href="#running-.js-files-via-a-mongosh-instance-on-the-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="running-.js-files-via-a-mongosh-instance-on-the-server">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   You can specify a JavaScript (
   <code class="leafygreen-ui-1l06pbn">
    .js
   </code>
   ) file to
   <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
    <span>
     <code class="leafygreen-ui-1l06pbn">
      mongosh
     </code>
    </span>
   </a>
   to execute the file on the server. This is a good technique
for performing batch administrative work. When you run
   <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://www.mongodb.com/docs/mongodb-shell/#mongodb-binary-bin.mongosh" target="_self">
    <span>
     <code class="leafygreen-ui-1l06pbn">
      mongosh
     </code>
    </span>
   </a>
   on the server, connecting via the localhost
interface, the connection is fast with low latency.
  </p>
  <span class="leafygreen-ui-1qnjd6d" id="std-label-disable-server-side-js">
  </span>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Disable Server-Side Execution of JavaScript
   <a class="headerlink leafygreen-ui-1kepx47" href="#disable-server-side-execution-of-javascript" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-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-server-side-execution-of-javascript">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   You can disable all server-side execution of JavaScript:
  </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 by passing the
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongod/#std-option-mongod.--noscripting">
      <code class="leafygreen-ui-1l06pbn">
       --noscripting
      </code>
     </a>
     option on the command
line or setting
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.javascriptEnabled">
      <code class="leafygreen-ui-1l06pbn">
       security.javascriptEnabled
      </code>
     </a>
     to false in the
configuration file.
    </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 by passing the
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/program/mongos/#std-option-mongos.--noscripting">
      <code class="leafygreen-ui-1l06pbn">
       --noscripting
      </code>
     </a>
     option on the command
line or setting
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/configuration-options/#mongodb-setting-security.javascriptEnabled">
      <code class="leafygreen-ui-1l06pbn">
       security.javascriptEnabled
      </code>
     </a>
     to false in the
configuration file.
    </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">
    Concurrency
    <a class="headerlink leafygreen-ui-1kepx47" href="#concurrency" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="concurrency">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    Refer to the individual method or operator documentation for any
concurrency information. See also the
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/faq/concurrency/#std-label-faq-concurrency-operations-locks">
     concurrency table
     <!-- -->
     .
    </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="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-server-side-javascript" target="_self">
     <span>
      server-side JavaScript
     </span>
    </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="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>
