<section>
 <h1 class="contains-headerlink leafygreen-ui-41fr9s" weight="medium">
  $linearFill (aggregation)
  <a class="headerlink leafygreen-ui-1kepx47" href="#-linearfill--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="-linearfill--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>
   <li class="leafygreen-ui-1jqtqlb">
    <a class="lg-ui-0001 leafygreen-ui-cpmeq5" href="#restrictions" target="_self">
     <span>
      Restrictions
     </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">
     $linearFill
    </code>
    <a class="headerlink leafygreen-ui-1kepx47" href="#mongodb-group-grp.-linearFill" 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.-linearFill">
     </div>
    </a>
   </dt>
   <dd>
    <div>
     <p>
      <em>
       New
       <!-- -->
       in version
       <!-- -->
       5.3
      </em>
      .
     </p>
    </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">
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-linearFill" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        $linearFill
       </code>
      </span>
     </a>
     is only available in the
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
      <code class="leafygreen-ui-1l06pbn">
       $setWindowFields
      </code>
     </a>
     stage.
    </p>
   </dd>
  </dl>
 </section>
 <section>
  <h2 class="contains-headerlink leafygreen-ui-19f71ei" weight="medium">
   Syntax
   <a class="headerlink leafygreen-ui-1kepx47" href="#syntax" title="Permalink to this heading">
    <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
     <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
     </path>
     <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
     </path>
    </svg>
    <div class="css-1btu0w3 evvkmar0" id="syntax">
    </div>
   </a>
  </h2>
  <p class="leafygreen-ui-1kp3ins">
   The
   <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-linearFill" target="_self">
    <span>
     <code class="leafygreen-ui-1l06pbn">
      $linearFill
     </code>
    </span>
   </a>
   expression has this syntax:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ $linearFill: &lt;expression&gt; }"}
  </script>
  <div class="css-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">{ $linearFill: &lt;expression&gt; }</td></tr></tbody></table></code></pre>
     <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
      <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
       <div class="leafygreen-ui-xhlipt">
        <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
         <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 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>
 </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-group-grp.-linearFill" target="_self">
    <span>
     <code class="leafygreen-ui-1l06pbn">
      $linearFill
     </code>
    </span>
   </a>
   fills
   <code class="leafygreen-ui-1l06pbn">
    null
   </code>
   and missing fields using
   <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/wiki/Linear_interpolation" rel="noopener noreferrer">
    <span>
     linear interpolation
    </span>
    <svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
     <path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
     </path>
     <path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
     </path>
    </svg>
   </a>
   based on surrounding non-
   <code class="leafygreen-ui-1l06pbn">
    null
   </code>
   field values.
The surrounding field values are determined by the sort order specified
in
   <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
    <code class="leafygreen-ui-1l06pbn">
     $setWindowFields
    </code>
    .
   </a>
  </p>
  <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.-linearFill" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        $linearFill
       </code>
      </span>
     </a>
     fills
     <code class="leafygreen-ui-1l06pbn">
      null
     </code>
     and missing values proportionally
spanning the value range between surrounding non-
     <code class="leafygreen-ui-1l06pbn">
      null
     </code>
     values. To
determine the values for missing fields,
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-linearFill" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        $linearFill
       </code>
      </span>
     </a>
     uses:
    </p>
    <ul class="leafygreen-ui-rioki0">
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       The difference of surrounding non-
       <code class="leafygreen-ui-1l06pbn">
        null
       </code>
       values.
      </p>
     </li>
     <li class="leafygreen-ui-d7p7b6">
      <p class="leafygreen-ui-1kp3ins">
       The number of
       <code class="leafygreen-ui-1l06pbn">
        null
       </code>
       fields to fill between the surrounding
values.
      </p>
     </li>
    </ul>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-linearFill" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        $linearFill
       </code>
      </span>
     </a>
     can fill multiple consecutive
     <code class="leafygreen-ui-1l06pbn">
      null
     </code>
     values if
those values are preceded and followed by non-
     <code class="leafygreen-ui-1l06pbn">
      null
     </code>
     values
according to the sort order specified in
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
      <code class="leafygreen-ui-1l06pbn">
       $setWindowFields
      </code>
      .
     </a>
    </p>
    <div class="leafygreen-ui-vg7qwp" role="note">
     <h2 class="leafygreen-ui-2xicti">
      Example
     </h2>
     <div class="leafygreen-ui-9mycqc">
      <p class="leafygreen-ui-1kp3ins">
       If a collection contains these documents:
      </p>
      <script class="structured_data" type="application/ld+json">
       {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ index: 0, value: 0 },\n{ index: 1, value: null },\n{ index: 2, value: null },\n{ index: 3, value: null },\n{ index: 4, value: 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">index</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">value</span>: <span class="lg-highlight-number">0</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">index</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">value</span>: <span class="lg-highlight-literal">null</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">index</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">value</span>: <span class="lg-highlight-literal">null</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">index</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">value</span>: <span class="lg-highlight-literal">null</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">index</span>: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-attr">value</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">
       After using
       <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-linearFill" target="_self">
        <span>
         <code class="leafygreen-ui-1l06pbn">
          $linearFill
         </code>
        </span>
       </a>
       to fill the
       <code class="leafygreen-ui-1l06pbn">
        null
       </code>
       values, the
documents become:
      </p>
      <script class="structured_data" type="application/ld+json">
       {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"{ index: 0, value: 0 },\n{ index: 1, value: 2.5 },\n{ index: 2, value: 5 },\n{ index: 3, value: 7.5 },\n{ index: 4, value: 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-attr">index</span>: <span class="lg-highlight-number">0</span>, <span class="lg-highlight-attr">value</span>: <span class="lg-highlight-number">0</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">index</span>: <span class="lg-highlight-number">1</span>, <span class="lg-highlight-attr">value</span>: <span class="lg-highlight-number">2.5</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">index</span>: <span class="lg-highlight-number">2</span>, <span class="lg-highlight-attr">value</span>: <span class="lg-highlight-number">5</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">index</span>: <span class="lg-highlight-number">3</span>, <span class="lg-highlight-attr">value</span>: <span class="lg-highlight-number">7.5</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">{ <span class="lg-highlight-attr">index</span>: <span class="lg-highlight-number">4</span>, <span class="lg-highlight-attr">value</span>: <span class="lg-highlight-number">10</span> }</td></tr></tbody></table></code></pre>
        </div>
       </div>
      </div>
      <p class="leafygreen-ui-1kp3ins">
       For a complete example, see
       <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-linearFill-example" target="_self">
        <span>
         Examples
         <!-- -->
         .
        </span>
       </a>
      </p>
     </div>
    </div>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     <code class="leafygreen-ui-1l06pbn">
      null
     </code>
     values that are not preceded and followed by non-
     <code class="leafygreen-ui-1l06pbn">
      null
     </code>
     values remain
     <code class="leafygreen-ui-1l06pbn">
      null
     </code>
     .
    </p>
   </li>
  </ul>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Comparison of
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/fill/#mongodb-pipeline-pipe.-fill">
     <code class="leafygreen-ui-1l06pbn">
      $fill
     </code>
    </a>
    and
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-linearFill" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $linearFill
      </code>
     </span>
    </a>
    <a class="headerlink leafygreen-ui-1kepx47" href="#comparison-of--and" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="comparison-of--and">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    To fill missing field values 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>
    , you can use:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/fill/#mongodb-pipeline-pipe.-fill">
       <code class="leafygreen-ui-1l06pbn">
        $fill
       </code>
      </a>
      stage  with
      <code class="leafygreen-ui-1l06pbn">
       { method: "linear" }
      </code>
      .
     </p>
     <p class="leafygreen-ui-1kp3ins">
      When you use the
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/fill/#mongodb-pipeline-pipe.-fill">
       <code class="leafygreen-ui-1l06pbn">
        $fill
       </code>
      </a>
      stage, the field you specify in the
output is the same field used as the source data. See
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/fill/#std-label-fill-example-linear">
       Fill Missing Field Values with Linear Interpolation
       <!-- -->
       .
      </a>
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      The
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-linearFill" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         $linearFill
        </code>
       </span>
      </a>
      operator inside of a
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
       <code class="leafygreen-ui-1l06pbn">
        $setWindowFields
       </code>
      </a>
      stage.
     </p>
     <p class="leafygreen-ui-1kp3ins">
      When you use the
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-linearFill" target="_self">
       <span>
        <code class="leafygreen-ui-1l06pbn">
         $linearFill
        </code>
       </span>
      </a>
      operator, you can set values
for a different field than the field used as the source data. See
      <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#std-label-linearFill-example-multiple-methods" target="_self">
       <span>
        Use Multiple Fill Methods in a Single Stage
        <!-- -->
        .
       </span>
      </a>
     </p>
    </li>
   </ul>
   <span class="leafygreen-ui-1qnjd6d" id="std-label-linearFill-example">
   </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>
  <p class="leafygreen-ui-1kp3ins">
   The examples on this page use a
   <code class="leafygreen-ui-1l06pbn">
    stock
   </code>
   collection that contains
tracks a single company's stock price at hourly intervals:
  </p>
  <script class="structured_data" type="application/ld+json">
   {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.stock.insertMany( [\n   {\n      time: ISODate(\"2021-03-08T09:00:00.000Z\"),\n      price: 500\n   },\n   {\n      time: ISODate(\"2021-03-08T10:00:00.000Z\"),\n   },\n   {\n      time: ISODate(\"2021-03-08T11:00:00.000Z\"),\n      price: 515\n   },\n   {\n      time: ISODate(\"2021-03-08T12:00:00.000Z\")\n   },\n   {\n      time: ISODate(\"2021-03-08T13:00:00.000Z\")\n   },\n   {\n      time: ISODate(\"2021-03-08T14:00:00.000Z\"),\n      price: 485\n   }\n] )","programmingLanguage":"JavaScript"}
  </script>
  <div class="css-16lmvu9">
   <div class="leafygreen-ui-196mwvg">
    <div class="leafygreen-ui-yzcs8i">
     <pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">stock</span>.<span class="lg-highlight-title lg-highlight-function">insertMany</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T09:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">500</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T10:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->}<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T11:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">515</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T12:00:00.000Z"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T13:00:00.000Z"</span>)</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T14:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">485</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
     <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
      <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
       <div class="leafygreen-ui-xhlipt">
        <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
         <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
         </path>
        </svg>
       </div>
      </button>
     </div>
    </div>
   </div>
  </div>
  <p class="leafygreen-ui-1kp3ins">
   The
   <code class="leafygreen-ui-1l06pbn">
    price
   </code>
   field is missing for some of the documents in the
collection.
  </p>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Fill Missing Values with Linear Interpolation
    <a class="headerlink leafygreen-ui-1kepx47" href="#fill-missing-values-with-linear-interpolation" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="fill-missing-values-with-linear-interpolation">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    To populate the missing
    <code class="leafygreen-ui-1l06pbn">
     price
    </code>
    values using
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/wiki/Linear_interpolation" rel="noopener noreferrer">
     <span>
      linear interpolation
     </span>
     <svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
      <path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
      </path>
      <path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
      </path>
     </svg>
    </a>
    ,
use
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-linearFill" target="_self">
     <span>
      <code class="leafygreen-ui-1l06pbn">
       $linearFill
      </code>
     </span>
    </a>
    inside of a
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
     <code class="leafygreen-ui-1l06pbn">
      $setWindowFields
     </code>
    </a>
    stage:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.stock.aggregate( [\n   {\n      $setWindowFields:\n         {\n            sortBy: { time: 1 },\n            output:\n               {\n                  price: { $linearFill: \"$price\" }\n               }\n         }\n   }\n] )","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-yzcs8i">
      <pre class="leafygreen-ui-11itue3" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">stock</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">$setWindowFields</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">            <span class="lg-highlight-attr">sortBy</span>: <!-- -->{ <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">            <span class="lg-highlight-attr">output</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                  <span class="lg-highlight-attr">price</span>: <!-- -->{ <span class="lg-highlight-attr">$linearFill</span>: <span class="lg-highlight-string">"$price"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
      <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
       <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
        <div class="leafygreen-ui-xhlipt">
         <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
          <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
          </path>
         </svg>
        </div>
       </button>
      </div>
     </div>
    </div>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    In the example:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      <code class="leafygreen-ui-1l06pbn">
       sortBy: { time: 1 }
      </code>
      sorts the documents by the
      <code class="leafygreen-ui-1l06pbn">
       time
      </code>
      field in
ascending order, from earliest to latest.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-output">
       output
      </a>
      specifies:
     </p>
     <ul class="leafygreen-ui-rioki0">
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        <code class="leafygreen-ui-1l06pbn">
         price
        </code>
        as the field for which to fill in missing values.
       </p>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        <code class="leafygreen-ui-1l06pbn">
         { $linearFill: "$price" }
        </code>
        as the value for the missing field.
        <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-linearFill" target="_self">
         <span>
          <code class="leafygreen-ui-1l06pbn">
           $linearFill
          </code>
         </span>
        </a>
        fills missing
        <code class="leafygreen-ui-1l06pbn">
         price
        </code>
        values using
        <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/wiki/Linear_interpolation" rel="noopener noreferrer">
         <span>
          linear interpolation
         </span>
         <svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
          <path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
          </path>
          <path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
          </path>
         </svg>
        </a>
        based on the surrounding
        <code class="leafygreen-ui-1l06pbn">
         price
        </code>
        values in
the sequence.
       </p>
      </li>
     </ul>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    Example output:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n   {\n      _id: ObjectId(\"620ad555394d47411658b5ef\"),\n      time: ISODate(\"2021-03-08T09:00:00.000Z\"),\n      price: 500\n   },\n   {\n      _id: ObjectId(\"620ad555394d47411658b5f0\"),\n      time: ISODate(\"2021-03-08T10:00:00.000Z\"),\n      price: 507.5\n   },\n   {\n      _id: ObjectId(\"620ad555394d47411658b5f1\"),\n      time: ISODate(\"2021-03-08T11:00:00.000Z\"),\n      price: 515\n   },\n   {\n      _id: ObjectId(\"620ad555394d47411658b5f2\"),\n      time: ISODate(\"2021-03-08T12:00:00.000Z\"),\n      price: 505\n   },\n   {\n      _id: ObjectId(\"620ad555394d47411658b5f3\"),\n      time: ISODate(\"2021-03-08T13:00:00.000Z\"),\n      price: 495\n   },\n   {\n      _id: ObjectId(\"620ad555394d47411658b5f4\"),\n      time: ISODate(\"2021-03-08T14:00:00.000Z\"),\n      price: 485\n   }\n]","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-1lju8ps">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1jheeh3">
      <pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5ef"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T09:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">500</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5f0"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T10:00:00.000Z"</span>)<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">507.5</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5f1"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T11:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">515</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5f2"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T12:00:00.000Z"</span>)<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">505</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5f3"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T13:00:00.000Z"</span>)<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">495</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5f4"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T14:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">485</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }</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-linearFill-example-multiple-methods">
   </span>
  </section>
  <section>
   <h3 class="contains-headerlink leafygreen-ui-15ajumo" weight="medium">
    Use Multiple Fill Methods in a Single Stage
    <a class="headerlink leafygreen-ui-1kepx47" href="#use-multiple-fill-methods-in-a-single-stage" title="Permalink to this heading">
     <svg aria-label="Link Icon" class="leafygreen-ui-a30zj9" fill="none" height="12" role="img" viewbox="0 0 16 16" width="12" xmlns="http://www.w3.org/2000/svg">
      <path d="M6.039 9.953a3.596 3.596 0 0 1-.327-.38l1.45-1.445a1.553 1.553 0 0 0 2.496.423l2.885-2.87a1.57 1.57 0 0 0 .01-2.213 1.553 1.553 0 0 0-2.203-.01l-.379.377A.995.995 0 0 1 8.56 3.83a1.005 1.005 0 0 1 .006-1.418l.38-.377a3.542 3.542 0 0 1 5.024.023 3.58 3.58 0 0 1-.022 5.047l-2.884 2.871a3.542 3.542 0 0 1-5.025-.022Z" fill="currentColor">
      </path>
      <path d="M9.961 6.047c.12.12.228.248.327.38l-1.45 1.445a1.553 1.553 0 0 0-2.496-.423l-2.885 2.87a1.57 1.57 0 0 0-.01 2.213 1.553 1.553 0 0 0 2.203.01l.379-.377a.995.995 0 0 1 1.411.006 1.005 1.005 0 0 1-.006 1.418l-.38.377a3.542 3.542 0 0 1-5.024-.023 3.58 3.58 0 0 1 .022-5.047l2.884-2.871a3.542 3.542 0 0 1 5.025.022Z" fill="currentColor">
      </path>
     </svg>
     <div class="css-1btu0w3 evvkmar0" id="use-multiple-fill-methods-in-a-single-stage">
     </div>
    </a>
   </h3>
   <p class="leafygreen-ui-1kp3ins">
    When you use the
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
     <code class="leafygreen-ui-1l06pbn">
      $setWindowFields
     </code>
    </a>
    stage to fill missing
values, you can set values for a different field than the field you
fill from. As a result, you can use multiple fill methods in a single
    <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
     <code class="leafygreen-ui-1l06pbn">
      $setWindowFields
     </code>
    </a>
    stage and output the results in distinct
fields.
   </p>
   <p class="leafygreen-ui-1kp3ins">
    The following pipeline populates missing
    <code class="leafygreen-ui-1l06pbn">
     price
    </code>
    fields using
    <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/wiki/Linear_interpolation" rel="noopener noreferrer">
     <span>
      linear interpolation
     </span>
     <svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
      <path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
      </path>
      <path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
      </path>
     </svg>
    </a>
    and the last-observation-carried-forward method:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"db.stock.aggregate( [\n   {\n      $setWindowFields:\n         {\n            sortBy: { time: 1 },\n            output:\n               {\n                  linearFillPrice: { $linearFill: \"$price\" },\n                  locfPrice: { $locf: \"$price\" }\n               }\n         }\n   }\n] )","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-16lmvu9">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1b1bvay">
      <pre class="leafygreen-ui-11itue3" tabindex="0"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">db.<span class="lg-highlight-property">stock</span>.<span class="lg-highlight-title lg-highlight-function">aggregate</span>( <!-- -->[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">      <span class="lg-highlight-attr">$setWindowFields</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">            <span class="lg-highlight-attr">sortBy</span>: <!-- -->{ <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-number">1</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">            <span class="lg-highlight-attr">output</span>:</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                  <span class="lg-highlight-attr">linearFillPrice</span>: <!-- -->{ <span class="lg-highlight-attr">$linearFill</span>: <span class="lg-highlight-string">"$price"</span> }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">                  <span class="lg-highlight-attr">locfPrice</span>: <!-- -->{ <span class="lg-highlight-attr">$locf</span>: <span class="lg-highlight-string">"$price"</span> }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">               }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">         }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">   }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">] )</td></tr></tbody></table></code></pre>
      <div class="leafygreen-ui-1bqcprk" data-testid="leafygreen-code-panel">
       <button aria-disabled="false" aria-label="Copy" class="leafygreen-ui-1c6zwke" data-testid="code_copy-button" tabindex="0">
        <div class="leafygreen-ui-xhlipt">
         <svg aria-label="Copy Icon" class="leafygreen-ui-1st74h4" height="16" role="img" viewbox="0 0 16 16" width="16">
          <path clip-rule="evenodd" d="M1 5.71428V10.2857C1 11.2325 1.76751 12 2.71429 12H5.75V7.10957C5.75 6.54414 5.97724 6.00244 6.38065 5.60623L8.67403 3.35381C8.77447 3.25516 8.88376 3.16757 9 3.09182V2.71429C9 1.76751 8.23249 1 7.28571 1H5.8V4.42857C5.8 5.13865 5.22437 5.71428 4.51429 5.71428H1ZM9 4.78571L7.25654 6.49804C7.24689 6.50752 7.23749 6.5172 7.22834 6.52708C7.22208 6.53383 7.21594 6.54068 7.20991 6.54762C7.07504 6.70295 7 6.90234 7 7.10957V7.79762H9H10.0095C10.4829 7.79762 10.8667 7.41386 10.8667 6.94047V4H10.1505C9.92587 4 9.7102 4.0882 9.54992 4.24562L9 4.78571ZM4.86667 1H4.15053C3.92587 1 3.7102 1.0882 3.54992 1.24562L1.25654 3.49804C1.09244 3.65921 1 3.87957 1 4.10957V4.79762H4.00952C4.48291 4.79762 4.86667 4.41386 4.86667 3.94047V1ZM7 12V8.71428H9H10.5143C11.2244 8.71428 11.8 8.13865 11.8 7.42857V4H13.2857C14.2325 4 15 4.76751 15 5.71429V13.2857C15 14.2325 14.2325 15 13.2857 15H8.71429C7.76751 15 7 14.2325 7 13.2857V12Z" fill="currentColor" fill-rule="evenodd">
          </path>
         </svg>
        </div>
       </button>
      </div>
     </div>
    </div>
   </div>
   <p class="leafygreen-ui-1kp3ins">
    In the example:
   </p>
   <ul class="leafygreen-ui-rioki0">
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      <code class="leafygreen-ui-1l06pbn">
       sortBy: { time: 1 }
      </code>
      sorts the documents by the
      <code class="leafygreen-ui-1l06pbn">
       time
      </code>
      field in
ascending order, from earliest to latest.
     </p>
    </li>
    <li class="leafygreen-ui-d7p7b6">
     <p class="leafygreen-ui-1kp3ins">
      <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-output">
       output
      </a>
      specifies:
     </p>
     <ul class="leafygreen-ui-rioki0">
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        <code class="leafygreen-ui-1l06pbn">
         linearFillPrice
        </code>
        as a target field to be filled.
       </p>
       <ul class="leafygreen-ui-rioki0">
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          <code class="leafygreen-ui-1l06pbn">
           { $linearFill: "$price" }
          </code>
          is the value for the
          <code class="leafygreen-ui-1l06pbn">
           linearFillPrice
          </code>
          field.
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-linearFill" target="_self">
           <span>
            <code class="leafygreen-ui-1l06pbn">
             $linearFill
            </code>
           </span>
          </a>
          fills missing
          <code class="leafygreen-ui-1l06pbn">
           price
          </code>
          values using
          <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="https://en.wikipedia.org/wiki/Linear_interpolation" rel="noopener noreferrer">
           <span>
            linear interpolation
           </span>
           <svg alt="" aria-hidden="true" class="leafygreen-ui-1ie5kuq" height="16" role="presentation" viewbox="0 0 16 16" width="16">
            <path d="M13.823 2.4491C13.8201 2.30008 13.6999 2.17994 13.5509 2.17704L9.5062 2.09836C9.25654 2.09351 9.12821 2.39519 9.30482 2.5718L10.3856 3.65257L7.93433 6.10383C7.87964 6.15852 7.83047 6.21665 7.78683 6.27752L5.99909 8.06525C5.46457 8.59977 5.46457 9.4664 5.99909 10.0009C6.53361 10.5354 7.40023 10.5354 7.93475 10.0009L9.72249 8.21317C9.78336 8.16953 9.84148 8.12037 9.89618 8.06567L12.3474 5.61441L13.4282 6.69518C13.6048 6.87179 13.9065 6.74347 13.9016 6.4938L13.823 2.4491Z" fill="currentColor">
            </path>
            <path d="M7.25 3.12893C7.66421 3.12893 8 3.46472 8 3.87893C8 4.29315 7.66421 4.62893 7.25 4.62893H4C3.72386 4.62893 3.5 4.85279 3.5 5.12893V11.9929C3.5 12.2691 3.72386 12.4929 4 12.4929H10.864C11.1401 12.4929 11.364 12.2691 11.364 11.9929V8.75C11.364 8.33579 11.6998 8 12.114 8C12.5282 8 12.864 8.33579 12.864 8.75V11.9929C12.864 13.0975 11.9686 13.9929 10.864 13.9929H4C2.89543 13.9929 2 13.0975 2 11.9929V5.12893C2 4.02436 2.89543 3.12893 4 3.12893H7.25Z" fill="currentColor">
            </path>
           </svg>
          </a>
          based on the
surrounding
          <code class="leafygreen-ui-1l06pbn">
           price
          </code>
          values in the sequence.
         </p>
        </li>
       </ul>
      </li>
      <li class="leafygreen-ui-d7p7b6">
       <p class="leafygreen-ui-1kp3ins">
        <code class="leafygreen-ui-1l06pbn">
         locfPrice
        </code>
        as a target field to be filled.
       </p>
       <ul class="leafygreen-ui-rioki0">
        <li class="leafygreen-ui-d7p7b6">
         <p class="leafygreen-ui-1kp3ins">
          <code class="leafygreen-ui-1l06pbn">
           { $locf: "$price" }
          </code>
          is the value for the
          <code class="leafygreen-ui-1l06pbn">
           locfPrice
          </code>
          field.
          <code class="leafygreen-ui-1l06pbn">
           locf
          </code>
          stands for last observation carried forward.
          <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/locf/#mongodb-group-grp.-locf">
           <code class="leafygreen-ui-1l06pbn">
            $locf
           </code>
          </a>
          fills missing
          <code class="leafygreen-ui-1l06pbn">
           price
          </code>
          values with the value from
the previous document in the sequence.
         </p>
        </li>
       </ul>
      </li>
     </ul>
    </li>
   </ul>
   <p class="leafygreen-ui-1kp3ins">
    Example output:
   </p>
   <script class="structured_data" type="application/ld+json">
    {"@context":"https://schema.org","@type":"SoftwareSourceCode","codeSampleType":"code snippet","text":"[\n  {\n    _id: ObjectId(\"620ad555394d47411658b5ef\"),\n    time: ISODate(\"2021-03-08T09:00:00.000Z\"),\n    price: 500,\n    linearFillPrice: 500,\n    locfPrice: 500\n  },\n  {\n    _id: ObjectId(\"620ad555394d47411658b5f0\"),\n    time: ISODate(\"2021-03-08T10:00:00.000Z\"),\n    linearFillPrice: 507.5,\n    locfPrice: 500\n  },\n  {\n    _id: ObjectId(\"620ad555394d47411658b5f1\"),\n    time: ISODate(\"2021-03-08T11:00:00.000Z\"),\n    price: 515,\n    linearFillPrice: 515,\n    locfPrice: 515\n  },\n  {\n    _id: ObjectId(\"620ad555394d47411658b5f2\"),\n    time: ISODate(\"2021-03-08T12:00:00.000Z\"),\n    linearFillPrice: 505,\n    locfPrice: 515\n  },\n  {\n    _id: ObjectId(\"620ad555394d47411658b5f3\"),\n    time: ISODate(\"2021-03-08T13:00:00.000Z\"),\n    linearFillPrice: 495,\n    locfPrice: 515\n  },\n  {\n    _id: ObjectId(\"620ad555394d47411658b5f4\"),\n    time: ISODate(\"2021-03-08T14:00:00.000Z\"),\n    price: 485,\n    linearFillPrice: 485,\n    locfPrice: 485\n  }\n]","programmingLanguage":"JavaScript"}
   </script>
   <div class="css-1lju8ps">
    <div class="leafygreen-ui-196mwvg">
     <div class="leafygreen-ui-1jheeh3">
      <pre class="leafygreen-ui-1453az5" tabindex="-1"><code class="lg-highlight-hljs-light javascript leafygreen-ui-9ybzas"><table class="leafygreen-ui-1tr4g10"><tbody><tr class=""><td class="leafygreen-ui-7razhx">[</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5ef"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T09:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">500</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">linearFillPrice</span>: <span class="lg-highlight-number">500</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">locfPrice</span>: <span class="lg-highlight-number">500</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5f0"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T10:00:00.000Z"</span>)<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">linearFillPrice</span>: <span class="lg-highlight-number">507.5</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">locfPrice</span>: <span class="lg-highlight-number">500</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5f1"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T11:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">515</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">linearFillPrice</span>: <span class="lg-highlight-number">515</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">locfPrice</span>: <span class="lg-highlight-number">515</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5f2"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T12:00:00.000Z"</span>)<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">linearFillPrice</span>: <span class="lg-highlight-number">505</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">locfPrice</span>: <span class="lg-highlight-number">515</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5f3"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T13:00:00.000Z"</span>)<!-- -->,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">linearFillPrice</span>: <span class="lg-highlight-number">495</span>,</td></tr><tr class="leafygreen-ui-1wr1lm6"><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">locfPrice</span>: <span class="lg-highlight-number">515</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  }<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">  <!-- -->{</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">_id</span>: <span class="lg-highlight-title lg-highlight-class">ObjectId</span>(<span class="lg-highlight-string">"620ad555394d47411658b5f4"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">time</span>: <span class="lg-highlight-title lg-highlight-class">ISODate</span>(<span class="lg-highlight-string">"2021-03-08T14:00:00.000Z"</span>)<!-- -->,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">price</span>: <span class="lg-highlight-number">485</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">linearFillPrice</span>: <span class="lg-highlight-number">485</span>,</td></tr><tr class=""><td class="leafygreen-ui-7razhx">    <span class="lg-highlight-attr">locfPrice</span>: <span class="lg-highlight-number">485</span></td></tr><tr class=""><td class="leafygreen-ui-7razhx">  }</td></tr><tr class=""><td class="leafygreen-ui-7razhx">]</td></tr></tbody></table></code></pre>
     </div>
    </div>
   </div>
  </section>
 </section>
 <section>
  <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>
  <ul class="leafygreen-ui-rioki0">
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     To use
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-linearFill" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        $linearFill
       </code>
      </span>
     </a>
     , you must use the
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-sortBy">
      sortBy
     </a>
     field to sort your data.
    </p>
   </li>
   <li class="leafygreen-ui-d7p7b6">
    <p class="leafygreen-ui-1kp3ins">
     When using
     <a class="lg-ui-0001 leafygreen-ui-fgcsv5" href="#mongodb-group-grp.-linearFill" target="_self">
      <span>
       <code class="leafygreen-ui-1l06pbn">
        $linearFill
       </code>
      </span>
     </a>
     window function,
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#mongodb-pipeline-pipe.-setWindowFields">
      <code class="leafygreen-ui-1l06pbn">
       $setWindowFields
      </code>
     </a>
     returns an error if there are any
repeated values in the
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-sortBy">
      sortBy
     </a>
     field
in a single
     <a class="leafygreen-ui-tqgtui" href="/docs/manual/reference/operator/aggregation/setWindowFields/#std-label-setWindowFields-partitionBy">
      partition
      <!-- -->
      .
     </a>
    </p>
   </li>
  </ul>
 </section>
</section>
