MongoEngine / MongoEngine/mongoengine

EmbeddedDocumentListField cannot have a primary/unique field?

Open
#2,222 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

class RatingData(EmbeddedDocument):
    id = StringField(unique=True)
    rating = IntField()


class Subject(Document):
    id = IntField(primary_key=True)
    rating = EmbeddedDocumentListField(RatingData)
if not Subject.objects(id=par_subject_id).first() or not Subject.objects(
        id=par_subject_id).first().rating:
    Subject.objects(id=par_subject_id).upsert_one(rating=[rating_data])
else:
    Subject.objects(id=par_subject_id).update_one(push__rating=rating_data)

But there is duplicates in Mongo.

图片
图片

There is the index
图片

Mongoengine 0.18.2
Python 3.8.0

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

Reproduce the Subject and RatingData example on MongoEngine 0.18.2 with MongoDB, starting from EmbeddedDocumentListField and the unique=True and primary_key declarations. Compare the behavior of upsert_one and push__rating with the displayed index. Done means establishing whether uniqueness is expected for embedded list fields and recording the confirmed behavior or required change with a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.