使用mongoengine将多文档插入到mongodb中 [英] multi document insert using mongoengine into mongodb
问题描述
我的文档类被定义为
<$ (b.d文件):
name = db.StringField(max_length = 200,required = True)
loc = db.GeoPointField( required = True)
$ b $ def __unicode __(self):
return self.name
$ ba = []
a.append({name 'test','loc':[ - 87,101]})
a.append({name:'test',loc:[ - 88,101]})
x = places(a)
最后一条语句失败
x = places(a)
TypeError:__init __()需要1个参数(给出2个)
我也尝试将这个保存到我的实例中
$ $ $ $ $ $ $ $ places.insert(x)
places.save(x)
都失败。请帮助。
Places.objects.insert
不占用字典列表必须是 Places
实例。正常的操作是创建 Places 的单个实例,并保存或插入例如:
Places(name =test,loc = [ - 87,101])。save()
然而如果你想做一个批量插入,你可以传递一个在
对象
queryset中放置实例并调用插入
例如:
$ b $ pre $ $ $ $ $ $ $ b $ Places $ =test 2,loc = [ - 87,101])])
In my flask app I am using MongoeEgine. I am trying to insert multiple documents into my places collection in my MongoDB.
My document class is defined as
class places(db.Document):
name = db.StringField(max_length=200, required=True)
loc = db.GeoPointField(required=True)
def __unicode__(self):
return self.name
a=[]
a.append({"name" : 'test' , "loc":[-87,101]})
a.append({"name" : 'test' , "loc":[-88,101]})
x= places(a)
The last statement fails
x= places(a)
TypeError: __init__() takes exactly 1 argument (2 given)
I also tried to save this to my instance
places.insert(x)
places.save(x)
both fail. Please help.
Places.objects.insert
doesn't take a list of dictionaries it has to be Places
instances. Normal operations would be to create individual instances of Places
and save or insert eg:
Places(name="test", loc=[-87, 101]).save()
Places(name="test 2", loc=[-87, 101]).save()
However if you want to do a bulk insert you can pass a list of Places
instances and call insert
on the objects
queryset eg:
Places.objects.insert([Places(name="test", loc=[-87, 101]),
Places(name="test 2", loc=[-87, 101])])
这篇关于使用mongoengine将多文档插入到mongodb中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!