title: Select last value of array in sub document
body: <p>I have the following document structure</p>
  
  <pre><code>{ 
      "_id" : 1, 
      "prices" : { 
          "100" : [
                  5.67,
                  .
                  .
                  1.7,    
              ], 
          "101" : [
                  4.67,
                  .
                  .
                  1.4,  
      }, 
      "v" : 2 
  }
  </code></pre>
  
  <p>We need to get the last value from each of the fields "100" and "101", e.g 1.7 &amp; 1.4, using mongodb v3.0.2. Currently we are loading the whole document and then getting the required values in the application. It has become quite a bottleneck as each document may be ~1MB, which for 100s of documents is adding up. </p>
  
  <p>We are hoping there is a suitable feature of mongodb that will allow us to just retrieve the data we need from the document, possibly as a separate call for each price field, i.e one call for the price "100" key and second call for the price "101" key.</p>
  
  <p>We have experimented using the $slice operator but this unfortunately cannot be used with exclusion fields, <a href="https://jira.mongodb.org/browse/SERVER-3378" rel="nofollow noreferrer">https://jira.mongodb.org/browse/SERVER-3378</a>.</p>
  
  <p>We have also seen the following post:
  <a href="https://stackoverflow.com/questions/7670073/how-to-combine-both-slice-and-select-returned-keys-operation-in-function-update">How to combine both $slice and select returned keys operation in function update?</a></p>
  
  <p>Which almost works, but the return value from the query:</p>
  
  <pre><code>db.account_articles.find({"_id" : ObjectId("1")}, { "prices.100" : { $slice: -1 }, "_id" : 1 })
  </code></pre>
  
  <p>is:</p>
  
  <pre><code>{ 
      "_id" : 1, 
      "prices" : { 
          "100" : [
                  1.7    
              ], 
          "101" : [
                  4.67,
                  .
                  .
                  1.4,  
      }
  }
  </code></pre>
  
  <p>i.e. it's 90% of the way there, we just need to be able to exclude the "101" field.</p>
  
  <p>Are we missing something or is this not possible in monogodb?</p>