flaskpymongoflask-pymongo

Why Flask-PyMongo bulk_writes method doesn't exist?


I have some code snippet like this:

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)

When I try to execute it, I get the error message:

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 says that Collection object has this method, though I know that Flask-Pymongo is a wrapper around PyMongo. What could be the problem, and how could I use bulk_writes() with Flask-Pymongo collections?

Thank you for your help!


Solution

  • Look like you made a typo, adding an "s" to the method name. Try bulk_write with no "s".

    Flask-PyMongo, like PyMongo itself and the MongoDB shell, treat unknown attribute names on collections as dotted collection names, so it thinks you're trying to call a collection called collection1.bulk_writes.