Mongoengine documents with DataFrames

Viewed 197

suppose I have the following mongo model:

class User(Document):
    name = StringField(required=True,unique=True)
    characteristics = DictField()
class Office(Document):
    user = ReferenceField(User)
    office= StringField(required=True,unique=True)
    salary = IntField()
class Department(Document):
    offices = ReferenceField(Office)
    city = StringField(required=True,unique=True)
    country = StringField()

and I have the following dataframes:

df_users = pd.DataFrame({"name":["Goku","Gohan","Piccolo"], 
                         "characteristics":[{"a":1},{"b":2},{"c":3}]})
df_office = pd.DataFrame({"user":["Goku","Gohan","Piccolo"],
                          "office":["Earth","Pao","Namek"],
                          "salary":[1,2,3]})
df_department = pd.DataFrame({"offices":["Earth","Pao","Namek"],
                              "city":["South City","North City","nameki"],
                              "country":["A","B","C"] })

F

I know that I can upload those dataframes easily to a mongo collection with :

db.collection.insert_many(df_name.to_dict("records"))

But if I do that, then Why I defined those classes User, Office, Department? I don't know if I would get and non empty answer if I do

User.objects(); Office.objects(), Department.objects()

I know that I can do

record = User(name = "Goku", characteristic = 0) record.save # some command to upload to collection

So, my question is, How can I upload those dataframes using the classes I defined? some kind of

df_name.apply(lambda x: Respectively_Class(**x)).insert(db.collection_name)

So when I call Respectively_Class.objects I got a non empty result?

1 Answers

For example, you can do this:

array = df_office.to_dict()
instances = [Office(**data) for data in array]
Office.objects.insert(instances, load_bulk = False)
Related