Auto incrementing value

I want to auto increment the value of one field in a collection while inserting a document. I have got this link which specifies two ways but does someone have any other efficient way to auto increment the value?

Hi @jignesh_vyas

If I understand correctly, you are looking to update a field value by 1 while you are trying to insert a document.
As mentioned in the documentation, you can make use of the upsert() operation. However, please note that linked document is no longer supported as this is Version 2.2.

If your database is on Atlas, you can also make use of the MongoDB triggers to do so.
The article on MongoDB Auto-increment could be a good starting point for your read.

However, if this does not help, could you help me with some details on the above requirement?
Below are the information which would be useful to help you with a potentially solution.

  1. A few sample documents from the collection.
  2. The expectation from the query being used.
  3. The MongoDB version you are using.

This would help the community with more appropriate and potential solutions.

Regards
Aasawari

1 Like