在MongoDB中进行分组,并获取每个组的最新日期的所有匹配文档

时间:2019-02-18 06:41:51

标签: mongodb

以下是我数据库中的数据:

{ "School": "A",
  "Class": "A",
  "Student": "XXX",
  "Date": ISODate("2018-01-31T00:00:00.000Z")
},
{ "School": "A",
  "Class": "B",
  "Student": "YYY",
  "Date": ISODate("2018-01-10T00:00:00.000Z")
},
{ "School": "A",
  "Class": "C",
  "Student": "ZZZ",
  "Date": ISODate("2018-01-31T00:00:00.000Z")
},
{ "School": "B",
  "Class": "A",
  "Student": "ABC",
  "Date": ISODate("2019-01-31T00:00:00.000Z")
},
{ "School": "B",
  "Class": "B",
  "Student": "DEF",
  "Date": ISODate("2019-01-31T00:00:00.000Z")
},
{ "School": "B",
  "Class": "C",
  "Student": "GHI",
  "Date": ISODate("2019-02-03T00:00:00.000Z")
}

我的目标是获取每个“学校”组中的所有文档,并在每个“学校”组中使用最新的“日期”,而不是整个数据库中的最新“日期”。

预期结果:

{ "School": "A",
  "Class": "A",
  "Student": "XXX",
  "Date": ISODate("2018-01-31T00:00:00.000Z")
},
{ "School": "A",
  "Class": "C",
  "Student": "ZZZ",
  "Date": ISODate("2018-01-31T00:00:00.000Z")
},
{ "School": "B",
  "Class": "C",
  "Student": "GHI",
  "Date": ISODate("2019-02-03T00:00:00.000Z")
}

我尝试使用

db.myDB.aggregate([
  { "$sort": {
               "School":1, 
               "Date":1
             }
  },
  { "$group": {"_id": {School:"$School"},
                    "fullDocument": {
                         $push: 
                           {School: "$School", Class: "$Class", Date: $Date"}
                    }, 
                    "LatestDate": {"$max": "$Date"}
              }
  }
])

我在“ fullDocument”中得到的内容仍然包括所有未带有最新“日期”的文档

1 个答案:

答案 0 :(得分:0)

首先,您需要根据School进行分组,并使用$ ROOT以数组的形式获取特定学校的整个文档

然后,您只需要使用过滤器就可以将具有Date作为该数组中最大日期的文档获取

db.collection.aggregate([
  {
    $group: {
      _id: "$School",
      data: {
        "$push": "$$ROOT"
      }
    }
  },
  {
    $project: {
      data: {
        $filter: {
          input: "$data",
          as: "item",
          cond: {
            $eq: [
              "$$item.Date",
              {
                $max: "$data.Date"
              }
            ]
          }
        }
      }
    }
  }
])