MongoEngine / MongoEngine/mongoengine
Feature-request: Document.save(on_conflict="replace")
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
I have a feature-request to optionally replace the document on DuplicateKeyError. Those who are familiar with postgres, might know the ON CONFLICT option in insert-statements. This would be similar.
The part to extend is this one in document.py:
except pymongo.errors.DuplicateKeyError as err:
message = "Tried to save duplicate unique keys (%s)"
raise NotUniqueError(message % err)
So if on_conflict="replace" then the document in db would be deleted and the new one inserted.
Further on_conflict strategies could be:
- "update": get document from db, update it with new document
- "ignore" : do not throw an exception
- None: raise exception as it already does.
with default on_conflict=None it should be backwards-compatible
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in document.py at the DuplicateKeyError handling in the save path and inspect how documents are inserted. Confirm which conflict strategy is in scope, with replace explicitly requested and update, ignore, or None proposed; done means the selected behavior works while the default None remains backward-compatible.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- database
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100