james gicharu
james gicharu

Reputation: 99

mongoose sort latest document entry in db and update

I want to sort the latest document matching the query then set paid property to true.

Payment.update({orderRef: paidRef} , {$set:{paid:true}}, {new: true}, function(err, doc){ });

Upvotes: 1

Views: 837

Answers (1)

Orelsanpls
Orelsanpls

Reputation: 23515

Use findOneAndUpdate() because you only want to update one.

like :

findOneAndUpdate({
 orderRef: paidRef,
}, {
  $set: { paid : true },
}, { 
  new: true,
  sort: { date: -1 },
});

with date a field where the creation date is stored

Upvotes: 1

Related Questions