javascript - MongoDB - How to atomically increase a number and get the new value - Stack Overflow

Basically I need to increase an counter in an document and get the new value, but this has to work atom

Basically I need to increase an counter in an document and get the new value, but this has to work atomically.

Current I'm using the mand:

.updateOne({_id: ObjectId('5ed7f23789bcd51e9c6a82e0')}, {$inc: {nextTicket: 1}})

But I can't find how to immediately get the new incremented value.

Basically I need to increase an counter in an document and get the new value, but this has to work atomically.

Current I'm using the mand:

.updateOne({_id: ObjectId('5ed7f23789bcd51e9c6a82e0')}, {$inc: {nextTicket: 1}})

But I can't find how to immediately get the new incremented value.

Share Improve this question edited Apr 26, 2024 at 9:05 Jonas 129k103 gold badges328 silver badges405 bronze badges asked Aug 11, 2020 at 12:19 Ivo FritschIvo Fritsch 1351 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

updateOne does not return the document / the documents fields.

What you want to do is use findOneAndUpdate. now this still returns the "old" document so you want to specify returnOriginal: false

.findOneAndUpdate({_id: ObjectId('5ed7f23789bcd51e9c6a82e0')}, {$inc: {nextTicket: 1}}, {returnOriginal: false})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744990220a4604857.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信