且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

猫鼬:按ID排序

更新时间:2023-11-26 10:41:22

第二个参数用于选择字段.您需要在第三个参数中添加选项:

The second parameter is for the fields to select. You need to add options to the third parameter:

posts.find({'creator.user_id': req.params.user_id}, null, {sort: {'_id': -1}}, function(err,userpost) {
    if(err)
        res.send(err);
    res.json(userpost);
});

或者,您可以使用sort()函数:

Alternatively you can use the sort() function:

posts.find({'creator.user_id': req.params.user_id}).sort({'_id': -1}).exec(function(err,userpost) {
    if(err)
        res.send(err);
    res.json(userpost);
});

您可以在文档中找到更多示例.

You can find more examples in the documentation.