,
"userid" : "fa"
}
> db.userinfo.update({country:"china"},{set:{country:"zhongguo"}}) # 把country等于china的整条数据替换掉,只替换一条数据
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.userinfo.find() # 第一条数据被替换,只有_id字段没有改变
{ "_id" : ObjectId("5c94f37d25000fc9936a9759"), "set" : { "country" : "zhongguo" } }
{ "_id" : ObjectId("5c94f38425000fc9936a975a"), "country" : "china", "province" : "sh", "userid" : "b" }
{ "_id" : ObjectId("5c94f38625000fc9936a975b"), "country" : "china", "province" : "sh", "userid" : "c" }
{ "_id" : ObjectId("5c94f39325000fc9936a975c"), "country" : "china", "province" : "bj", "userid" : "da" }
{ "_id" : ObjectId("5c94f39925000fc9936a975d"), "country" : "china", "province" : "bj", "userid" : "fa" }
> db.userinfo.update({country:"china"},{$set:{country:"zhongguo"}}) # 把country等于china的字段更新为country等于zhongguo,只更新一条数据
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.userinfo.find()
{ "_id" : ObjectId("5c94f37d25000fc9936a9759"), "set" : { "country" : "zhongguo" } }
{ "_id" : ObjectId("5c94f38425000fc9936a975a"), "country" : "zhongguo", "province" : "sh", "userid" : "b" }
{ "_id" : ObjectId("5c94f38625000fc9936a975b"), "country" : "china", "province" : "sh", "userid" : "c" }
{ "_id" : ObjectId("5c94f39325000fc9936a975c"), "country" : "china", "province" : "bj", "userid" : "da" }
{ "_id" : ObjectId("5c94f39925000fc9936a975d"), "country" : "china", "province" : "bj", "userid" : "fa" }
> db.userinfo.update({country:"china"},{$set:{country:"zhongguo"}},{multi:true}) # 把country等于china的字段更新为country等于zhongguo,multi的值为true,所以更新所有符合条件的数据
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.userinfo.find()
{ "_id" : ObjectId("5c94f37d25000fc9936a9759"), "set" : { "country" : "zhongguo" } }
{ "_id" : ObjectId("5c94f38425000fc9936a975a"), "country" : "zhongguo", "province" : "sh", "userid" : "b" }
{ "_id" : ObjectId("5c94f38625000fc9936a975b"), "country" : "zhongguo", "province" : "sh", "userid" : "c" }
{ "_id" : ObjectId("5c94f39325000fc9936a975c"), "country |