mongoose findOne with sorting

前端 未结 1 394
你的背包
你的背包 2020-12-19 00:10

I have a problem with a mongo request:

models.user.findOne(
    {},
    {
        sort:{
            date_register:-1
        }
    },
    function(err, resu         


        
相关标签:
1条回答
  • 2020-12-19 00:33

    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)
    

    What you intend as options (the sort), mongoose is handling as fields (which fields to load).

    You might try explicitly passing null for fields:

    models.user.findOne({}, null, {sort: {date_register: -1 }}, callback);
    

    But if you can, you should probably use the query API, which is clearer, like:

    models.user.findOne({}).sort({date_register: -1}).exec(callback);
    
    0 讨论(0)
提交回复
热议问题