MongoEngine / MongoEngine/mongoengine

How to use Embedded Document in bulk insert

Open
#2,456 1 comment 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'm using this model

class Schedule(EmbeddedDocument):
    scheduleId: StringField(required=True)
    detail: StringField()
        
class AccountBalance(Document):
    memberID = StringField(required=True)
    schedule = ListField(EmbeddedDocumentField(Schedule))
 
 array = []
for index, row in df.iterrows():

    # handle schedule
    schedule = Schedule()
    schedule.scheduleId = "1234667777"
    schedule.detail = "some random detail"

    payload = {
        "memberID": "98765",
        "schedule": schedule,
    }

    array.append(payload)
    
 person_instances = [TrialBalance(**data) for data in array]
 AccountBalance.objects.insert(person_instances, load_bulk=False)

If I comment out code for schedule class (Embedded document), the code works like charm but when I try to run this code, it throws an error
The source SON object needs to be of type 'dict' but a '<class 'str'>' was found

Can someone tell me how to use the embedded document in bulk insert or how to fix this error??

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

The issue provides no repository file or test to start from. Reproduce the shown AccountBalance.objects.insert(...) path with the Schedule and payload examples, then use the embedded-document and bulk-insert behavior as the completion check.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.