MongoEngine / MongoEngine/mongoengine

Feature-request: Document.save(on_conflict="replace")

Open
#2,371 0 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.