Until you create a separate database connection for each workflow, it is completely safe for multiple processes to access the database at the same time. Any queries that they issue that make changes to the database will be applied individually, usually in the order in which they are received by the database. In most situations, this will be safe , but :
If your processes simply insert documents into the database, each insertion usually creates a separate object.
The exception is that you explicitly specify _id for the document, and this identifier is already used in the collection. This will cause the insert to fail. (So don't do this: leave _id and MongoDB will always generate a unique value for you.)
If your processes delete documents from the database, the operation will fail if another process has already deleted the same object. (This is not just bad luck, but it just means that someone else has reached you.)
If your processes update documents in the database, everything becomes duller.
While each process is updating a different document, you are fine.
If several processes are trying to update the same document at the same time, you need to be careful. Updates that replace values on an object will be applied in order, which may result in changes made by one process being inadvertently overwritten by another. You must be careful not to specify fields that you are not going to change. Using MongoDB update statements can be useful for performing complex atomic operations, for example, for changing the numeric values of fields.
Please note that “at the same time” does not necessarily mean that the operations occur at the same time. This means more generally that there is a “overlap” during the operation of two processes with the same document, for example.
Process A Process B --------- --------- Reads object from DB ... working... Reads object from DB working... working... updates object with changes working... updates object with changes
In the above situation, it is possible that some of the changes made by process A will be inadvertently overwritten by process B.
source share