Search code examples
c#integeratomicincrementmongodb

MongoDB: increment the integer field (C#)


How to increment integer field in atomic manner? Can FindAndModify method helps?

For example document contains field count and I wanna to increment it without retrieving full document and saving back.


Solution

  • I did leave a comment but I found it. The $inc modifier will increment a field internally. Completely atomically for this exact purpose.

    See here "Monog DB Atomic Operations"