![](/img/trans.png)
[英]How can we find random document matching given query and update it atomically in mongoDB?
[英]How to find document with a max field value and update it in one go (atomically)?
我們可以使用這樣的一行來查找具有最大值的文檔
db.collection.find().sort({ _id: -1 }).limit(1)
但是不可能鏈接更新操作並找到->更新意味着我想避免的兩個服務器調用。
是否有另一種方法可以在操作中查找和更新項目?
試試這個。
db.collection.update({},
{
"$set": {
key: 3
}
},
{
sort: {
_id: -1
},
upsert: true
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.