title: MongoDB Select MIN/MAX from values inside Array
body: <p>So I'm having issues performing a Query to return just two values, MIN and MAX for a field inside an Array field...</p>
  
  <p><code>client.files.uploads</code> is the field I want to get the MIN/MAX.</p>
  
  <p>The result will be just "<code>0,10</code>" for example, I don't want to get more fields, just look into every <code>client.files.uploads</code> field and get the MIN/MAX uploads whole <code>client</code>collection has, but return <strong>only the lowest and highest value</strong> not a row for each client.</p>
  
  <p>This is my query, but is not getting me what I want...</p>
  
  <pre><code>db.client.aggregate([
      {$unwind: "$files"},
      { 
          $group: { 
              _id : "$_id",
              data_min: {$min: "$files.uploads"},
              data_max: {$max: "$files.uploads"}
          }
      },
      {
          $project:{
              data_min: "$min",
              data_max: "$max",
          }
      }
  ],{allowDiskUse:true})
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>I've managed to do what I wanted, but the question still open because I don't thing my way is the best way to achieve it...</p>
  
  <pre><code>db.clients.aggregate([
    {$unwind: "$files"},
    {
      $group: {
        _id: "$_id",
        data_min: {$min: "$files.uploads"},
        data_max: {$max: "$files.uploads"}
      }
    },
    {
  
      $group: {
        _id: "1",
        min: {$min: "$data_min"},
        max: {$max: "$data_max"}
      }
    },
    {
      $project: {
        _id: 0,
        min: 1,
        max: 1
      }
    }
  ],
  {
    allowDiskUse: true
  })
  </code></pre>
  
  <p>This query is returning me one row only, with the lowest and highest value for the whole collection, which is what I wanted.</p>
  
  <p>This is an example data of my documents</p>
  
  <pre><code>[
    {
      "name": "John",
      "files": [
        {
          "uploads": 9685,
          "downloads": 83,
  
        },
        {
          "uploads": 1,
          "downloads": 833
        },
        {
          "uploads": 676,
          "downloads": 823
        }
      ]
    },
    {
      "name": "Peter",
      "files": [
        {
          "uploads": 32,
          "downloads": 99
        },
        {
          "uploads": 34,
          "downloads": 4
        }
      ]
    },
    {
      "name": "Mery",
      "files": [
        {
          "uploads": 3,
          "downloads": 244
        },
        {
          "uploads": 15,
          "downloads": 543
        },
        {
          "uploads": 1345,
          "downloads": 22
        },
        {
          "uploads": 6743,
          "downloads": 87543
        }
      ]
    }
  ]
  </code></pre>