如何在猫鼬中排序? [英] How to sort in mongoose?

查看:28
本文介绍了如何在猫鼬中排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有找到有关排序修饰符的文档.唯一的见解是在单元测试中:spec.lib.query.js#L12

I find no doc for the sort modifier. The only insight is in the unit tests: spec.lib.query.js#L12

writer.limit(5).sort(['test', 1]).group('name')

但它对我不起作用:

Post.find().sort(['updatedAt', 1]);

推荐答案

在 Mongoose 中,可以通过以下任一方式进行排序:

In Mongoose, a sort can be done in any of the following ways:

    Post.find({}).sort('test').exec(function(err, docs) { ... });
    Post.find({}).sort([['date', -1]]).exec(function(err, docs) { ... });
    Post.find({}).sort({test: 1}).exec(function(err, docs) { ... });
    Post.find({}, null, {sort: {date: 1}}, function(err, docs) { ... });

这篇关于如何在猫鼬中排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆