Python Data Access 简明教程
Python MongoDB - Update
你可以使用 update() 方法或 save() 方法更新现有文档的内容。
You can update the contents of an existing documents using the update() method or save() method.
更新方法修改现有的文档,而保存方法用新文档替换现有的文档。
The update method modifies the existing document whereas the save method replaces the existing document with the new one.
Syntax
以下为 MangoDB 的 update() 和 save() 方法的语法 −
Following is the syntax of the update() and save() methods of MangoDB −
>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)
Or,
db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
Example
假设我们在一个数据库中创建了一个集合,并在其中插入了 3 个记录,如下所示:
Assume we have created a collection in a database and inserted 3 records in it as shown below −
> 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 的文档的城市值。
Following method updates the city value of the document with 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 保存新的数据来替换文档。
Similarly you can replace the document with new data by saving it with same id using the save() method.
> 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" }
Updating documents using python
与检索单个文档的 find_one() 方法类似,pymongo 的 update_one() 方法更新单个文档。
Similar to find_one() method which retrieves single document, the update_one() method of pymongo updates a single document.
该方法接受一个查询,该查询指定要更新的文档和更新操作。
This method accepts a query specifying which document to update and the update operation.
Example
以下 Python 示例更新集合中某个文档的位置值。
Following python example updates the location value of a document in a collection.
from pymongo import MongoClient
#Creating a pymongo client
client = MongoClient('localhost', 27017)
#Getting the database instance
db = client['myDB']
#Creating a collection
coll = db['example']
#Inserting document into a collection
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 ......")
#Retrieving all the records using the find() method
print("Documents in the collection: ")
for doc1 in coll.find():
print(doc1)
coll.update_one({"_id":"102"},{"$set":{"city":"Visakhapatnam"}})
#Retrieving all the records using the find() method
print("Documents in the collection after update operation: ")
for doc2 in coll.find():
print(doc2)
Output
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() 方法更新满足指定条件的所有文档。
Similarly, the update_many() method of pymongo updates all the documents that satisfies the specified condition.
Example
以下示例更新集合中所有文档的位置值(空条件):
Following example updates the location value in all the documents in a collection (empty condition) −
from pymongo import MongoClient
#Creating a pymongo client
client = MongoClient('localhost', 27017)
#Getting the database instance
db = client['myDB']
#Creating a collection
coll = db['example']
#Inserting document into a collection
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 ......")
#Retrieving all the records using the find() method
print("Documents in the collection: ")
for doc1 in coll.find():
print(doc1)
coll.update_many({},{"$set":{"city":"Visakhapatnam"}})
#Retrieving all the records using the find() method
print("Documents in the collection after update operation: ")
for doc2 in coll.find():
print(doc2)
Output
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'}