var query = Session
.find({ player: player, logout: null })
.sort({ 'login.date': -1 })
.limit(1);
query.exec(function(err, data) {
var session = new Session(data);
session.logout = logoutEvent;
session.save();
});
How can I convert this into findOneAndUpdate?
Here is an example of using findOneAndUpdate but im not sure how to add sort
and limit
:
var query = {'username':req.user.username};
req.newData.username = req.user.username;
MyModel.findOneAndUpdate(query, req.newData, {upsert:true}, function(err, doc){
if (err) return res.send(500, { error: err });
return res.send("succesfully saved");
});
You can add sort to you options dict:
{ upsert: true, sort: { 'login.date': -1 } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With