title: Need to get the average of complex data on mongodb
body: <p>I'm not being able to find the average of a complex search in mongodb, using mongoose (nodejs) (and i'm not sure if its possible).</p>
  <p>So, i have a collection of Scouts.
  A Scout is basically the vote of a user for a player, in a match (fixture).
  Scout items looks like this:</p>
  <pre><code>{
      &quot;_id&quot;: &quot;6036dbbd148ccf17e82e6f14&quot;,
      &quot;player&quot;: &quot;60243968d7ec0721344514ab&quot;,
      &quot;user&quot;: &quot;601e9c826a339228e8f54305&quot;,
      &quot;fixture&quot;: &quot;602435741f2e4e263492de66&quot;,
      &quot;score&quot;: 7,
      &quot;text&quot;: &quot;Lorem ipsum&quot;,
      &quot;likes&quot;: 92,
      &quot;createdDate&quot;: &quot;2021-02-24T23:05:33.176Z&quot;,
      &quot;__v&quot;: 0
  },
  {
      &quot;_id&quot;: &quot;6036dbbd148ccf17e82e6f16&quot;,
      &quot;player&quot;: &quot;60243968d7ec0721344514ab&quot;,
      &quot;user&quot;: &quot;601e9c826a339228e8f54306&quot;,
      &quot;fixture&quot;: &quot;602435741f2e4e263492de66&quot;,
      &quot;score&quot;: 1,
      &quot;text&quot;: &quot;Lorem ipsum&quot;,
      &quot;likes&quot;: 76,
      &quot;createdDate&quot;: &quot;2021-02-24T23:05:33.181Z&quot;,
      &quot;__v&quot;: 0
  },
  </code></pre>
  <p>What i need is: the average score of a player in a fixture (among all users).
  And this is what i'm trying:</p>
  <pre><code>  Scout.aggregate([
       { $match: {
          $and: [
            {player: mongoose.Types.ObjectId(req.params.playerId)},
            {fixture: mongoose.Types.ObjectId(req.params.fixtureId)},
          ]}
       },
       { $group: { _id: &quot;$_id&quot;, &quot;avgScore&quot;: { &quot;$avg&quot;: &quot;$score&quot; }}}
     ])
  </code></pre>
  <p>But all i get as result is this (which is not the expected result - should be 4 as the avg):</p>
  <pre><code>{
      &quot;_id&quot;: &quot;6036dbbd148ccf17e82e6f16&quot;,
      &quot;avgScore&quot;: 1
  },
  {
      &quot;_id&quot;: &quot;6036dbbd148ccf17e82e6f14&quot;,
      &quot;avgScore&quot;: 7
  }
  </code></pre>
  <p>]</p>