title: MongoDB Aggregate Query On Property
body: <p>I have the following document structure stored in a collection:</p>
  
  <pre><code>{
      "name": "Empire State Building",
      "location": {
          "city": "New York",
          "lat": 54.665434,
          "lng": 3.34434
      }
  }
  </code></pre>
  
  <p>The collection contains the coordinates of various landmarks in a number of different cities across the world.</p>
  
  <p>What I want to be able to do, is get the average <code>lat</code> and <code>lng</code> for all cities based on the records within the collection.</p>
  
  <p>So far I am  not having  much luck, I can pull out distinct city  names using the following query:</p>
  
  <pre><code>db.pubs.distinct("Location.City").sort()
  </code></pre>
  
  <p>However I am at a loss on where to start with getting the average of the lat and lng properties for all matching values.</p>
  
  <p>If it makes any difference I am using the <code>mongo shell</code>.</p>