0

我有一些这样的代码片段:

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 文档Collectionobject 有这个方法,虽然我知道这Flask-Pymongo是一个PyMongo. 可能是什么问题,我如何使用bulk_writes()集合Flask-Pymongo

谢谢您的帮助!

4

1 回答 1

1

看起来你打错了,在方法名称中添加了一个“s”。尝试bulk_write不带“s”。

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

于 2018-07-02T11:40:27.340 回答