1

I am trying to auto increment an indexed field in mongodb whenever there is an insertion happens, I read many posts on SO this and mongoose-auto-increment, but I am not getting how are they working Because consider below scenario

Suppose I want to auto increment a field counter in my collection and currently the first record already exist whose counter value is 1, now suppose there are three concurrent inserts happens in the database now as counter value is 1 so all of them must be trying to set counter 2. But as we know know among these three whoever will get the first lock will successfully set its counter as 2, but what about other two operations because now when they will acquire lock they will also try to set counter value as 2 but as 2 is already taken so I guess mongoose will give error duplicate key error.

Can anyone please tell me how does above two posts solves the concurreny problem for auto-incrementing an indexed field in mongodb.

I know I am missing some conecpt but what ??

Thanks.

Sudhanshu Gaur
  • 7,061
  • 9
  • 41
  • 85

0 Answers0