title: compare two array elements with conditions in mongodb aggregation
body: <p>I have documents as</p>
  <pre><code>[{
          &quot;id&quot;: &quot;1&quot;,
          &quot;base&quot;: 23,
          &quot;dateValues&quot;: [{
              &quot;timestamp&quot;: &quot;2021-02-15T13:13:17.611Z&quot;,
              &quot;value&quot;: 50
          }, {
              &quot;timestamp&quot;: &quot;2021-02-22T13:13:17.611Z&quot;,
              &quot;value&quot;: 50
          }]
      },
      {
          &quot;id&quot;: &quot;2&quot;,
          &quot;base&quot;: 256,
          &quot;dateValues&quot;: [{
              &quot;timestamp&quot;: &quot;2021-02-01T13:13:17.611Z&quot;,
              &quot;value&quot;: 50
          }, {
              &quot;timestamp&quot;: &quot;2021-02-10T13:13:17.611Z&quot;,
              &quot;value&quot;: 50
          }]
      },
      {
          &quot;id&quot;: &quot;3&quot;,
          &quot;base&quot;: 256,
          &quot;dateValues&quot;: [{
              &quot;timestamp&quot;: &quot;2021-02-03T13:13:17.611Z&quot;,
              &quot;value&quot;: 50
          }, {
              &quot;timestamp&quot;: &quot;2021-02-04T13:13:17.611Z&quot;,
              &quot;value&quot;: 50
          }, {
              &quot;timestamp&quot;: &quot;2021-02-10T13:13:17.611Z&quot;,
              &quot;value&quot;: 50
          }]
      },
      {
          &quot;id&quot;: &quot;4&quot;,
          &quot;base&quot;: 256,
          &quot;dateValues&quot;: [{
              &quot;timestamp&quot;: &quot;2021-02-03T13:13:17.611Z&quot;,
              &quot;value&quot;: 50
          }, {
              &quot;timestamp&quot;: &quot;2021-02-08T13:13:17.611Z&quot;,
              &quot;value&quot;: 50
          }]
      }
  ]
  </code></pre>
  <p>Now I want to compare dates of all objects. I'll simplify the logic as simply as I can step wise..</p>
  <ol>
  <li>First I want to get all the distinct set of dates so these will be</li>
  </ol>
  <pre><code>&quot;2021-02-01&quot;
  &quot;2021-02-03&quot;
  &quot;2021-02-04&quot;
  &quot;2021-02-08&quot;
  &quot;2021-02-15&quot;
  &quot;2021-02-22&quot;
  &quot;2021-02-10&quot;
  </code></pre>
  <ol start="2">
  <li>Now I want to loop above dates and compare each date with each objects dateValues</li>
  </ol>
  <p>For example if we take first date &quot;2021-02-01&quot;, I want only one date from each object(document)</p>
  <ul>
  <li>who are less than or equal to &quot;2021-02-01&quot;</li>
  <li>whose timestamp is latest timestamp</li>
  <li>If no dateValue found then we take base value</li>
  <li>Finally we will sum these values</li>
  </ul>
  <p>so for this date <strong>&quot;2021-02-01&quot;</strong> dateValues will be</p>
  <pre><code>for id 1 no date value is there so in this case I need to take base value, which is 23
  for id 2 we found date value so we will take its corresponding value, which is 50
  for id 3 and 4 no date is matching so again we are taking its base value which 256 and 256
  so final value comes for date &quot;2021-02-01&quot; is 23+50+256+256 = 585
  </code></pre>
  <p>Just another example for date <strong>&quot;2021-02-22&quot;</strong></p>
  <pre><code>for id 1 we found date &quot;2021-02-22&quot;, value is 50
  for id 2 we found date &quot;2021-02-10&quot; value is 50
  for id 3 we found date &quot;2021-02-10&quot; value is 50
  for id 4 we found date &quot;2021-02-08&quot; value is 50
  Final value for date &quot;2021-02-22&quot; is 200
  </code></pre>
  <p>Can we do this logic in mongo db aggregations?
  I could only get distinct dates but after that I lost it. Can anyone help me with this mongodb aggregation? or is it even possible to implement this logic in mongodb aggregation?</p>