带有 DataFrames 的 Mongoengine 文档
Mongoengine documents with DataFrames
假设我有以下 mongo 模型:
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()
我有以下数据框:
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"] })
我知道我可以使用 :
轻松地将这些数据帧上传到 mongo 集合
db.collection.insert_many(df_name.to_dict("records"))
但如果我这样做,那么为什么我要定义那些 类 用户、办公室、部门?我不知道我是否会得到非空答案
User.objects(); Office.objects(), Department.objects()
我知道我能做到
record = User(name = "Goku", characteristic = 0)
record.save # 一些命令上传到集合
所以,我的问题是,如何使用我定义的 类 上传这些数据帧?
某种
df_name.apply(lambda x: Respectively_Class(**x)).insert(db.collection_name)
所以当我调用 Respectively_Class.objects 时我得到了一个非空的结果?
例如,您可以这样做:
array = df_office.to_dict()
instances = [Office(**data) for data in array]
Office.objects.insert(instances, load_bulk = False)
假设我有以下 mongo 模型:
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()
我有以下数据框:
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"] })
我知道我可以使用 :
轻松地将这些数据帧上传到 mongo 集合db.collection.insert_many(df_name.to_dict("records"))
但如果我这样做,那么为什么我要定义那些 类 用户、办公室、部门?我不知道我是否会得到非空答案
User.objects(); Office.objects(), Department.objects()
我知道我能做到
record = User(name = "Goku", characteristic = 0) record.save # 一些命令上传到集合
所以,我的问题是,如何使用我定义的 类 上传这些数据帧? 某种
df_name.apply(lambda x: Respectively_Class(**x)).insert(db.collection_name)
所以当我调用 Respectively_Class.objects 时我得到了一个非空的结果?
例如,您可以这样做:
array = df_office.to_dict()
instances = [Office(**data) for data in array]
Office.objects.insert(instances, load_bulk = False)