Фильтрация / Проекция mongodb

#mongodb

#mongodb

Вопрос:

Данные монго выглядят так. Конечная цель состоит в том, чтобы фильтровать и проектировать данные, имеющие только баллы.GroupScore и в массиве, если только балл равен === 8. (Это, очевидно, просто пример. У меня огромный набор данных.)

 _id:525252522 scores: {  groupScore: [  { name:"Norm", score: 7 },  { name:"Mandy", score: 8 },  { name:"Andy", score: 9 },  { name:"Chris",score: 10 },  ]   } _id:98989898 scores: {  groupScore: [  { name:"tess", score: 7 },  { name:"less", score: 8 },  { name:"kess", score: 9 },  { name:"yess",score: 10 },  ]   }  

Ожидаемые результаты

 _id:525252522 scores: {  groupScore: [  { name:"Mandy", score: 8 }  ]   } _id:98989898 scores: {  groupScore: [  { name:"less", score: 8 }  ]   }  

Ответ №1:

Попробуй вот это:

 db.collection.aggregate([  {  $set: {  "scores.groupScore": {  $filter: {  input: "$scores.groupScore",  cond: { $eq: ["$this.score", 8 ] }  }  }  }  } ])