使用mongoengine将多文档插入到mongodb中 [英] multi document insert using mongoengine into mongodb

查看:858
本文介绍了使用mongoengine将多文档插入到mongodb中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的烧瓶应用程序中,我正在使用MongoeEgine。我试图插入多个文件到我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆