multi document insert using mongoengine into mongodb

后端 未结 2 1283
深忆病人
深忆病人 2021-01-07 18:31

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

         


        
2条回答
  •  温柔的废话
    2021-01-07 19:12

    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]}))
    a.append(places(**{"name": 'test', "loc": [-88,101]}))
    x = places.objects.insert(a)
    

提交回复
热议问题