title: How to calculate Rating in my MongoDB design
body: <p>I'm creating a system that users can write review about an item and rate it from 0-5. I'm using MongoDB for this. And my problem is to find the best solution to calculate the total rating in product schema. I don't think querying all comments to get the size and dividing it by total rating is a good solution. Here is my Schema. I appreciate any advice:</p>
  
  <p>Comments:</p>
  
  <pre><code>var commentSchema = new Schema({
  Rating : {  type: Number, default:0 },
  Helpful : {  type: Number, default:0 },
  User :{
      type: Schema.ObjectId,
      ref: 'users'
       },
  Content: String,
  
  
  });
  </code></pre>
  
  <p>Here is my Item schema:</p>
  
  <pre><code>var productSchema = new Schema({
   //id is barcode
  _id : String,
  Rating : {  type: Number, default:0 },
  Comments :[
      {
          type: Schema.ObjectId,
          ref: 'comments'
      }
  ],
  
  });
  </code></pre>
  
  <hr>
  
  <p>EDIT: HERE is the solution I got from another topic : <a href="https://stackoverflow.com/questions/26810599/calculating-average-in-mongoose/26811305#26811305">calculating average in Mongoose</a></p>