title: MongoDB - How do join between the two collections by getting only one document from the second collection?
body: <p>I am using Mongo 3.4.24. I have two collections - <strong>health</strong>, <strong>health_history</strong>.</p>
  <p>I keep health of the different nodes in the document in the health collection and during each snapshot I move the older documents to health_history</p>
  <p><strong>health</strong></p>
  <pre><code>{
      &quot;_id&quot;: ObjectId(&quot;61857adabe73638f9c8bca19&quot;),
      &quot;name&quot;: &quot;Test&quot;,
      &quot;snapshot_time&quot;: &quot;2021-11-05 11:41:28&quot;,
      &quot;status&quot;: &quot;DOWN&quot;,
      &quot;create_time&quot;: ISODate(&quot;2021-11-05T18:41:30.468Z&quot;)
  }
  </code></pre>
  <p><strong>health_history</strong> - which keeps the older documents from <strong>health</strong> collection</p>
  <p>I want to find how long Test has been DOWN for by finding the last time when the status was UP.</p>
  <p>The challenge is there are multiple documents for <strong>Test</strong> in health_history. I need to find the latest <strong>create_time</strong> from the document in <strong>health_history</strong> when it was <strong>UP</strong> and calculate the time difference (in minutes) with the document in <strong>health</strong> collection and create a new key <strong>duration</strong></p>
  <p>Something like</p>
  <pre><code>{
      &quot;_id&quot;: ObjectId(&quot;61857adabe73638f9c8bca19&quot;),
      &quot;name&quot;: &quot;Test&quot;,
      &quot;snapshot_time&quot;: &quot;2021-11-05 11:41:28&quot;,
      &quot;status&quot;: &quot;DOWN&quot;,
      &quot;create_time&quot;: ISODate(&quot;2021-11-05T18:41:30.468Z&quot;),
      &quot;duration&quot;: &quot;30&quot;
  
  }
  </code></pre>
  <p>How do I do this in Mongo?</p>