2017-10-02 8 views
0

私は取得していますエラー:

「認識できない表現 『$ addFields』」私は配列からフィールドの平均値を計算しようとしています

MongoDB集約フレームワークを使用します。

以下のコードスニペットで、間違っていることを理解するのに役立つ人がいますか?

私のDBのバージョンは私のモデルは

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 

が必要で、食べ3.4.5

"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": 

されている:真、デフォルト:Date.now}

+0

$ addFieldsはトップレベルのagg関数です。 $ mapの中では使用できません。代表的な1ページまたは2ページの記事を投稿して、どのアレイで平均化しようとしているのかをテキストで具体的に記述してください。私たちはあなたに解決策を提供します。 –

+0

ありがとうございます。私は質問のモデルを更新しました。基本的には、hospitalName、costUpperBound、doctorName、およびdb.HopeのDoctorUserRatingの平均を取得するapiを作成していますので、解決策を提供するのに十分な情報が得られます –

答えて

0

を私が解決策を見つけた -

"DoctorUserRating": { 

"$map": { 
    "input": "$$doctor.DoctorUserRating", 
    "as": "docUserRate", 
    "in": { 
     "$cond": [//specify the filter for user rating 
     {}, 
     { 
     "averageDocRate": { "$avg": "$$docUserRate.userRating" } 
     }, 
} 
+0

"OK" MongoDB $フィルタ関数を見ることをお勧めします。 $ mapの使い方は、$ filterがはるかに便利な選択肢かもしれないようです。 –

関連する問題