MongoEngine / MongoEngine/mongoengine

Exception on bulk insert with EmbeddedDocument as primary key

Open
#2,652 5 comments 1 reaction 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 get an exception when trying to do a bulk insert on a collection where I have defined the primary key as an embedded document.

Repro:

from mongoengine import EmbeddedDocument, StringField, Document, EmbeddedDocumentField, connect

class Key(EmbeddedDocument):
    property1 = StringField()
    property2 = StringField()

class MainDoc(Document):
    key = EmbeddedDocumentField(Key, primary_key=True)

def main():
    connect(host="mongodb://root:rootpassword@localhost:27017/test_db?authSource=admin&authMechanism=SCRAM-SHA-1&tls=false")

    docs = [MainDoc(key=Key(property1='aaa', property2='bbb')),
            MainDoc(key=Key(property1='ccc', property2='ddd')),
            MainDoc(key=Key(property1='eee', property2='fff'))]

    MainDoc.objects.insert(docs)

main()

This results in the following output:

Traceback (most recent call last):
  File "C:\Users\danielber\AppData\Roaming\JetBrains\PyCharm2022.1\scratches\scratch_6.py", line 26, in <module>
    main()
  File "C:\Users\danielber\AppData\Roaming\JetBrains\PyCharm2022.1\scratches\scratch_6.py", line 23, in main
    MainDoc.objects.insert(docs)
  File "C:\Users\danielber\venvs\styrax\lib\site-packages\mongoengine\queryset\base.py", line 385, in insert
    documents = self.in_bulk(ids)
  File "C:\Users\danielber\venvs\styrax\lib\site-packages\mongoengine\queryset\base.py", line 748, in in_bulk
    doc_map[doc["_id"]] = self._document._from_son(
TypeError: unhashable type: 'dict'

Note that the data does get inserted to the collection, it looks like it's failing in verification code after the insert.

I encountered this using Python 3.9.9, mongoengine 0.24.1.

Thanks!

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 with the bulk insert path in mongoengine/queryset/base.py, especially insert and in_bulk, using the provided MainDoc.objects.insert reproduction. Check the post-insert verification that indexes documents by _id; done means bulk insertion completes without the unhashable-dict exception and the inserted documents remain usable.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.