title: "Unrecognized expression '$addFields'"
body: <p>I am getting an error :</p>
  
  <blockquote>
    <p>"Unrecognized expression '$addFields'" </p>
  </blockquote>
  
  <p>When I am trying to calculate the average of fields from an array using MongoDB aggregation framework.</p>
  
  <p>Can some one help me to understand what is wrong in the below code snippet.</p>
  
  <p>My DB version is 3.4.5</p>
  
  <pre><code>"doctor": {
  
          "$map": {
              "input": "$$procedure.doctor",
              "as": "doctor",
              "in": {
                  "$cond": [//specify the filter for doctor
                      {
                          "$eq": ["$$doctor.activeFlag", "Y"]
                      },
                      {
                          "doctorName": "$$doctor.doctorName", 
  
                          "DoctorUserRating": {
  
                              "$map": {
                                  "input": "$$doctor.DoctorUserRating",
                                  "as": "docUserRate",
                                  "in": {
                                      "$cond": [//specify the filter for user rating
                                      {},
                                      {
                                       //Calculate the average of userRating from array
                                          "$addFields": {
                                              "avarageDocRating": {
                                                  "$divide": [
                                                      { // expression returns total
                                                          "$reduce": {
                                                              "input": "$$docUserRate",
                                                              "initialValue": 0,
                                                              "in": { "$add": ["$$value", "$$this.userRating"] }
                                                          }
                                                      },
                                                      { // expression returns ratings count
                                                          "$cond": [
                                                              { "$ne": [{ "$size": "$$docUserRate" }, 0] },
                                                              { "$size": "$$docUserRate" },
                                                              1
                                                          ]
                                                      }
                                                  ]
                                              }
                                          },
  
                                      //////////////////////
                                          },
                                          false
                                      ],
                                  },//"in": {
                              },//"$map": {
                          },//"DoctorUserRating": {          
                      },
                      false
                  ],
              },//"in": {
          },//"$map": {
      },//"doctor": 
  </code></pre>
  
  <p>My Model is</p>
  
  <pre><code>hospitalName: { type: String, required: true, trim: true },
  hospitalID: { type: Number, required: true, unique: true, dropDups: true }, 
  serviceActiveFlag: { type: String, required: true, enum: ['Y', 'N'] },//new    
  Treatment: [{        
      procedureid: { type: Number },
  
      costUpperBound: { type: Number, required: true },                
  
      doctor: [{
          doctorId: { type: Number},
          doctorName: { type: String, required: true, trim: true },                    
          DoctorUserRating:[ {  
              userRating:{type: Number, required: true, //To get default rating for cost api
                  min: [1, 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).'],
                  max: [5, 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).']                                       
              },
              userId: { type: String, required: false, trim: true},
          }],
     }],
  }],
  updated_at: { type: D
  </code></pre>
  
  <p>ate, required: true, default: Date.now }</p>