【发布时间】:2019-04-12 07:12:15
【问题描述】:
我刚开始学习 Python 和所有相关内容。
我尝试迈出第一步,安装 MongoDB(工作)并连接到它。
from pymongo import MongoClient
from pprint import pprint
from random import randint
client = MongoClient('localhost', 27017)
db = client.test
collection = db.users
user = {"id": 1, "username": "Test"}
user_id = collection.insert_one(user).inserted_id
print(user_id)
这是完整的代码。
pymongo 版本:3.7.2 检查:
pip freeze | grep pymongo
Output: pymongo==3.7.2
Python 版本:3.7.1
如果我尝试执行我的小脚本,则会出现以下错误:
'Collection' object is not callable.
If you meant to call the 'insert_one' method on a 'Collection'
object it is
failing because no such method exists.
我的错在哪里?
一项小研究表明,在 pymongo v2 中,“.insert_one”是“.insert”,但安装的是 3.7.2 版本,所以我应该(并且必须)使用“.insert.one”,而不是“.insert”。插入”
【问题讨论】:
标签: python pymongo pymongo-3.x