首先,我们操作的数据都存放在test数据库中的name集合,集合中有4条记录如下:
> db
test
> show collections
name
system.indexes
> db.name.find()
{ "_id" : ObjectId("5059221f955cfb1fd75066cb"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("5059223a955cfb1fd75066cc"), "fname" : "xiaoqiang", "lname" : "he" }
{ "_id" : ObjectId("50592245955cfb1fd75066cd"), "fname" : "dengdeng", "lname" : "pan" }
{ "_id" : ObjectId("50592253955cfb1fd75066ce"), "fname" : "guage", "lname" : "zeng" }
下面我们来试着修改一下这些数据.在前面的插入中,我说过,插入时可以以文档的的方式插入,也可以先把文档赋值给变量,再以变量的方式插入.update方法也可以用这两种方式.我先用文档的方式来修改集合中的第二条记录,命令如下:
> db.name.update({"_id" : ObjectId("5059223a955cfb1fd75066cc")},{"fname" : "qiang", "lname" : "he"})
> db.name.find() #检查发现,数据修改过来了,和预期的结果一样
{ "_id" : ObjectId("5059221f955cfb1fd75066cb"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("5059223a955cfb1fd75066cc"), "fname" : "qiang", "lname" : "he" }
{ "_id" : ObjectId("50592245955cfb1fd75066cd"), "fname" : "dengdeng", "lname" : "pan" }
{ "_id" : ObjectId("50592253955cfb1fd75066ce"), "fname" : "guage", "lname" : "zeng" }
我们再用变量的方式来修改该集合中的第三条记录.首先将它找出来赋值给变量someone:
> someone=db.name.findOne({"_id" : ObjectId("50592245955cfb1fd75066cd")})
{
"_id" : ObjectId("50592245955cfb1fd75066cd"),
"fname" : "dengdeng",
"lname" : "pan"
}
重新赋值你要修改的内容来修改变量someone的值:
> someone.fname="deng"
deng
用修改过的变量替换之前文档的位置来达到修改数据的目的:
> db.name.update({"_id" : ObjectId("50592245955cfb1fd75066cd")},someone)
> db.name.find() #检查发现,数据修改过来了,和预期的结果一样
{ "_id" : ObjectId("5059221f955cfb1fd75066cb"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("5059223a955cfb1fd75066cc"), "fname" : "qiang", "lname" : "he" }
{ "_id" : ObjectId("50592245955cfb1fd75066cd"), "fname" : "deng", "lname" : "pan" }
{ "_id" : ObjectId("50592253955cfb1fd75066ce"), "fname" : "guage", "lname" : "zeng" }
之前我说过,save函数实际就是根据参数条件,调用了insert或update函数.那么save方法是否也能实现数据的修改操作呢?下面我们来实验一下.首先还是以文档的方式来进行修改操作:
> db.name.save({"_id" : ObjectId("50592253955cfb1fd75066ce")}, {"fname" : "dongren", "lname" : "zeng"})
> db.name.find() #检查发现,数据修改过来了,但和预期的结果不一样,跟之前插入数据一样,save方法也同样将第二个参数"忽略"了.
{ "_id" : ObjectId("5059221f955cfb1fd75066cb"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("5059223a955cfb1fd75066cc"), "fname" : "qiang", "lname" : "he" }
{ "_id" : ObjectId("50592245955cfb1fd75066cd"), "fname" : "deng", "lname" : "pan" }
{ "_id" : ObjectId("50592253955cfb1fd75066ce") }
正确的操作方式如下:
> db.name.save({"_id" : ObjectId("50592253955cfb1fd75066ce"), "fname" : "dongren", "lname" : "zeng"})
> db.name.find() #检查发现,数据修改过来了,和预期的结果一样
{ "_id" : ObjectId("5059221f955cfb1fd75066cb"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("5059223a955cfb1fd75066cc"), "fname" : "qiang", "lname" : "he" }
{ "_id" : ObjectId("50592245955cfb1fd75066cd"), "fname" : "deng", "lname" : "pan" }
{ "_id" : ObjectId("50592253955cfb1fd75066ce"), "fname" : "dongren", "lname" : "zeng" }
下面使用变量的方式来修改:
> someone=db.name.findOne({"_id" : ObjectId("50592253955cfb1fd75066ce")})
{
"_id" : ObjectId("50592253955cfb1fd75066ce"),
"fname" : "dongren",
"lname" : "zeng"
}
> someone.fname="guage"
guage
> db.name.save(someone)
> db.name.find() #检查发现,数据修改过来了,和预期的结果一样
{ "_id" : ObjectId("5059221f955cfb1fd75066cb"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("5059223a955cfb1fd75066cc"), "fname" : "qiang", "lname" : "he" }
{ "_id" : ObjectId("50592245955cfb1fd75066cd"), "fname" : "deng", "lname" : "pan" }
{ "_id" : ObjectId("50592253955cfb1fd75066ce"), "fname" : "guage", "lname" : "zeng" }
下面,我们也同样来看看save和update这两方法的具体实现.首先是save方法:
> db.name.save
function (obj) {
if (obj == null || typeof obj == "undefined") {
throw "can't save a null";
}
if (typeof obj == "number" || typeof obj == "string") {
throw "can't save a number or string";
}
if (typeof obj._id == "undefined") {
obj._id = new ObjectId;
return this.insert(obj);
} else {
return this.update({_id:obj._id}, obj, true);
}
}
参数说明:
obj:要更新的记录,只能是单条记录,如果collection没有存在与obj一样的id,则添加一条记录,否则更新.
再来看看update方法:
> db.name.update
function (query, obj, upsert, multi) {
assert(query, "need a query");
assert(obj, "need an object");
var firstKey = null;
for (var k in obj) {
firstKey = k;
break;
}
if (firstKey != null && firstKey[0] == "$") {
this._validateObject(obj);
} else {
this._validateForStorage(obj);
}
if (typeof upsert === "object") {
assert(multi === undefined, "Fourth argument must be empty when specifying upsert and multi with an object.");
opts = upsert;
multi = opts.multi;
upsert = opts.upsert;
}
this._db._initExtraInfo();
this._mongo.update(this._fullName, query, obj, upsert ? true : false, multi ? true : false);
this._db._getExtraInfo("Updated");
}
参数说明:
1)query:查询条件,类似于update语句内where后面的内容
2)obj:update的对象和一些更新的操作符(如$、$inc等),也可以理解为关系型数据库update语句内set后面的内容
3)upsert:如果不存在update的纪录,是否插入obj这个新的document。true为插入,默认是false,不插入
4)multi:默认是false,只更新找到的第一条纪录,如果为true,按条件查出来的多条纪录全部更新。
update方法至少得接收两个参数:要修改的对象(query)和修改后的对象(obj),而后两个参数它的默认值均为false.下面我再来简单实验下,后两个参数的基本用法.
首先说说第四个参数multi的作用:默认是false,只更新找到的第一条纪录,如果为true,按条件查出来的多条纪录全部更新.
> db.name.update({fname:"jeff"},{$set:{lname:"li"}},false,false) #为了让大家看得清楚点,我把后两个值都写了出来,这里可以不用写的,因为默认均为false.这里用到了修改器$set,关于修改器我将在以后介绍
> db.name.find() #参数multi的值是false,只更新找到的第一条纪录
{ "_id" : ObjectId("5059221f955cfb1fd75066cb"), "fname" : "jeff", "lname" : "li" }
{ "_id" : ObjectId("5059223a955cfb1fd75066cc"), "fname" : "qiang", "lname" : "he" }
{ "_id" : ObjectId("50592245955cfb1fd75066cd"), "fname" : "deng", "lname" : "pan" }
{ "_id" : ObjectId("50592253955cfb1fd75066ce"), "fname" : "guage", "lname" : "zeng" }
{ "_id" : ObjectId("50592ead955cfb1fd75066cf"), "fname" : "jeff", "lname" : "chen" }
{ "_id" : ObjectId("50592ecb955cfb1fd75066d0"), "fname" : "jeff", "lname" : "zhao" }
{ "_id" : ObjectId("50592edb955cfb1fd75066d1"), "fname" : "jeff", "lname" : "qian" }
> db.name.update({fname:"jeff"},{$set:{lname:"jiang"}},false,true)
> db.name.find() #参数multi的值是true,更新找到的所有纪录
{ "_id" : ObjectId("5059221f955cfb1fd75066cb"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("5059223a955cfb1fd75066cc"), "fname" : "qiang", "lname" : "he" }
{ "_id" : ObjectId("50592245955cfb1fd75066cd"), "fname" : "deng", "lname" : "pan" }
{ "_id" : ObjectId("50592253955cfb1fd75066ce"), "fname" : "guage", "lname" : "zeng" }
{ "_id" : ObjectId("50592ead955cfb1fd75066cf"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("50592ecb955cfb1fd75066d0"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("50592edb955cfb1fd75066d1"), "fname" : "jeff", "lname" : "jiang" }
再来看看第三个参数upsert的作用:如果不存在update的纪录,是否插入obj这个新的document。true为插入,默认是false,不插入.
> db.name.update({fname:"jeffery"},{$set:{lname:"jiang"}},false,true)
> db.name.find() #参数upsert的值是false,不插入
{ "_id" : ObjectId("5059221f955cfb1fd75066cb"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("5059223a955cfb1fd75066cc"), "fname" : "qiang", "lname" : "he" }
{ "_id" : ObjectId("50592245955cfb1fd75066cd"), "fname" : "deng", "lname" : "pan" }
{ "_id" : ObjectId("50592253955cfb1fd75066ce"), "fname" : "guage", "lname" : "zeng" }
{ "_id" : ObjectId("50592ead955cfb1fd75066cf"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("50592ecb955cfb1fd75066d0"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("50592edb955cfb1fd75066d1"), "fname" : "jeff", "lname" : "jiang" }
> db.name.update({fname:"jeffery"},{$set:{lname:"jiang"}},true,true)
> db.name.find() #参数upsert的值是true,插入.又学到一种插入数据的方法!
{ "_id" : ObjectId("5059221f955cfb1fd75066cb"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("5059223a955cfb1fd75066cc"), "fname" : "qiang", "lname" : "he" }
{ "_id" : ObjectId("50592245955cfb1fd75066cd"), "fname" : "deng", "lname" : "pan" }
{ "_id" : ObjectId("50592253955cfb1fd75066ce"), "fname" : "guage", "lname" : "zeng" }
{ "_id" : ObjectId("50592ead955cfb1fd75066cf"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("50592ecb955cfb1fd75066d0"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("50592edb955cfb1fd75066d1"), "fname" : "jeff", "lname" : "jiang" }
{ "_id" : ObjectId("5059357ad3ba22406ad408e6"), "fname" : "jeffery", "lname" : "jiang" }
update方法操作其实还是有点复杂的,这里我只简单地介绍了下它的基体操作,复杂的操作是合理利用各种修改器,以达到更高效更方便的管理.关于修改器的使用,我将在以后实验.