i have a little problem, i thought how to resolve it but i don't know if it's the right way.
in my project I have several functions, all of which must use a sequential unique id every time a function is executed
for example:
person 1 runs function X using id 1
person 2 runs function Y using id 2
person 3 runs function Z using id 3
person 4 runs function Y using id 4
person 1 runs function Z using id 5
....
how to do that?
I thought to save that id in a table inside mongoDB and every time it will used inside a function, the function will increment it, but i don't think it is the right way, it is?
then, if it is the right way, how to co-ordinate the access to the DB? like if 2 people runs 2 functions in the same time, the will take the same ID from DB and this absolutely must not happen, every time a function is called a different id must be used, how can I do?
You can use findOneAndUpdate
Example:
db.collection.findOneAndUpdate(
{ "name" : "function_trigger_count" },
{ $inc: { "count: 1 } }
)
FindOneAndUpdate ensure the atomicity of the query
OR
If you are using MongoDB version 5.x you can use Transaction feature to ensure the atomicity