Insert_one 不存在这样的方法@pymongo 3.7.2
Insert_one no such method exists @ pymongo 3.7.2
我刚开始学习 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 版本,所以我应该(并且必须)使用“.[=30” =]”,而不是“.insert”
insert_one 根据 pymongo 文档存在,服务器版本 >= 3.2...
用途是:
user = {'x': 1}
result = db.test.insert_one(user)
result.inserted_id
关于insert_one更完整的解释:
>>> db.test.count_documents({'x': 1})
0
>>> result = db.test.insert_one({'x': 1})
>>> result.inserted_id
ObjectId('54f112defba522406c9cc208')
>>> db.test.find_one({'x': 1})
{u'x': 1, u'_id': ObjectId('54f112defba522406c9cc208')}
以下内容,我执行并运行良好:
# importing client mongo to make the connection
from pymongo import MongoClient
print("--- Exemplo pymongo Connection ---")
# Connection to MongoDB
client = MongoClient('localhost', 27017)
# Selection the Database
db = client.python
# Select the collection
collection = db.users
# Set up a document
user = {"id": 1, "username": "Test"}
# insert one document into selected document
result = collection.insert_one(user)
# Selection just one document from collection
#result = collection.find_one()
# removing the document inserted
collection.delete_one(user)
# print the inserted_id
print("inserted_id: ", result.inserted_id)
我刚开始学习 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 版本,所以我应该(并且必须)使用“.[=30” =]”,而不是“.insert”
insert_one 根据 pymongo 文档存在,服务器版本 >= 3.2...
用途是:
user = {'x': 1}
result = db.test.insert_one(user)
result.inserted_id
关于insert_one更完整的解释:
>>> db.test.count_documents({'x': 1})
0
>>> result = db.test.insert_one({'x': 1})
>>> result.inserted_id
ObjectId('54f112defba522406c9cc208')
>>> db.test.find_one({'x': 1})
{u'x': 1, u'_id': ObjectId('54f112defba522406c9cc208')}
以下内容,我执行并运行良好:
# importing client mongo to make the connection
from pymongo import MongoClient
print("--- Exemplo pymongo Connection ---")
# Connection to MongoDB
client = MongoClient('localhost', 27017)
# Selection the Database
db = client.python
# Select the collection
collection = db.users
# Set up a document
user = {"id": 1, "username": "Test"}
# insert one document into selected document
result = collection.insert_one(user)
# Selection just one document from collection
#result = collection.find_one()
# removing the document inserted
collection.delete_one(user)
# print the inserted_id
print("inserted_id: ", result.inserted_id)