title: MongoDB: Retrieve most referenced document
body: <p>I have a MongoDB collection (called 'links') with documents like this one:</p>
  
  <pre><code>{
      "_id" : ObjectId("544bc8abd4c66b0e3cf12665"),
      "name" : "Pet 4056 AgR",
      "file" : "P0001J01",
      "quotes" : [
          {
              "_id" : ObjectId("544bc8afd4c66b0e3cf15173"),
              "name" : "Pet 4837 ED",
              "file" : "P1103J03"
          },
          {
              "_id" : ObjectId("544bc8b6d4c66b0e3cf19425"),
              "name" : "ACO 845 AgR",
              "file" : "P2810J07"
          },
          {
              "_id" : ObjectId("544bc8afd4c66b0e3cf14a77"),
              "name" : "ACO 1574 AgR",
              "file" : "P0924J05"
          }
      ]
  }
  </code></pre>
  
  <p>In my db, this means that this document references 3 other documents. 
  For each document, in its <code>quotes</code> array there are no two documents with the same id/name/file. The <code>name</code> field is unique in the collection.</p>
  
  <p>Now, I need to get the document that is the most referenced. It's the document that appears in most <code>quotes</code> arrays. How can I do that?
  I believe this is achieved through an aggregation, but I can't figure out how to do it, especially because the names are inside an array.</p>
  
  <p>Thanks! :)</p>