【发布时间】:2018-03-20 20:20:36
【问题描述】:
我在 mlab 上托管一个 mongo 数据库,并且我有一个非常简单的程序可以将文档插入到现有集合中。由于某种原因,插入在 JS 中对我有用,但在 Python 中却不行。这就是它在 Python 中的样子:
from pymongo import MongoClient
client = MongoClient('mongodb://<user>:<password>@<address>/<db-name>')
db = client.congresspersons
posts = db.posts
post_data = {
'title': 'Python and MongoDB',
'content': 'PyMongo is fun, you guys',
'author': 'Scott'
}
result = posts.insert_one(post_data)
这段代码大多来自here。
但是,我不断收到此错误:
pymongo.errors.OperationFailure: not authorized on config to execute command { insert: "congresspersons.posts", ordered: true, documents: [ { content: "PyMongo is fun, you guys", _id: ObjectId('5ab16ae3626b6217f7c2a079'), author: "Scott", title: "Python and MongoDB" } ] }
这些是用户的权限:
{
...
"roles": [
{
"role": "dbOwner",
...
}
]
}
我不明白为什么这种简单的插入在 Python 中不起作用。我怎样才能让它工作?
【问题讨论】:
标签: python database mongodb mlab