MongoDB入门4——更新文档和修改器2
c)数组修改器数组修改器,顾名思义,当然是操作数组的啦。一般来说多数组的操作有若干不同种类的,因此,MongoDB也准备了不同的数组修改器。我们会一一学习。
$push修改器
$push修改器能够向指定的数组的末端插入一个新的元素。假设这个数组本身都不存在,那么先创建这个数组。现在假设我现在发表一篇博客,刚一发表,就有人发表评论了。这时候就需要往评论列表中加一个评论记录。
> db.blog.insert({"title":"A New Blog","content":"MongoDB tutorial..."});
> db.blog.update({"title":"A New Blog"},{"$push":{"comments":{"name":"xiaobai","content":"very good"}}});
> db.blog.find();
{ "_id" : ObjectId("4ee47369af9c9fecc9c9cf61"), "comments" : [ { "name" : "xiaobai", "content" : "very good" } ], "content" : "MongoDB tutorial...", "title" : "A New Blog" }
>
> db.blog.insert({"title":"A New Blog","content":"MongoDB tutorial..."});
> db.blog.update({"title":"A New Blog"},{"$push":{"comments":{"name":"xiaobai","content":"very good"}}});
> db.blog.find();
{ "_id" : ObjectId("4ee47369af9c9fecc9c9cf61"), "comments" : [ { "name" : "xiaobai", "content" : "very good" } ], "content" : "MongoDB tutorial...", "title" : "A New Blog" }
>
之前没记录,但是现在已经添加了一个评论数组,并且已经往里面加入了xiaobai的评论。现在再往里面添加一条评论:
> db.blog.update({"title":"A New Blog"},{"$push":{"comments":{"name":"xiaohong","content":"very very good"}}});
> db.blog.find();
{ "_id" : ObjectId("4ee47369af9c9fecc9c9cf61"), "comments" : [ { "name" : "xiaobai", "content" : "very good" }, { "name" : "xiaohong", "content" : "very very good" } ], "content" : "MongoDB tutorial...", "title" : "A New Blog" }
> <SPAN style="BACKGROUND-COLOR: #ffffff; FONT-FAMILY: Monaco; WHITE-SPACE: normal; COLOR: #555555; FONT-SIZE: 14px"> </SPAN>
> db.blog.update({"title":"A New Blog"},{"$push":{"comments":{"name":"xiaohong","content":"very very good"}}});
> db.blog.find();
{ "_id" : ObjectId("4ee47369af9c9fecc9c9cf61"), "comments" : [ { "name" : "xiaobai", "content" : "very good" }, { "name" : "xiaohong", "content" : "very very good" } ], "content" : "MongoDB tutorial...", "title" : "A New Blog" }
>
$ne修改器
如果有这种情况:一个人只允许只允许评论一次,那么我们在添加新的评论时,就需要判断要添加的评论是否已经存在了。$ne修改器就是判断是否不存在某条记录,不存在的时候才执行某个操作。接着上面的例子,我们先查看,评论中没有lisi的评论的话,我们就插入一条新的lisi的评论:
> db.blog.update({"title":"A New Blog","comments.name":{"$ne":"lisi"}},{"$push":{"comments":{"name":"lisi","comment":"Not Bad"}}});
> db.blog.find();
{ "_id" : ObjectId("4ee47369af9c9fecc9c9cf61"), "comments" : [ { "name" : "xiaobai", "content" : "very good" }, { "name" : "xiaohong", "content" : "very very good" }, { "name" : "lisi", "comment" : "Not Bad" } ], "content" : "MongoDB tutorial...", "title" : "A New Blog" }
>
> db.blog.update({"title":"A New Blog","comments.name":{"$ne":"lisi"}},{"$push":{"comments":{"name":"lisi","comment":"Not Bad"}}});
> db.blog.find();
{ "_id" : ObjectId("4ee47369af9c9fecc9c9cf61"), "comments" : [ { "name" : "xiaobai", "content" : "very good" }, { "name" : "xiaohong", "content" : "very very good" }, { "name" : "lisi", "comment" : "Not Bad" } ], "content" : "MongoDB tutorial...", "title" : "A New Blog" }
> 现在,如果我们再次执行上面的update代码,就不会再添加lisi的评论了。这里我们就不再演示了。
$addToSet修改器
这个修改器其实做的就是上面一样的事情。它会自动的判断某个键是否存在重复的值。不存在才添加记录。现在如果有一个关于用户信息的文档如下:
> var user = {"name":"Tom","emailaddrs":["a@b.com","b@b.com"]};
> db.users.insert(user);
> db.users.find();
{ "_id" : ObjectId("4ee49adeaf9c9fecc9c9cf62"), "name" : "Tom", "emailaddrs" : [ "a@b.com", "b@b.com" ] }
>
> var user = {"name":"Tom","emailaddrs":["a@b.com","b@b.com"]};
> db.users.insert(user);
> db.users.find();
{ "_id" : ObjectId("4ee49adeaf9c9fecc9c9cf62"), "name" : "Tom", "emailaddrs" : [ "a@b.com", "b@b.com" ] }
> 现在我们需要再加一个邮件地址:c@b.com,我们可以使用这个修改器添加新的邮件地址:
> db.users.update({"_id" : ObjectId("4ee49adeaf9c9fecc9c9cf62")},{"$addToSet":{"emailaddrs":"c@b.com"}});
> db.users.find();
{ "_id" : ObjectId("4ee49adeaf9c9fecc9c9cf62"), "emailaddrs" : [ "a@b.com", "b@b.com", "c@b.com" ], "name" : "Tom" }
> &nb