python与mongodb更新_Python MongoDB-更新

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

更新方法修改现有文档,而保存方法用新文档替换现有文档。

句法

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

>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

Or,

db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})

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

>usetestdatabase

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示例更新了集合中文档的位置值。

frompymongoimportMongoClient#Creating a pymongo clientclient=MongoClient('localhost',27017)#Getting the database instancedb=client['myDB']#Creating a collectioncoll=db['example']#Inserting document into a collectiondata=[{"_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() methodprint("Documents in the collection: ")fordoc1incoll.find():print(doc1)coll.update_one({"_id":"102"},{"$set":{"city":"Visakhapatnam"}})#Retrieving all the records using the find() methodprint("Documents in the collection after update operation: ")fordoc2incoll.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()方法更新所有满足指定条件的文档。

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

frompymongoimportMongoClient#Creating a pymongo clientclient=MongoClient('localhost',27017)#Getting the database instancedb=client['myDB']#Creating a collectioncoll=db['example']#Inserting document into a collectiondata=[{"_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() methodprint("Documents in the collection: ")fordoc1incoll.find():print(doc1)coll.update_many({},{"$set":{"city":"Visakhapatnam"}})#Retrieving all the records using the find() methodprint("Documents in the collection after update operation: ")fordoc2incoll.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'}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值