为什么Flask-PyMongo bulk_writes方法不存在?

时间:2018-07-01 23:04:14

标签: flask pymongo flask-pymongo

我有一些这样的代码段:

from flask import Flask
from flask_pymongo import PyMongo
from pymongo import InsertOne, UpdateOne, DeleteOne, ReplaceOne
from flask import jsonify

app = Flask(__name__)
app.config['MONGO_DBNAME'] = 'MyDB'

mongo = PyMongo(app)
coll = mongo.db['collection1']

requests = []
for d in data_to_delete:
    requests.append(DeleteOne({'key1': d}))

result = coll.bulk_writes(requests)

return jsonify(result=result.deleted_count)

当我尝试执行它时,出现错误消息:

TypeError: 'Collection' object is not callable. If you meant to call the 'bulk_writes' method on a 'Collection' object it is failing because no such method exists.

PyMongo documentation表示Collection对象具有此方法,尽管我知道Flask-PymongoPyMongo的包装。 可能是什么问题,如何将bulk_writes()Flask-Pymongo集合一起使用?

谢谢您的帮助!

1 个答案:

答案 0 :(得分:1)

就像您输入了错误一样,在方法名称上添加了“ s”。尝试bulk_write不使用“ s”。

Flask-PyMongo与PyMongo本身和MongoDB Shell一样,将集合中的未知属性名称视为点分集合名称,因此它认为您正在尝试调用名为collection1.bulk_writes的集合。