First of all, I want to welcome you to the MongoDB Community Forum! - My name is Josman and I happy to help you with your question.
The service is about incrementing a custom serial number and inserting a document into the collection. When users made the concurrent request the webhook is not working as expected and we see some lag between the generated custom serial number which are duplicated sometimes.
Webhooks linked functions do not work like Triggers in the sense that event ordering cannot be enabled to allow multiple executions to occur sequentially based on the timestamps of change events. In Webhooks, functions will run in parallel.
However, that said, if you do not want duplicate documents to be inserted into your collections, you could follow two complementary approaches:
Create a unique index in the field you don’t want to have duplicates. Moreover, you can control this in the logic of your function.
- Control this logic in the body of your function, i.e, using idempotent update operators like
$addToSet instead of
I hope you found my explanation useful. Please do not hesitate to contact me if you need any further clarification or if you would like to provide us with further details.