3

我正在尝试通过以下方式在和尚 api 中使用限制和排序:

db.user.find({ changesId : '1234' }, { limit :4 , sort : { name : 1 } }, function (err,res) {

});

在这种情况下,排序似乎不起作用。我究竟做错了什么?

4

2 回答 2

14

您的代码不正确 -}丢失。正确的:

db.user.find({ changesId : '1234' },
    { limit : 4, sort : { name : 1 } }, // <-  here
    function (err,res) { });
于 2013-11-13T13:35:20.790 回答
-1

collection.find 必须尽可能简单,我更喜欢可读性和更简洁的方式。看一看

        query={limit : 4, sort : "'name'  'asc'"} ;

            collection.find(query,
        function(error,sortedVals){
            if(error)
                response.send(error)
            else{
                response.send(sortedVals);
            }
        });

希望能帮助到你。

于 2017-02-01T11:43:32.453 回答