我想从学生集合中按姓名排序获取 100 条记录。现在我只想获取这 100 条记录中分数大于 x 的学生。任何人都可以帮忙解决这个问题吗?当我在查询中给出 Max(marks) 时,它会在排序和跳过之前执行。
这是我尝试过的查询
var query = {};
query["marks"] = {$gt:35};
db.collection("student").find(query).skip(0).limit(100).sort("name")
最佳答案
当您的查询按照本例中的步骤进行描述时,聚合
能够将结果从一个操作的输出传送到下一个操作的输入,这使其成为一种自然的选择:
db.student.aggregate([
// First sort all the docs by name
{$sort: {name: 1}},
// Take the first 100 of those
{$limit: 100},
// Of those, take only ones where marks > 35
{$match: {marks: {$gt: 35}}}
])
关于mongodb - 查询 mongodb 中的前 N 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20727257/