MongoEngine / mongoengine

A Python Object-Document-Mapper for working with MongoDB
http://mongoengine.org
MIT License
4.24k stars 1.23k forks source link

How to use Embedded Document in bulk insert #2456

Open mustafakhalidcs opened 3 years ago

mustafakhalidcs commented 3 years ago

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??

martin-milon-capgemini commented 1 month ago

up