2016-08-21 119 views
0

鑑於集合中的以下項目,如何獲得所有記錄的id == 1的assessment.answer字段的平均值?此外,我需要返回subs []的第一個索引與每個記錄像subs [0]Mongo DB在集合的子字段上獲得平均值

{ 
    "_id" : ObjectId("57b5e55e8426706357a90728"), 
    "updatedAt" : ISODate("2016-08-20T00:21:16.497Z"), 
    "createdAt" : ISODate("2016-08-18T16:42:06.173Z"), 
    "password" : "password", 
    "email" : "[email protected]", 
    "lastName" : "User", 
    "firstName" : "Test", 
    "assessment" : [ 
     { 
      "subs" : [ 
       100, 
       100, 
       100, 
       100, 
       100 
      ], 
      "answer" : 2, 
      "id" : 1 
     }, 
     { 
      "subs" : [ 
       80, 
       40, 
       100, 
       60, 
       20 
      ], 
      "answer" : 2, 
      "id" : 2 
     }, 
     { 
      "subs" : [ 
       80, 
       20, 
       20, 
       20, 
       20 
      ], 
      "answer" : 4, 
      "id" : 3 
     }, 
     { 
      "subs" : [ 
       80, 
       100, 
       60, 
       100, 
       100 
      ], 
      "answer" : 5, 
      "id" : 4 
     }, 
     { 
      "subs" : [ 
       60, 
       100, 
       100, 
       100, 
       100 
      ], 
      "answer" : 1, 
      "id" : 5 
     }, 
     { 
      "subs" : [ 
       100, 
       100, 
       100, 
       100, 
       100 
      ], 
      "answer" : 3, 
      "id" : 6 
     }, 
     { 
      "subs" : [ 
       80, 
       20, 
       100, 
       80, 
       60 
      ], 
      "answer" : 3, 
      "id" : 7 
     }, 
     { 
      "subs" : [ 
       20, 
       20, 
       20, 
       20, 
       20 
      ], 
      "answer" : 4, 
      "id" : 8 
     }, 
     { 
      "subs" : [ 
       60, 
       60, 
       60, 
       60, 
       40 
      ], 
      "answer" : 5, 
      "id" : 9 
     }, 
     { 
      "subs" : [ 
       60, 
       60, 
       60, 
       60, 
       40 
      ], 
      "answer" : 3, 
      "id" : 10 
     }, 
     { 
      "subs" : [ 
       100, 
       100, 
       100, 
       100, 
       100 
      ], 
      "answer" : 4, 
      "id" : 11 
     }, 
     { 
      "subs" : [ 
       80, 
       80, 
       80, 
       80, 
       80 
      ], 
      "answer" : 1, 
      "id" : 12 
     }, 
     { 
      "subs" : [ 
       60, 
       60, 
       60, 
       60, 
       60 
      ], 
      "answer" : 2, 
      "id" : 13 
     }, 
     { 
      "subs" : [ 
       40, 
       40, 
       40, 
       40, 
       40 
      ], 
      "answer" : 3, 
      "id" : 14 
     }, 
     { 
      "subs" : [ 
       80, 
       20, 
       20, 
       20, 
       100 
      ], 
      "answer" : 3, 
      "id" : 15 
     } 
    ], 
    "__v" : 51 
} 
+0

什麼是你的MongoDB服務器的版本? – styvane

+0

該版本是3.0.7 –

+0

請看更新的問題... –

回答

0

你幾乎在那裏。您需要選擇只有$match$in

var ids = [ 1, 2, 3, 4, 5 ]; 
var matchStage = { "$match": { "assessment.id": { "$in": ids } } }; 

db.accounts.aggregate(
    [ 
     matchStage, 
     { "$unwind": "$assessment" }, 
     matchStage, 
     { "$group": { 
      "_id": null, 
      "avg": { "$avg": "$assessment.answer" } 
     }} 
    ] 
) 

從MongoDB的3.2啓動符合您的條件的文件,你可以做這樣的:

db.accounts.aggregate(
    [ 
     matchStage, 
     { "$project": { 
      "preAvg": { 
       "$avg": { 
        "$map": { 
         "input": { 
          "$filter": { 
           "input": "$assessment", 
           "as": "el", 
           "cond": { "$setIsSubset": [ [ "$$el.id" ], ids ] } 
          } 
         }, 
         "as": "val", 
         "in": "$$val.answer" 
        } 
       } 
      } 
     }}, 
     { "$group": { 
      "_id": null, 
      "avg": { "$sum": "$preAvg" } 
     }} 
    ] 
) 
+0

例如,我希望看到所有評估爲1的地方。然後,對於每個記錄,選擇subs數組中每個索引的平均值。所以如果2個人有答案id = 1,那麼顯示和平均subs [0]爲這些用戶。如果每個分別選擇1和4,我應該看到2 –