Skip to main content
 首页 » 编程设计

python之Mongoengine 弃用警告 : insert is deprecated

2024年12月31日12me-sa

我在我的项目中使用 mongoengine 进行 mongodb 操作。我以前没有收到这样的警告。

DeprecationWarning: insert is deprecated. Use insert_one or insert_many instead 

我收到警告是因为下面的行

 class Human(Document): 
     name = StringField() 
 
 Human.objects.insert([ 
      Human(name='Steve'), 
      Human(name='Jack'), 
      Human(name='Chris') 
 ]) 

据我了解 Human.objects.insert() 在新版本中已弃用 但我找不到 insert_many() 而不是它。我的程序仍在运行,但我无法解决此错误。 感谢您的帮助。

请您参考如下方法:

insert_many 是在 mongoengine 版本 0.15.3 中引入的。您可以尝试更新您的 mongoengine。 如果您不想这样做,请尝试:

collection = Human._get_collection() 
collection.insert_many([{"name":"John"}, {"name":"Cookie Monster"}])