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更新所有批量的行。