1

我编写了以下程序来连接我的 mongo 数据库

from flask import Flask
from flask_mongoalchemy import MongoAlchemy
app = Flask(__name__)

app.config['MONGOALCHEMY_DATABASE']='MyDatabase'
app.config['MONGOALCHEMY_CONNECTION_STRING']='mongodb://<username>:<password>@mycluster-shard-00-00-cfkbg.mongodb.net:27017,mycluster-shard-00-01-cfkbg.mongodb.net:27017,mycluster-shard-00-02-cfkbg.mongodb.net:27017/test?ssl=true&replicaSet=MyCluster-shard-0&authSource=admin&retryWrites=true&w=majority'

db=MongoAlchemy(app)

class Example(db.Document):
    name=db.StringField()

if __name__=='__main__':
    obj=Example(name='Aman')
    obj.save()

错误:

Traceback (most recent call last):
  File "app.py", line 15, in <module>
    obj.save()
  File "/usr/local/lib/python3.6/dist-packages/flask_mongoalchemy/__init__.py", line 271, in save
    self._session.insert(self, safe=safe)
  File "/usr/local/lib/python3.6/dist-packages/mongoalchemy/session.py", line 172, in insert
    self.add(item, safe=safe)
  File "/usr/local/lib/python3.6/dist-packages/mongoalchemy/session.py", line 188, in add
    return self.flush()
  File "/usr/local/lib/python3.6/dist-packages/mongoalchemy/session.py", line 414, in flush
    result = op.execute()
  File "/usr/local/lib/python3.6/dist-packages/mongoalchemy/ops.py", line 97, in execute
    return self.collection.save(self.data, safe=self.safe)
  File "/usr/local/lib/python3.6/dist-packages/pymongo/collection.py", line 1881, in save
    write_concern = WriteConcern(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'safe'

4

1 回答 1

1

尝试安装旧版本的 pymongo(最好是 2.9.0)。根据https://github.com/MongoEngine/mongoengine/issues/1154

mongoengine 的最新版本与 pymongo 3+ 不兼容:

于 2020-05-10T06:37:37.533 回答