简体   繁体   English

ZCCADDEDB567ABAE643E15DCF0974E503Z 查询排序,跳过和限制

[英]Mongoose query to sort, skip and limit

Here is my mongoose code which does sorting And pagination.这是我的 mongoose 代码,它进行排序和分页。 Here when i am removing sorting or adding other type of sort no result is coming.在这里,当我删除排序或添加其他类型的排序时,没有结果。 And i have 3000 documents in db我在数据库中有 3000 个文档

  res = await Question.find(filters)
  .sort({'_id': "-1"})
  .skip(args.skip ? parseInt(args.skip) : 0)
  .limit(args.limit ? parseInt(args.limit) : 100)
  .exec();

Please take a look请看一下

Replace this => sort({'_id': "-1"}) with => sort({_id: -1})将此 => sort({'_id': "-1"}) 替换为 => sort({_id: -1})

The .sort() command accept as Objects or strings as parameters. .sort()命令接受对象或字符串作为参数。

On the Object syntax the values allowed are asc , desc , ascending , descending , 1 , and -1在 Object 语法中,允许的值为ascdescascendingdescending1-1

  .sort({_id: -1})

string syntax字符串语法

  .sort('-_id').

Note: You don't need the .exec() part when you are using promises注意:使用 Promise 时不需要.exec()部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM