猫鼬 findOne 与排序 [英] mongoose findOne with sorting
本文介绍了猫鼬 findOne 与排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 mongo 请求有问题:
I have a problem with a mongo request:
models.user.findOne(
{},
{
sort: {
date_register: -1
}
},
function(err, result) {
console.log(err);
}
我有
{ [MongoError: Error: Unsupported projection option: date_register] name: 'MongoError' }
作为错误
我想通过 date_register DESC 获取我的用户
I'd like to get my users by date_register DESC
谢谢
推荐答案
这会因您的 mongoose 版本而略有不同,但 findOne
的方法签名看起来像这样:
This will vary slightly depending on your version of mongoose, but the method signature for findOne
looks something like this:
function findOne (conditions, fields, options, callback)
您打算将什么作为 options
(排序),mongoose 将作为 fields
(要加载的字段)进行处理.
What you intend as options
(the sort), mongoose is handling as fields
(which fields to load).
您可以尝试为字段显式传递 null
:
You might try explicitly passing null
for fields:
models.user.findOne({}, null, { sort: { date_register: -1 } }, callback);
但是如果可以的话,您应该使用查询 API,它更清晰,例如:
But if you can, you should probably use the query API, which is clearer, like:
models.user.findOne({}).sort({ date_register: -1 }).exec(callback);
这篇关于猫鼬 findOne 与排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文