【MongoDB】谨慎使用更新操作

1.仅更新查询出来的第一行数据 

> db.users.update({"Gender":"F"},{$set:{"Country":"UK"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find({"Gender":"F"})
{ "_id" : ObjectId("666a5b45adc823c6544594dc"), "FName" : "Test User", "Age" : 45, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594dd"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594de"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594df"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594e0"), "Name" : "Test User4", "Age" : 14, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594e1"), "Name" : "Test User5", "Age" : 15, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594e2"), "Name" : "Test User6", "Age" : 16, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594e3"), "Name" : "Test User7", "Age" : 17, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594e4"), "Name" : "Test User8", "Age" : 18, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594e5"), "Name" : "Test User9", "Age" : 19, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594e6"), "Name" : "Test User10", "Age" : 20, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594e7"), "Name" : "Test User11", "Age" : 21, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594e8"), "Name" : "Test User12", "Age" : 22, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594e9"), "Name" : "Test User13", "Age" : 23, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594ea"), "Name" : "Test User14", "Age" : 24, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594eb"), "Name" : "Test User15", "Age" : 25, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594ec"), "Name" : "Test User16", "Age" : 26, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594ed"), "Name" : "Test User17", "Age" : 27, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594ee"), "Name" : "Test User18", "Age" : 28, "Gender" : "F", "Country" : "India" }
{ "_id" : ObjectId("666a5b86adc823c6544594ef"), "Name" : "Test User19", "Age" : 29, "Gender" : "F", "Country" : "India" }
Type "it" for more

第一个查询出来的女生国家变成UK.

2.批量更新所有批量的行 

> db.users.update({"Gender":"F"},{$set:{"Country":"UK"}},{multi:true})
WriteResult({ "nMatched" : 21, "nUpserted" : 0, "nModified" : 20 })
> db.users.find({"Gender":"F"})
{ "_id" : ObjectId("666a5b45adc823c6544594dc"), "FName" : "Test User", "Age" : 45, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594dd"), "Name" : "Test User1", "Age" : 11, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594de"), "Name" : "Test User2", "Age" : 12, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594df"), "Name" : "Test User3", "Age" : 13, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e0"), "Name" : "Test User4", "Age" : 14, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e1"), "Name" : "Test User5", "Age" : 15, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e2"), "Name" : "Test User6", "Age" : 16, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e3"), "Name" : "Test User7", "Age" : 17, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e4"), "Name" : "Test User8", "Age" : 18, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e5"), "Name" : "Test User9", "Age" : 19, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e6"), "Name" : "Test User10", "Age" : 20, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e7"), "Name" : "Test User11", "Age" : 21, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e8"), "Name" : "Test User12", "Age" : 22, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594e9"), "Name" : "Test User13", "Age" : 23, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594ea"), "Name" : "Test User14", "Age" : 24, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594eb"), "Name" : "Test User15", "Age" : 25, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594ec"), "Name" : "Test User16", "Age" : 26, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594ed"), "Name" : "Test User17", "Age" : 27, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594ee"), "Name" : "Test User18", "Age" : 28, "Gender" : "F", "Country" : "UK" }
{ "_id" : ObjectId("666a5b86adc823c6544594ef"), "Name" : "Test User19", "Age" : 29, "Gender" : "F", "Country" : "UK" }
Type "it" for more

"nModified" : 20,说明本次跟新了20行,由此可看,通过添加:{multi:true}属性能够使得mongodb更新所有批量的行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值