multi document insert using mongoengine into mongodb

后端 未结 2 1251
深忆病人
深忆病人 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:06

    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])])
    

提交回复
热议问题