LittleMygler
LittleMygler

Reputation: 632

query mongoose to find all by a name and then picking the last created

What I'm trying to accomplish is to first sort out all documents not containing the correct string, and then picking the document that was last created by those.

My query now looks like this :

 projectModel.findOne().sort({owner: req.body.owner, date: -1}).exec(function(err, doc) {

but it gives me errors obviously. How to do this correctly?

I'm very grateful for every answer!

Upvotes: 1

Views: 431

Answers (2)

Shubham Patwa
Shubham Patwa

Reputation: 334

Just try this You are trying to find owner in sort which will not work because sort function will take only one argument if you are using date as sort parameter. So you can do one thing just put the owner parameter in findone function and then everything will work fine as required.

projectModel.findOne({owner: req.body.owner}).sort({date: -1}).exec(function(err, doc) { // your codes here }

Upvotes: 4

umutyerebakmaz
umutyerebakmaz

Reputation: 1037

please try add this pamater _id:-1 and limit:1 in your sort chain

projectModel.findOne().sort({owner: req.body.owner, _id: -1}).limit(1)

Upvotes: 0

Related Questions