Mongoose request request

I have a problem with the mongo request:

models.user.findOne( {}, { sort:{ date_register:-1 } }, function(err, result){ console.log(err); } 

I have

 { [MongoError: Error: Unsupported projection option: date_register] name: 'MongoError' } 

like a mistake

I want to get my users by DESC registration_date

thanks

+7
source share
1 answer

This will change slightly depending on your mongoose version, but the method signature for findOne looks something like this:

 function findOne (conditions, fields, options, callback) 

What you mean as options (sorting), mongoose treats as fields (which fields to load).

You can try explicitly passing null for the fields:

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

But if you can, you should probably use a query API that is more understandable, for example:

 models.user.findOne({}).sort({date_register: -1}).exec(callback); 
+25
source

All Articles