Python MongoDB - 更新

您可以使用 update() 方法或 save() 方法更新现有文档的内容。

update 方法会修改现有文档,而 save 方法会用新文档替换现有文档。

语法

以下是 MangoDB 的 update() 和 save() 方法的语法 −

>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)
或者,
db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})

示例

假设我们在数据库中创建了一个集合并在其中插入了 3 条记录,如下所示 −

> use testdatabase
switched to db testdatabase
> data = [
   ... {"_id": "1001", "name": "Ram", "age": "26", "city": "Hyderabad"},
   ... {"_id": "1002", "name" : "Rahim", "age" : 27, "city" : "Bangalore" },
   ... {"_id": "1003", "name" : "Robert", "age" : 28, "city" : "Mumbai" }
]
[
   {"_id" : "1001", "name" : "Ram", "age" : "26", "city" : "Hyderabad"},
   {"_id" : "1002", "name" : "Rahim", "age" : 27, "city" : "Bangalore"},
   {"_id" : "1003", "name" : "Robert", "age" : 28, "city" : "Mumbai"}
]
> db.createCollection("sample")
{ "ok" : 1 }
> db.sample.insert(data)

以下方法更新 id 为 1002 的文档的城市值。

>db.sample.update({"_id":"1002"},{"$set":{"city":"Visakhapatnam"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.sample.find()
{ "_id" : "1001", "name" : "Ram", "age" : "26", "city" : "Hyderabad" }
{ "_id" : "1002", "name" : "Rahim", "age" : 27, "city" : "Visakhapatnam" }
{ "_id" : "1003", "name" : "Robert", "age" : 28, "city" : "Mumbai" }

同样,您可以使用 save() 方法用相同的 id 保存文档,从而用新数据替换文档。

> db.sample.save({ "_id" : "1001", "name" : "Ram", "age" : "26", "city" : "Vijayawada" })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.sample.find()
{ "_id" : "1001", "name" : "Ram", "age" : "26", "city" : "Vijayawada" }
{ "_id" : "1002", "name" : "Rahim", "age" : 27, "city" : "Visakhapatnam" }
{ "_id" : "1003", "name" : "Robert", "age" : 28, "city" : "Mumbai" }

使用 python 更新文档

与检索单个文档的 find_one() 方法类似,pymongo 的 update_one() 方法更新单个文档。

此方法接受指定要更新哪个文档和更新操作的查询。

示例

以下 python 示例更新集合中文档的位置值。

from pymongo import MongoClient

#创建 pymongo 客户端
client = MongoClient('localhost', 27017)

#获取数据库实例
db = client['myDB']

#创建集合
coll = db['example']

#将文档插入集合
data = [
   {"_id": "101", "name": "Ram", "age": "26", "city": "Hyderabad"},
   {"_id": "102", "name": "Rahim", "age": "27", "city": "Bangalore"},
   {"_id": "103", "name": "Robert", "age": "28", "city": "Mumbai"}
]
res = coll.insert_many(data)
print("Data inserted ......")

#使用 find() 方法检索所有记录
print("Documents in the collection: ")
for doc1 in coll.find():
   print(doc1)
coll.update_one({"_id":"102"},{"$set":{"city":"Visakhapatnam"}})

#使用 find() 方法检索所有记录
print("Documents in the collection after update operation: ")
for doc2 in coll.find():
   print(doc2)

输出

Data inserted ......
Documents in the collection:
{'_id': '101', 'name': 'Ram', 'age': '26', 'city': 'Hyderabad'}
{'_id': '102', 'name': 'Rahim', 'age': '27', 'city': 'Bangalore'}
{'_id': '103', 'name': 'Robert', 'age': '28', 'city': 'Mumbai'}
Documents in the collection after update operation:
{'_id': '101', 'name': 'Ram', 'age': '26', 'city': 'Hyderabad'}
{'_id': '102', 'name': 'Rahim', 'age': '27', 'city': 'Visakhapatnam'}
{'_id': '103', 'name': 'Robert', 'age': '28', 'city': 'Mumbai'}

类似地,pymongo 的 update_many() 方法更新所有满足指定条件的文档。

示例

以下示例更新集合中所有文档的位置值(空条件)−

from pymongo import MongoClient

#创建 pymongo 客户端
client = MongoClient('localhost', 27017)

#获取数据库实例
db = client['myDB']

#创建集合
coll = db['example']

#将文档插入集合
data = [
   {"_id": "101", "name": "Ram", "age": "26", "city": "Hyderabad"},
   {"_id": "102", "name": "Rahim", "age": "27", "city": "Bangalore"},
   {"_id": "103", "name": "Robert", "age": "28", "city": "Mumbai"}
]
res = coll.insert_many(data)
print("Data inserted ......")

#使用 find() 方法检索所有记录
print("Documents in the collection: ")
for doc1 in coll.find():
   print(doc1)
coll.update_many({},{"$set":{"city":"Visakhapatnam"}})

#使用 find() 方法检索所有记录
print("Documents in the collection after update operation: ")
for doc2 in coll.find():
   print(doc2)

输出

Data inserted ......
Documents in the collection:
{'_id': '101', 'name': 'Ram', 'age': '26', 'city': 'Hyderabad'}
{'_id': '102', 'name': 'Rahim', 'age': '27', 'city': 'Bangalore'}
{'_id': '103', 'name': 'Robert', 'age': '28', 'city': 'Mumbai'}
Documents in the collection after update operation:
{'_id': '101', 'name': 'Ram', 'age': '26', 'city': 'Visakhapatnam'}
{'_id': '102', 'name': 'Rahim', 'age': '27', 'city': 'Visakhapatnam'}
{'_id': '103', 'name': 'Robert', 'age': '28', 'city': 'Visakhapatnam'}