我正在使用mongoose,我想在收到数据时发出一个排序:
Model
.findOne({_user: req.user._id, name: req.params.name})
.sort({'music.vote': -1})
数据看起来像这样,我想通过投票DESC来订购音乐阵列
{
"__v": 2,
"_id": "5492bfaf59c17bdead8363c6",
"_owner": "5491e02a9f5418da92bfb4e6",
"createdAt": "2014-12-18T11:51:11.961Z",
"updatedAt": "2014-12-18T11:51:30.965Z",
"name": "list-one",
"music": [
{
"name": "music 1",
"vote": 1,
...
},
{
"name": "music 2",
"vote": 5,
...
}
],
"_user": [
"5491e02a9f5418da92bfb4e6"
]
}
答案 0 :(得分:0)
根据您的mongoose版本,这会略有不同,但findOne的方法签名如下所示:
function findOne(conditions, fields, options, callback)
作为选项(排序),mongoose作为字段处理(要加载的字段)。
您可以尝试为字段显式传递null:
Model.findOne({}, null, {sort: {music.vote: -1 }}, callback);