我正在努力用MongoEngine进行批量插入。我可以很容易地用循环写一个.save()来写入数据库,但是我找不到从MongoEngine文档中进行批量插入的过程。唯一提到的是SO:multi document insert using mongoengine into mongodb。我尝试这样做但我只是插入所有文档而不在Schema中验证它们。
这是我的代码:
import mongoengine as me
# connect to MongoDB
me.connect(host='CONNECTION_STRING')
# venue Schema
class newVenues(me.Document):
name = me.StringField(required=True)
latitude = me.DecimalField(required=True)
longitude = me.DecimalField(required=True)
# some test data
venues = [{'name': 'Vega',
'latitude': 55.672867,
'longitude': 12.473692},
{'name': 'Tivoli',
'latitude': 55.681256,
'longitude': 12.553412}]
# the list of venues to bulk insert to MongoDB
venues_to_insert_list = []
for venue in venues:
venues_to_insert_list.append(
newVenues(
name=venue.get('name'),
latitude=venue.get('latitude'),
longitude=venue.get('longitude')
)
)
# bulk insert to MongoDB
newVenues.objects.insert(venues_to_insert_list)
答案 0 :(得分:0)
如果要插入所有文档而不在架构中对其进行验证。 您需要在mongoengine中创建动态文档
下面的代码用于读取csv文件,并在不验证模式的情况下将其插入mongodb的集合中(模式也是以动态方式生成的。)
import mongoengine as me
class DynamicDoc(me.DynamicDocument):
any_field = me.StringField()
import pandas as pd
all_csv_records = data_frame.to_dict('records')
data_frame = pd.read_csv(file_path)
for data in all_csv_records:
report_data = DynamicDoc()
DynamicDoc.any_field = str('temp_data')
for col, row in data.iteritems():
report_data[col] = row
report_data.save()
注意-mongo模型中只有'any_field'是字符串形式的必需输入输入。因为字段在mongo模型中手动定义。
答案 1 :(得分:0)
import mongoengine as me
# connect to MongoDB
me.connect(host='CONNECTION_STRING')
# venue Schema
class newVenues(me.Document):
name = me.StringField(required=True)
latitude = me.DecimalField(required=True)
longitude = me.DecimalField(required=True)
# venue Schema
class newVenues(Document):
name = StringField(required=True)
latitude = DecimalField(required=True)
longitude = DecimalField(required=True)
# some test data
venues = [
{"name": "Vega", "latitude": 55.672867, "longitude": 12.473692},
{"name": "Tivoli", "latitude": 55.681256, "longitude": 12.553412},
]
# the list of venues to bulk insert to MongoDB
venues_to_insert_list = [newVenues(**data) for data in venues]
# bulk insert to MongoDB
newVenues.objects.insert(venues_to_insert_list)