mongodb - Trouble updating last document from a query -
hello i'm new mongodb, trying update last document in query result having trouble doing so.
i know how last document using
db.collection.find().sort({$natural:-1}).limit(1)
but how update this? tried doing:
db.collection.update(db.collection.find().sort({$natural:-1}).limit(1))
but didn't work. , don't think:
db.collection.update(query,update,option).sort({$natural:-1}).limit(1))
would want. checked official documentation couldn't find on querying last document.
you can use findandmodify
perform update requires sorting identify document update:
db.test.findandmodify({ query: {}, sort: {$natural: -1}, update: {$set: {foo: 'bar'}} })
Comments
Post a Comment