Take the 2-minute tour ×

in my flask app i am using mongoengine , i am trying to insert multiple rows(documents) into my places collection in my mongodb
My document class defined as below

class places(db.Document):

  name = db.StringField(max_length=200, required=True)    
  loc =   db.GeoPointField(required=True)
  def __unicode__(self):
    return self.name

    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)

also tried to save this to my instance

places.insert(x) places.save(x)

both fail . Please help.

share|improve this question

2 Answers 2

up vote 7 down vote accepted

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])])
share|improve this answer
Thanks. How could i use this to do an upsert . I want to insert these rows if they are new , else update the existing row. Is there a Places.objects.upsert or Places.object.insert with upsert=True flag? –  user1340513 Mar 3 '13 at 8:47

You try to initialize Document object for multiple documents at once. If you look at mongoengine's BaseDocument class, you'll see, that its __init__ method takes a dictionary of keyword arguments, which relate to fields of one single document.

If you want to do a bulk save, you have to make a list of places instances and pass it to insert() method.

a = []
a.append(places(**{"name": 'test', "loc": [-87,101]}))
x = places.objects.insert(a)
share|improve this answer

Your Answer


