如何在MongoDb中返回从一个过滤掉的多个数组

我在MongoDb数据库中有一些数据,并且正在使用NodeJS MongoDb本机驱动程序访问它。

user: {
    _id: ****Example Id****
    name: 'Foo bar',examScores: [
        { subject: 'Geography',score: 80,teacher: 'Mr Foo',date: 'somedate'},{ subject: 'History',score: 57,teacher: 'Mrs Bar',{ subject: 'Maths',score: 43,teacher: 'Mrs Fizz',{ subject: 'Geography',teacher: 'Mr Buzz',score: 78,score: 41,date: 'somedate'}
    ]
}

我想检索每个主题的前十名得分/考试。为简洁起见,我在示例中未进行十次以上的考试。

类似的东西:

user: {
    _id: ****Example Id****
    name: 'Foo bar',grades: [
        { subject: 'Geography',exams: [ 
            { score: 80,date: 'somedate' },{ score: 78,{ score: 43,date: 'somedate' }
            ]
        },exams: [
            { score: 57,{ score: 41,date: 'somedate'}
            ]
        },exams: [
            { score: 43,date: 'somedate'}
            ]
        }
    ]
}       
taolongyang1 回答:如何在MongoDb中返回从一个过滤掉的多个数组

尝试此查询,希望对您有所帮助。我使用过$unwind,然后汇总了数据。

db.collection.aggregate([ {$unwind:"$examScores"},{$group:{"_id":"$examScores.subject",name: { $first: "$name" },exams: { 

$addToSet:{ teacher: '$examScores.teacher',score:'$examScores.score',date:'$examScores.date' } }}},{$group:{"_id":null,"grades":{$push: 

{"subject":"$_id","exams":"$exams"}}}},{$project:{"_id":0,"grades":1,"name":1}} ])

我得到的预期输出在我的本地计算机上

{
  "name": "Foo bar","grades": [
    {
      "subject": "History","exams": [
        {
          "teacher": "Mr Buzz","score": 41,"date": "somedate"
        },{
          "teacher": "Mrs Bar","score": 57,"date": "somedate"
        }
      ]
    },{
      "subject": "Maths","exams": [
        {
          "teacher": "Mrs Fizz","score": 43,{
      "subject": "Geography","exams": [
        {
          "teacher": "Mr Foo","score": 78,{
          "teacher": "Mr Buzz",{
          "teacher": "Mr Foo","score": 80,"date": "somedate"
        }
      ]
    }
  ]
}
,

请尝试以下查询:

  1. 展开examScores数组
  2. 基于主题和分数的降序排序集合,因为 您需要按照分数的降序排列每个主题的结果。
  3. 基于主题分组结果
  4. 由于只需要列出每个主题的前10个最高分 您可以使用$Slice操作将分数限制在前10 每个主题。

    db.collection.aggregate([ 
    
                   { $unwind : "$examScores" },{ $sort : 
                           { "examScores.subject" : 1,"examScores.score" : -1
                           }
                   },{ $group : { 
                              "_id" : "$examScores.subject","name" : { $first: "$name" },"examsAll" : { 
                                     $addToSet : { 
                                            teacher: '$examScores.teacher',score : '$examScores.score',date : '$examScores.date' 
                                                 } 
                                           }
                                }
                   },{ $project : { 
                                 "_id" : 1,"name" : 1,"exams" : { $slice: [ "$examsAll",10 ] } 
                                }
                   },{ $group : { "_id" : null,"name" :  { $first: "$name" },"grades" : { $push: 
                                                 { "subject" : "$_id","exams" : "$exams" 
                                                 }
                                           }
                               }
                   },{ $project : { "_id" : 0,"grades" : 1,"name" : 1
                                }
                   } 
         ]);
    
本文链接:https://www.f2er.com/3041540.html

大家都在问