Python MongoDB – Update_one()
MongoDB是一个跨平台的面向文档和非关系(即 NoSQL)的数据库程序。它是一个开源文档数据库,以键值对的形式存储数据。
首先创建一个我们执行 update_one() 操作的数据库:
Python3
# importing Mongoclient from pymongo
from pymongo import MongoClient
try:
conn = MongoClient() # Making connection
except:
print("Could not connect to MongoDB")
# database
db = conn.database
# Created or Switched to collection
# names: GeeksForGeeks
collection = db.GeeksForGeeks
# Creating Records:
record1 = { "appliance":"fan",
"quantity":10,
"rating":"3 stars",
"company":"havells"}
record2 = { "appliance":"cooler",
"quantity":15,
"rating":"4 stars",
"company":"symphony"}
record3 = { "appliance":"ac",
"quantity":20,
"rating":"5 stars",
"company":"voltas"}
record4 = { "appliance":"tv",
"quantity":12,
"rating":"3 stars",
"company":"samsung"}
# Inserting the Data
rec_id1 = collection.insert_one(record1)
rec_id2 = collection.insert_one(record2)
rec_id3 = collection.insert_one(record3)
rec_id4 = collection.insert_one(record4)
# Printing the data inserted
print("The data in the database is:")
cursor = collection.find()
for record in cursor:
print(record)
Python3
# importing Mongoclient from pymongo
from pymongo import MongoClient
conn = MongoClient('localhost', 27017)
# database
db = conn.database
# Created or Switched to collection
# names: GeeksForGeeks
collection = db.GeeksForGeeks
# Updating fan quantity form 10 to 25.
filter = { 'appliance': 'fan' }
# Values to be updated.
newvalues = { "$set": { 'quantity': 25 } }
# Using update_one() method for single
# updation.
collection.update_one(filter, newvalues)
# Printing the updated content of the
# database
cursor = collection.find()
for record in cursor:
print(record)
Python3
# importing Mongoclient from pymongo
from pymongo import MongoClient
conn = MongoClient('localhost', 27017)
# database
db = conn.database
# Created or Switched to collection
# names: GeeksForGeeks
collection = db.GeeksForGeeks
# Updating the tv company name from
# 'samsung' to 'sony'.
filter = { 'appliance': 'tv' }
# Values to be updated.
newvalues = { "$set": { 'company': "sony" } }
# Using update_one() method for single updation.
collection.update_one(filter, newvalues)
# Printing the updated content of the database
cursor = collection.find()
for record in cursor:
print(record)
输出 :
MongoDB外壳:
更新一个()
它是一个函数,我们可以通过它更新 MongoDB 数据库或集合中的记录。该方法主要关注我们传递的两个参数,一个是定义要更新哪个文档的查询(即过滤器)对象,第二个是定义文档的新值(即 new_values)的对象,其余参数是可选的,我们将在语法部分讨论。此函数查找与查询匹配的第一个文档,并使用定义文档新值的对象对其进行更新,即根据过滤器更新集合中的单个文档。
句法:
collection.update_one(filter, new_values, upsert=False, bypass_document_validation=False, collation=None, array_filters=None, session=None)
Parameters:
- ‘filter’ : A query that matches the document to update.
- ‘new_values’ : The modifications to apply.
- ‘upsert’ (optional): If “True”, perform an insert if no documents match the filter.
- ‘bypass_document_validation’ (optional) : If “True”, allows the write to opt-out of document level validation. Default is “False”.
- ‘collation’ (optional) : An instance of class: ‘~pymongo.collation.Collation’. This option is only supported on MongoDB 3.4 and above.
- ‘array_filters’ (optional) : A list of filters specifying which array elements an update should apply. Requires MongoDB 3.6+.
- ‘session’ (optional) : a class:’~pymongo.client_session.ClientSession’.
示例 1:在此示例中,我们将风扇数量从 10 更新为 25。
Python3
# importing Mongoclient from pymongo
from pymongo import MongoClient
conn = MongoClient('localhost', 27017)
# database
db = conn.database
# Created or Switched to collection
# names: GeeksForGeeks
collection = db.GeeksForGeeks
# Updating fan quantity form 10 to 25.
filter = { 'appliance': 'fan' }
# Values to be updated.
newvalues = { "$set": { 'quantity': 25 } }
# Using update_one() method for single
# updation.
collection.update_one(filter, newvalues)
# Printing the updated content of the
# database
cursor = collection.find()
for record in cursor:
print(record)
输出 :
MongoDB外壳:
示例 2:在此示例中,我们使用 update_one() 将电视公司名称从“三星”更改为“索尼”:
Python3
# importing Mongoclient from pymongo
from pymongo import MongoClient
conn = MongoClient('localhost', 27017)
# database
db = conn.database
# Created or Switched to collection
# names: GeeksForGeeks
collection = db.GeeksForGeeks
# Updating the tv company name from
# 'samsung' to 'sony'.
filter = { 'appliance': 'tv' }
# Values to be updated.
newvalues = { "$set": { 'company': "sony" } }
# Using update_one() method for single updation.
collection.update_one(filter, newvalues)
# Printing the updated content of the database
cursor = collection.find()
for record in cursor:
print(record)
输出 :
MongoDB外壳:
注意: “$set”运算符将字段的值替换为指定的值。如果该字段不存在,“$set”将添加一个具有指定值的新字段,前提是新字段不违反类型约束。