在我的烧瓶应用程序中,我使用的是MongoeEgine。我正尝试将多个文档插入到我的MongoDB中的位置集合中。多文档插入使用mongoengine进入mongodb
我的文档类定义为
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)
最后一条语句失败
x= places(a)
TypeError: __init__() takes exactly 1 argument (2 given)
我也试过这个保存到我的实例
places.insert(x)
places.save(x)
都失败。请帮忙。
谢谢。我怎么能用这个做一个upsert。我想插入这些行,如果他们是新的,否则更新现有的行。有upsert = True标志的Places.objects.upsert或Places.object.insert吗? – user1340513 2013-03-03 08:47:17
@Ross是否有一些简单的方法来避免* NotUniqueError *?我在某处阅读PyMongo批量插入中使用* ordered *的地方,但我更愿意使用Mongoengine。我知道_getCollection(),但我仍然宁愿避免不直接访问集合。我更喜欢使用Mongoengine语法:) – giliev 2016-11-01 23:48:43
@Ross上述哪一个操作是高效的,我的意思是它是save()操作还是批量插入操作。 – hkhr 2017-08-16 04:27:53