title: MongoDB - Find documents with earliest occurrence of duplicate value
body: <p>I have a collection within MongoDB (4.4 but the version isn't important to me) and one of the document values is an array of URLs. There will be multiple documents with multiple URLs per document (within the array) and some of those URLs will already exist.
  I would like to select each document with the earliest occurrence of each URL (the intention being to mark it as the 'origin'.</p>
  <p>MongoPlayground link for sample collection - <a href="https://mongoplayground.net/p/ZAgCqr517-8" rel="nofollow noreferrer">https://mongoplayground.net/p/ZAgCqr517-8</a></p>
  <pre><code>  {
      &quot;title&quot;: &quot;story1_first&quot;,
      &quot;isoDate&quot;: &quot;2022-01-01T00:00:00.000Z&quot;,
      &quot;links&quot;: [
        &quot;www.first.com/article1&quot;,
        &quot;www.anotherdomain.com&quot;
      ]
    },
    {
      &quot;title&quot;: &quot;story1_mention&quot;,
      &quot;isoDate&quot;: &quot;2022-01-10T00:00:00.000Z&quot;,
      &quot;links&quot;: [
        &quot;www.first.com/article1&quot;,
        &quot;www.somesite.com&quot;
      ]
    },
    {
      &quot;title&quot;: &quot;story2_first&quot;,
      &quot;isoDate&quot;: &quot;2022-01-20T00:00:00.000Z&quot;,
      &quot;links&quot;: [
        &quot;www.newstory.com/article2&quot;,
        &quot;www.anothercompany.com&quot;
      ]
    },
    {
      &quot;title&quot;: &quot;story2_mention&quot;,
      &quot;isoDate&quot;: &quot;2022-01-20T00:00:00.000Z&quot;,
      &quot;links&quot;: [
        &quot;www.newstory.com/article2&quot;,
        &quot;www.anothercompany.com&quot;
      ]
    }
  ]
  </code></pre>
  <p>In this example, I would like the query / aggregation to return the two documents with &quot;first&quot; in the title as they are the documents which share a common URL within 'links' and are the documents which have the earliest date. Similar to how a search engine ranks up a site depending on how many other sites link to it.</p>