首先是$set和$unset.
$set用来指定一个键的值.如果这个键存在,就修改它;不存在,就创建它.
> db.name.find()
{ "_id" : ObjectId("505a5925f67c1b9a341caefb"), "fname" : "jeff", "lname" : "jiang" }
> db.name.update({"_id" : ObjectId("505a5925f67c1b9a341caefb")},{$set:{"fname" : "jeffery"}})
> db.name.find()
{ "_id" : ObjectId("505a5925f67c1b9a341caefb"), "fname" : "jeffery", "lname" : "jiang" }
可以看到,原文档的"fname"是存在的,所以$set修改器只修改了它的值("jeff"-->"jeffery")
> db.name.update({"_id" : ObjectId("505a5925f67c1b9a341caefb")},{$set:{age:23}})
> db.name.find()
{ "_id" : ObjectId("505a5925f67c1b9a341caefb"), "age" : 23, "fname" : "jeffery", "lname" : "jiang" }
而在这里,原文档中是没有"age"这个键的,修改器创建了它.可是,问题来了,它怎么插到了这里,这和我理想中的不一样,我是希望它插入到文档的最后的.是不是因为我用"_id"查找文档,然后它就插入到了"_id"的后面呢.那么,如果我用文档的最后那个键"lname"来查找文档,它是不是会插入到新文档的最后呢?我们实验一下:
> db.name.find()
{ "_id" : ObjectId("505a5925f67c1b9a341caefb"), "fname" : "jeffery", "lname" : "jiang" }
> db.name.update({"lname" : "jiang"},{$set:{age:23}})
> db.name.find()
{ "_id" : ObjectId("505a5925f67c1b9a341caefb"), "age" : 23, "fname" : "jeffery", "lname" : "jiang" }
还是插入到了原来的位置,我们的猜想是错误的.那么,我们一定要把它插入到文档的最后怎么办呢.我目前只知道一种最原始的办法.
> db.name.find()
{ "_id" : ObjectId("505a5925f67c1b9a341caefb"), "fname" : "jeffery", "lname" : "jiang" }
> db.name.update({"_id" : ObjectId("505a5925f67c1b9a341caefb")},{"fname" : "jeffery", "lname" : "jiang",age:23})
> db.name.find()
{ "_id" : ObjectId("505a5925f67c1b9a341caefb"), "fname" : "jeffery", "lname" : "jiang", "age" : 23 }
这应该是最笨的方法了,如果有高手,望不吝赐教。
> db.name.find()
{ "_id" : ObjectId("505a5925f67c1b9a341caefb"), "fname" : "jeffery", "lname" : "jiang", "age" : 23, "favorite" : "reading" }
用$set可以修改键的数据类型。例如的的爱好不会只有一种,像我这样没爱好的,也能说出个两三个来。那么,我们可以将上面普通的“favorite”键的值变成一个数组:
> db.name.update({"_id" : ObjectId("505a5925f67c1b9a341caefb")},{$set:{favorite:"reading","swimming","surfing the Insternet","listening to music"]}})
> db.name.find()
{ "_id" : ObjectId("505a5925f67c1b9a341caefb"), "age" : 23, "favorite" : [ "reading", "swimming", "surfing the Insternet", "listening to music" ], "fname" : "jeffery", "lname" : "jiang" }
真是头疼,修改后,文档的顺序结构也变了,这里先不管了。我们用findOne()可能看得清楚一些:
> db.name.findOne()
{
"_id" : ObjectId("505a5925f67c1b9a341caefb"),
"age" : 23,
"favorite" : [
"reading",
"swimming",
"surfing the Insternet",
"listening to music"
],
"fname" : "jeffery",
"lname" : "jiang"
}
上面我们将普通的“favorite”键的值变成了一个数组,这里我们再将数组变为文档:
> db.name.update({"_id" : ObjectId("505a5925f67c1b9a341caefb")},{$set:{favorite:{1:"reading",2:"swimming",3:"surfing the Insternet",4:"listening to music"}}})
> db.name.findOne()
{
"_id" : ObjectId("505a5925f67c1b9a341caefb"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "surfing the Insternet",
"4" : "listening to music"
},
"fname" : "jeffery",
"lname" : "jiang"
}
现在我们有内嵌文档了,用$set也可以来修改内嵌文档,它的用法就像针对普通文档一样。下面我想再添加一个爱好,我觉得打乒乓球很不错,来简单实验下:
> db.name.update({"_id" : ObjectId("505a5925f67c1b9a341caefb")},{$set:{"favorite.5":"playing Ping-Pong"}})
> db.name.findOne()
{
"_id" : ObjectId("505a5925f67c1b9a341caefb"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "surfing the Insternet",
"4" : "listening to music",
"5" : "playing Ping-Pong"
},
"fname" : "jeffery",
"lname" : "jiang"
}
可是,我又觉得,打乒乓没有打篮球帅,我想把其中一个爱好改成打篮球,也很简单:
> db.name.update({"_id" : ObjectId("505a5925f67c1b9a341caefb")},{$set:{"favorite.5":"playing basketball"}})
> db.name.findOne()
{
"_id" : ObjectId("505a5925f67c1b9a341caefb"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "surfing the Insternet",
"4" : "listening to music",
"5" : "playing basketball"
},
"fname" : "jeffery",
"lname" : "jiang"
}
$set的基本用法差不多实验完了,下面来看看$unset。从名字就可以看出,它们两是一对。前者是来修改键,后者用来删除键。$unset也可以修改普通文档和内嵌文档。这里我用它来修改内嵌文档。像游泳这种爱好,一年难得一两次的,我想把它从爱好里删除,怎么操作呢?简单看一下:
> db.name.update({"_id" : ObjectId("505a5925f67c1b9a341caefb")},{$unset:{"favorite.2":1}})
> db.name.findOne()
{
"_id" : ObjectId("505a5925f67c1b9a341caefb"),
"age" : 23,
"favorite" : {
"1" : "reading",
"3" : "surfing the Insternet",
"4" : "listening to music",
"5" : "playing basketball"
},
"fname" : "jeffery",
"lname" : "jiang"
}
下面,我们来学习一下另一个修改器。今年,哥23岁了,再过不到两个月,就要24岁了。如果现在我想增加年龄,虽然$set修改器可以达到这个目的,但这里我想要用另外一个:$inc修改器。它用来增加已有键的值,或者在键不存在时创建一个键。这在有变化数值的地方,使用起来非常方便。一转眼几个月过去了,我24了,下面来修改数据:
> db.name.update({"_id" : ObjectId("505a5925f67c1b9a341caefb")},{$inc:{age:1}})
> db.name.findOne()
{
"_id" : ObjectId("505a5925f67c1b9a341caefb"),
"age" : 24,
"favorite" : {
"1" : "reading",
"3" : "surfing the Insternet",
"4" : "listening to music",
"5" : "playing basketball"
},
"fname" : "jeffery",
"lname" : "jiang"
}
世界那么大,理想那么小,而我那小小的理想目前还不曾实现,这时间又匆匆地过了,压力各种大啊。看,只要那么一下,又过了6年。30可是人生的分水领,不能再这么没出息了,不然一辈子也就这样了:
db.name.update({"_id" : ObjectId("505a5925f67c1b9a341caefb")},{$inc:{age:6}})
> db.name.findOne()
{
"_id" : ObjectId("505a5925f67c1b9a341caefb"),
"age" : 30,
"favorite" : {
"1" : "reading",
"3" : "surfing the Insternet",
"4" : "listening to music",
"5" : "playing basketball"
},
"fname" : "jeffery",
"lname" : "jiang"
}
以上是键存在的情况,存在就修改;那么键不存在呢?上面我们说了,不存在就创建。哥试着把哥的身高添进去,这是个很艰难的决定。就哥这身高,穿上鞋子都不够170的,遇到赤脚的女神都得仰望,我说不自卑,你信么?
> db.name.update({"_id" : ObjectId("505a5925f67c1b9a341caefb")},{$inc:{height:166}})
> db.name.findOne()
{
"_id" : ObjectId("505a5925f67c1b9a341caefb"),
"age" : 30,
"favorite" : {
"1" : "reading",
"3" : "surfing the Insternet",
"4" : "listening to music",
"5" : "playing basketball"
},
"fname" : "jeffery",
"height" : 166,
"lname" : "jiang"
}
还好哥才活了23年,到30岁,还能肆无忌惮地奋斗6、7年。希望能混出点名堂。其实我想说的是,$inc修改器不止能增加数字,它也能用来减少。下面我们来操作一下:
> db.name.update({"_id" : ObjectId("505a5925f67c1b9a341caefb")},{$inc:{age:-7}})
> db.name.findOne()
{
"_id" : ObjectId("505a5925f67c1b9a341caefb"),
"age" : 23,
"favorite" : {
"1" : "reading",
"3" : "surfing the Insternet",
"4" : "listening to music",
"5" : "playing basketball"
},
"fname" : "jeffery",
"height" : 166,
"lname" : "jiang"
}
总之,你想怎么修改数据,就给$inc传递一个不同的值就好了。但是,$inc只能用于整数、长整数和双精度浮点数。要是其他类型应该使用$set修改器或其他修改器。还有,在使用修改器时,“_id”的值是不能改变的,但使用整个文档替换时是可以改变的。其他键值,包括其他唯一索引的键都是可以修改的。$set和$inc用来修改标量值。