I am performing some operation in mongoDB where in am doing bulk insert WritemanyAsync()
on multiple thread. Say, there are two entities J
and C
(Collection). On every update of document J
am fetching all C
and similarly for every update of document C
am fetching all J
and performing a INSERT
in a document say XXX
.
For example (Pseudo): Say there are (J1 .. J3)
and C1 .. C3
and thus if J1 updates then a operation happens with J1 X (C1 .. C3)
.
This entire operation happens on multiple threads. I am seeing some duplicates records in collection XXX
which has same J
and C
(I mean with same id).
Question: is there any possibility that duplicate is occurring cause the bulk insert is happening in multiple thread. Means, by the time thread T1 performing insert for record (C1,J1) another thread is also performing the same insert and thus the duplicates.
I found that, mongoDB takes a row level (document level) lock while performing insert and so there is a chance for this situation to occur but not sure.
Can anyone please suggest or shed some light in this.
This is first time working in mongoDB and so have no idea about it (my sole experience is in relational database but mongoDB concurrency doesn't work the way relational DB does).
EDIT: From some documentation, got to know that INSERT
on mongoDB acquires a write
lock/latch and also there could be only one writer lock at any point in time per DB / per collection.
With that, no matter how many parallel insert happens, they will be queued and will not perform simultaneously and thus my said scenario should never occur.
Can someone please confirm (or) let me know.