通过对集合进行排序获得唯一的文档

时间:2019-06-12 03:49:19

标签: java mongodb

我有一个mongo集合“学生”,其中包含以下文件

`{
    "_id" : ObjectId("5d006ed8506fe860333c5aa2"),
    "studentId" : "123",
    "examName" : "abc",
    "marks" : 56,
    "isLatest" : 34343,
    "examStatus" : "Completed",
    "addedOn" : ISODate("2019-06-09T07:02:00.650Z")
}
{
    "_id" : ObjectId("5d006ed8506fe860333cwewew2"),
    "studentId" : "123",
    "examName" : "abc",
    "marks" : 57,
    "isLatest" : 45454,
    "examStatus" : "Completed",
    "addedOn" : ISODate("2019-06-10T07:02:00.650Z")
}
{
    "_id" : ObjectId("5d006ed8506fe860333c5aa5"),
    "studentId" : "123",
    "examName" : "def",
    "marks" : 56,
    "isLatest" : 4434,
    "examStatus" : "Completed",
    "addedOn" : ISODate("2019-06-10T07:02:00.650Z")
}
{
    "_id" : ObjectId("5d006ed8506fe860333c5aa8"),
    "studentId" : "123",
    "examName" : "def",
    "isLatest" : 1,
    "examStatus" : "Under progress",
    "addedOn" : ISODate("2019-06-11T07:02:00.650Z")
}
{
    "_id" : ObjectId("5d00708a506fe860333c5ad9"),
    "studentId" : "456",
    "examName" : "def",
    "marks" : 56,
    "isLatest" : 1,
    "examStatus" : "Completed",
    "addedOn" : ISODate("2019-06-10T07:02:00.650Z")
}`

现在,我想在examName中获取最新的唯一数据,而examStatus已完成。例如,对于examName:def,查询应返回以下文档

`{
    "_id" : ObjectId("5d00708a506fe860333c5ad9"),
    "studentId" : "456",
    "examName" : "def",
    "marks" : 56,
    "isLatest" : 1,
    "examStatus" : "Completed",
    "addedOn" : ISODate("2019-06-10T07:02:00.650Z")
}`

对于testName“ abc”,它应该返回

`{
    "_id" : ObjectId("5d006ed8506fe860333cwewew2"),
    "studentId" : "123",
    "examName" : "abc",
    "marks" : 57,
    "isLatest" : 45454,
    "examStatus" : "Completed",
    "addedOn" : ISODate("2019-06-10T07:02:00.650Z")
}`

如果考试状态不佳的文档更改为已完成,如下所示

{
    "_id" : ObjectId("5d006ed8506fe860333c5aa8"),
    "studentId" : "123",
    "examName" : "def",
    "marks" : 99,
    "isLatest" : 1,
    "examStatus" : "Completed",
    "addedOn" : ISODate("2019-06-11T07:02:00.650Z")
}

这时,对于checkName和statusStatus完成的学生输出将返回我

{
    "_id" : ObjectId("5d00708a506fe860333c5ad9"),
    "studentId" : "456",
    "examName" : "def",
    "marks" : 56,
    "isLatest" : 1,
    "examStatus" : "Completed",
    "addedOn" : ISODate("2019-06-10T07:02:00.650Z")
}
{
    "_id" : ObjectId("5d006ed8506fe860333c5aa8"),
    "studentId" : "123",
    "examName" : "def",
    "marks" : 99,
    "isLatest" : 1,
    "examStatus" : "Completed",
    "addedOn" : ISODate("2019-06-11T07:02:00.650Z")
}

到目前为止我尝试过的以下查询

db.getCollection('Student').aggregate([ { "$sort" : { "addedOn" : -1}}, { "$match" : {"examName" : "def"} } ])

1 个答案:

答案 0 :(得分:1)

尝试这种方法,

db.Student.find({examName:"def", examStatus:"Completed"}).sort({addedOn : -1}).limit(1)

否则,要获取最新值,可以按ObjectId排序。您可以执行此操作,因为ObjectId的4字节值表示自Unix时代以来的秒数,

db.Student.find({examName:"def", examStatus:"Completed"}).sort({_id : -1}).limit(1)

已更新

我认为您无法通过一个查询来执行此操作。尝试使用两个查询,

var student = db.Student.findOne({examName:"abc", examStatus:"Completed"})
db.Student.find({"addedOn" : {$gte : student.addedOn}, "examName" : {$eq : student.examName}, "examStatus" : {$eq : student.examStatus}});