0

I don't have unique constraint on my collection nor can I add it. On insert, under certain unknown circumstances, mongodb creates duplicate records.

Any idea on how and why is this happening?

  • Surely Mongo is not creating duplicates on its own. It must be related to your reads and writes related. Can't say without understanding more about it. – Amit Phaltankar Mar 06 '18 at 10:37
  • Could it be mongoengine that is making multiple save calls? I use `save()` method of `mongoengine`, it updates record if `_id` is given, inserts otherwise. I create an object of collection type, fill its attributes and `save()` it (note that it doesn't have `_id` field). `save()` then generates `_id` and inserts document. Is there a possibility that `save()` itself is making many calls with different `_id`s? Should I use `update(upsert=True, **kwargs)` instead? – Aparna Kulkarni Mar 06 '18 at 10:40
  • It must be something related to your program which works with mongoengine. You should definitely give try to `upsert=True` – Amit Phaltankar Mar 06 '18 at 10:44
  • Thanks for response @Amit. Any idea how upsert works? Couldn't find any article that describes it clearly. Assuming two threads in my case are trying to save same document, what if in `upsert` both of them search for a particular document, and because the document isn't present in collection, both of them call `insert` part of `upsert`? I'm assuming upsert holds a lock on collection, is why it inserts once and updates second query onward. Correct me if I'm wrong. – Aparna Kulkarni Mar 06 '18 at 13:27
  • Aparna, did you have a look at https://stackoverflow.com/questions/8447502/how-to-do-insert-if-not-exist-else-update-with-mongoengine – Amit Phaltankar Mar 06 '18 at 13:34
  • 1
    I have, @AmitPhaltankar. My question was more of why duplicates with `save()`. We have decided to go ahead with `upsert` flag. Thanks for your response. – Aparna Kulkarni Mar 08 '18 at 01:58

0 Answers0