这里,我们将了解一下数组修改器。数组,是我们经常看到和使用到的且非常有用的数据结构:它不仅可以通过索进行引用,还可以作为集合来使用。数组修改器,顾名思义,它是用来修改数组的,而不能用来修改整数或者字符串。数组修改器不多,就那么几个,但熟练掌握它后,将给我们带来非常方便的操作。下面,我们来了解一下:
> db.user.findOne()
{
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"fname" : "deng",
"lname" : "pan"
},
{
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "dongren",
"lname" : "zeng"
}
]
}
以上是我的还在完善中的个人信息文档。假设最近我又交了一个好朋友,我想把他加到我的人际关系“relationships”数组中。这时,$push修改器就派上用场了。$push的作用就是,如果指定的键已经存在,它会向已有的数组末尾加入一个元素,要是没有就会创建一个新的数组。下面我们把新朋友加进去。
> db.user.update({"_id" : ObjectId("4ffcb2ed65282ea95f7e3304")},{$push:{"relationships":{"fname":"xiong","lname":"lan"}}})
> db.user.findOne()
{
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"fname" : "deng",
"lname" : "pan"
},
{
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "dongren",
"lname" : "zeng"
},
{
"fname" : "xiong",
"lname" : "lan"
}
]
}
有加就有减,那么怎么对数组进行“减”操作呢。能达到对数组“减”目的的修改器有两个,$pop和$pull。$pop和$pull又有区别,我们来分别实验。首先是$pop
> db.user.update({"_id" : ObjectId("4ffcb2ed65282ea95f7e3304")},{$pop:{"relationships":1}})
> db.user.findOne()
{
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"fname" : "deng",
"lname" : "pan"
},
{
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "dongren",
"lname" : "zeng"
}
]
}
从上面可以看出,它把我们刚加进去的朋友又删除了,也就是说它从数组的最后删除。那么,如果我们想从数组的开头删除该怎么办呢。很简单,把上面的“1”改成“-1”,它将逆向操作。下面看一下:
> db.user.update({"_id" : ObjectId("4ffcb2ed65282ea95f7e3304")},{$pop:{"relationships":-1}})
> db.user.findOne()
{
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "dongren",
"lname" : "zeng"
}
]
}
从结果可以看出,它达到了我们预期的目的。那么如果我们想删除数组中间的呢。这时,$pull派上用场。首先,我们先将新朋友加进去,这里我不再演示。但我们可以想到“dongren”这个人是在数组的中间。现在我们要将他删除:
> db.user.update({"_id" : ObjectId("4ffcb2ed65282ea95f7e3304")},{$pull:{"relationships":{"fname":"dongren","lname":"zeng"}}})
> db.user.findOne()
{
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "xiong",
"lname" : "lan"
}
]
}
从上面可以看出,$pull可以将数组中间的数据删除。这里还有一点要注意,$pull会将所有匹配到的数据都删除,这里我就不做实验了。下面,我们再来看看$push还有什么特点,我们再往数组里插入一相同的数据,看看会如何:
> db.user.update({"_id" : ObjectId("4ffcb2ed65282ea95f7e3304")},{$push:{"relationships":{"fname":"xiong","lname":"lan"}}})
> db.user.findOne()
{
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "xiong",
"lname" : "lan"
},
{
"fname" : "xiong",
"lname" : "lan"
}
]
}
结果表明,它是能正常插入到数组的。而在实际生产环境中,我们都不想看到这样的结果,那么,这里又出现了两个可以用的修改器:$ne和$addToSet。$ne主要拿来判断,若数组里面有这个值,则不插入;没有才插入。
> db.user.update({"relationships.fname":{$ne:"xiong"}},{$set:{"fname":"xiong","lname":"lan"}})
> db.user.findOne()
{
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "xiong",
"lname" : "lan"
},
{
"fname" : "xiong",
"lname" : "lan"
}
]
}
由结果可以看出,由于该数据在数组中已经存在,所以不再插入。其实,$addToSet比$ne更好用,它可以自己判断数据是否存在,而且它和$each结合使用,还能同时在数组中插入多个数据,这是$ne没办法办到的,下面我们来看一下$addToSet的用法,这里顺便结合了$each的使用:
> db.user.update({"_id" : ObjectId("4ffcb2ed65282ea95f7e3304")},
{$addToSet:{"relationships":{$each:[{"fname":"xiong","lname":"lan"},
{"fname":"dongren","lname":"zeng"}]}}})
> db.user.findOne()
{
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "xiong",
"lname" : "lan"
},
{
"fname" : "xiong",
"lname" : "lan"
},
{
"fname" : "dongren",
"lname" : "zeng"
}
]
}
在修改语句中,我们想同时插入{"fname":"xiong","lname":"lan"},
{"fname":"dongren","lname":"zeng"}两个数据,但在原数组中,
{"fname":"xiong","lname":"lan"}这个数据已经存在,所以它只插入了后面那条。达到了我们想要的目的。所以,我个人更喜欢使用$addToSet。
有时候数组有多个值,而我们只想对其中的一部分进行操作。如果我们把整个文档都抄下来,那太麻烦也太愚蠢了。好在mongodb给我们提供了两种简便方法:通过位置或者操作符“$”。下面我们来分别看看这两种方法怎么使用。首先是通过数组位置来操作。数组都是以0开头的,可以将下标直接作为键来选择元素。例如,我们想给数组的第一个数据加上年龄键值对,我们可以这么操作:
> db.user.update({"_id" : ObjectId("4ffcb2ed65282ea95f7e3304")},{$set:{"relationships.0.age":22}})
> db.user.findOne()
{
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"age" : 22,
"fname" : "qiang",
"lname" : "he"
},
{
"fname" : "deng",
"lname" : "pan"
},
{
"fname" : "xiong",
"lname" : "lan"
}
]
}
可是很多情况下,不预先查询文档我们就不知道要修改数组的元素的下标。这时定位操作符“$”就很好用了。它就是用来定位查询文档已匹配的元素,并进行更新。我们来看看它怎么用:
> db.user.update({"relationships.fname":"xiong"},{$set:{"relationships.$.age":22}})
> db.user.findOne()
{
"_id" : ObjectId("4ffcb2ed65282ea95f7e3304"),
"age" : 23,
"favorite" : {
"1" : "reading",
"2" : "swimming",
"3" : "listening music"
},
"fname" : "jeff",
"height" : 166,
"lname" : "jiang",
"relationships" : [
{
"age" : 22,
"fname" : "qiang",
"lname" : "he"
},
{
"age" : 22,
"fname" : "deng",
"lname" : "pan"
},
{
"age" : 22,
"fname" : "xiong",
"lname" : "lan"
}
]
}
有关数组的常用的修改器的用法就写到这里。可能例子用得不是很恰当,但通过这些例子,我觉得至少能对这些修改器的作用有个大致的了解。不妥甚至不对的地方,欢迎大神们拍砖。