我有以下文件:
{
"_id"423434234,
"username":"Tom",
"comments":
[
{
"comment":"Hello",
"dateCreated":2342362234523
},
{
"comment":"Hello",
"dateCreated":2342362234523
},
{
"comment":"Hello",
"dateCreated":2342362234523
},
{
"comment":"Hello",
"dateCreated":2342362234523
},
{
"comment":"Hello",
"dateCreated":2342362234523
},
{
"comment":"Hello",
"dateCreated":2342362234523
}
]
}
使用 findOne 我想按 _id 查询文档,只返回按 dateCreated DESC 排序的前 5 条评论。我试过这个查询:
findOne({"_id":ObjectId("517b9b749451a74ce661fa74")},{"comments":{"$slice":5}},{"$sort":{"comments.dateCreated":-1}})
它返回前 5 条评论,但不应用所需的排序。