mongodb修改器:

来源:互联网 发布:windows系统声音没了 编辑:程序博客网 时间:2024/05/17 07:44
mongodb修改器:    $inc        用来增加或减少已有的键的键值((只能为满足要求的数字)),或者在键不存在的时候创建一个键。{"$inc":{"k1":±数字}}    $set        1.修改器用来指定一个键值。如果这个键不存在,则创建他,2.修改内嵌文档.例:{"$set":{"k1.k11":"v11"}}    $unset      用于将键删除.{"$unset":{"k1":1}}    $push       数组修改器,如果指定的值已经存在,"$push"会想已有的数组末尾加入一个元素,要是没有就会创建一个新的数组。例:{"$push":{"k1":{"k11":"v11","k12":"v12"}}    $ne         如果一个值不在数组里面就把他加进去,如果在不添加。例:update({"k1":"v1","k2":{$ne:"v2n"}},{$push:{"k2":"v2n"}})    $addToSet   如果一个值不在数组里面就把他加进去,如果在不添加。可避免重复。例:update({"k1":v1},{$addToSet:{"k2":"v2n"}})    $each       和$addToSet修改结合起来用,可以一次添加多个不同的值。例:{"k1":v1},{$addToSet:{"k2":{$each:["v21","v22","v21"]}}}    $pop        从数组中删除元素,他可以从数组中的任何一端删除元素。例:{$pop:{key:1}} 从数组末尾删除一个元素.{$pop:{key:-1}} 从数组头部删除一个元素.    $pull       基于特定条件来删除元素。例:{$pull:{"k2":"v2n"}}    $           数组的定位修改器.                update({"k1":"v1"},{$set:{"k2.number.k2n":"newDate"}})//k2为数组,number为数组第几个,0为第一个,                update({"k2.k2n":"k2n"},{$set:{"k2.$.k2m":"v2m"}})
详细解读:
1.$set修改器--------------------------------------------------------------------------------$set修改器用来指定一个键值,如果这个键不存在,则创建他。修改内嵌文档,对于内嵌文档在使用$set更新时,使用"."连接的方式例一:> db.users.findOne(){        "_id" : ObjectId("56fe7df8b322e3ff1dabf834"),        "name" : "joe",        "favorite book" : "war and pace"}> db.users.update({"name":"joe"},{"$set":{"favorite book":["cat's cardle","foundation trilogy","ender's game"]}})//添加键值对WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.findOne(){        "_id" : ObjectId("56fe7df8b322e3ff1dabf834"),        "name" : "joe",        "favorite book" : [                "cat's cardle",                "foundation trilogy",                "ender's game"        ]}例二:> db.blog.findOne(){        "_id" : ObjectId("57709da84f533aa7535d46d3"),        "title" : "a blog post",        "author" : {                "name" : "joe",                "email" : "joe@example.com"        }}> db.blog.update({"author.name":"joe"},{"$set":{"author.name":"joe schmoe"}})//修改键值WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.blog.findOne(){        "_id" : ObjectId("57709da84f533aa7535d46d3"),        "title" : "a blog post",        "author" : {                "name" : "joe schmoe",                "email" : "joe@example.com"        }}2.$inc修改器--------------------------------------------------------------------------------$inc其用来增加或减少已有的键的键值(键值必须是数字),或者在键不存在的时候创建一个键。> db.games.findOne(){        "_id" : ObjectId("5770a1394f533aa7535d46d4"),        "game" : "pinball",        "user" : "joe"}> db.games.update({"game":"pinball","user":"joe"},{"$inc":{"score":50}})//键值不存在时WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.games.findOne(){        "_id" : ObjectId("5770a1394f533aa7535d46d4"),        "game" : "pinball",        "user" : "joe",        "score" : 50}> db.games.update({"game":"pinball","user":"joe"},{"$inc":{"score":50}})//增加WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.games.findOne(){        "_id" : ObjectId("5770a1394f533aa7535d46d4"),        "game" : "pinball",        "user" : "joe",        "score" : 100}> db.games.update({"game":"pinball","user":"joe"},{"$inc":{"score":-20}})//减少WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.games.findOne(){        "_id" : ObjectId("5770a1394f533aa7535d46d4"),        "game" : "pinball",        "user" : "joe",        "score" : 80}3.$unset修改器--------------------------------------------------------------------------------$unset修改器将键删除> db.users.findOne(){        "_id" : ObjectId("56fe7df8b322e3ff1dabf834"),        "name" : "joe",        "age" : 30,        "favorite book" : [                "cat's cardle",                "foundation trilogy",                "ender's game"        ]}> db.users.update({"name":"joe"},{"$unset":{"favorite book":1}})//删除键为:favorite book的键值对WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.findOne(){        "_id" : ObjectId("56fe7df8b322e3ff1dabf834"),        "name" : "joe",        "age" : 30,}数组修改器:1.$push修改器--------------------------------------------------------------------------------$push修改器如果指定的值已经存在,"$push"会想已有的数组末尾加入一个元素,要是没有就会创建一个新的数组。> db.blog.findOne(){ "_id" : ObjectId("57709da84f533aa7535d46d3"), "title" : "a blog post"}> db.blog.update({"title":"a blog post"},{"$push":{"comments":{"name":"joe","email":"joe@example.com","content":"nice post"}}})//键值对不存在WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.blog.findOne(){        "_id" : ObjectId("57709da84f533aa7535d46d3"),        "title" : "a blog post",        "comments" : [                {                        "name" : "joe",                        "email" : "joe@example.com",                        "content" : "nice post"                }        ]}> db.blog.update({"title":"a blog post"},{"$push":{"comments":{"name":"bob","email":"bob@example.com","content":"good post"}}})//键值存在,在已有数组下添加元素WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.blog.findOne(){        "_id" : ObjectId("57709da84f533aa7535d46d3"),        "title" : "a blog post",        "comments" : [                {                        "name" : "joe",                        "email" : "joe@example.com",                        "content" : "nice post"                },                {                        "name" : "bob",                        "email" : "bob@example.com",                        "content" : "good post"                }        ]}2.数组修改器 $ne--------------------------------------------------------------------------------在查询文档中,如果一个值不在数组里面就把他加进去,如果在不添加。> db.users.findOne(){        "_id" : ObjectId("5770ca42e90c1adc80040a08"),        "name" : "joe",        "emails" : [                "joe@example.com",                "joe@gmail.com",                "joe@yahoo.com"        ]}> db.users.update({"name":"joe","emails":{$ne:"joe@gmail.com"}},{$push:{"emails":"joe@gmail.com"}})//值在数组里面,没有改变WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })                                    --nMatched为0表示没有修改> db.users.update({"name":"joe","emails":{$ne:"joe@itpub.com"}},{$push:{"emails":"joe@itpub.com"}})//值不在数组里面,添加WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.findOne(){        "_id" : ObjectId("5770ca42e90c1adc80040a08"),        "name" : "joe",        "emails" : [                "joe@example.com",                "joe@gmail.com",                "joe@yahoo.com",                "joe@itpub.com"        ]}3.数组修改器 $addToSet--------------------------------------------------------------------------------实现的功能与$ne修改器相同,且更为方便。使用$addToSet修改器可以避免重复。> db.users.findOne(){        "_id" : ObjectId("5770ca42e90c1adc80040a08"),        "name" : "joe",        "emails" : [                "joe@example.com",                "joe@gmail.com",                "joe@yahoo.com",                "joe@itpub.com"        ]}> db.users.update({"_id":ObjectId("5770ca42e90c1adc80040a08")},{$addToSet:{"emails":"joe@gmail.com"}})--原文档里已有"joe@gmail.com",修改完也没有产生重复值WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })> db.users.findOne(){        "_id" : ObjectId("5770ca42e90c1adc80040a08"),        "name" : "joe",        "emails" : [                "joe@example.com",                "joe@gmail.com",                "joe@yahoo.com",                "joe@itpub.com"        ]}> db.users.update({"_id":ObjectId("5770ca42e90c1adc80040a08")},{$addToSet:{"emails":"joe@163.com"}})--不存在添加成功WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.findOne(){        "_id" : ObjectId("5770ca42e90c1adc80040a08"),        "name" : "joe",        "emails" : [                "joe@example.com",                "joe@gmail.com",                "joe@yahoo.com",                "joe@itpub.com",                "joe@163.com"        ]}4.数组修改器 $each--------------------------------------------------------------------------------$each数组修改器要和$addToSet修改结合起来用,可以一次添加多个不同的值。例如上面的例子中,我们一次添加多个email值。> db.users.findOne(){        "_id" : ObjectId("5770ca42e90c1adc80040a08"),        "name" : "joe",        "emails" : [                "joe@example.com",                "joe@gmail.com",                "joe@yahoo.com",                "joe@itpub.com",                "joe@163.com"        ]}> db.users.update({"_id":ObjectId("5770ca42e90c1adc80040a08")},{$addToSet:{"emails":{$each:["joe@example.com","joe@python.com","joe@php.com"]}}})--为数组添加多个键值WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.findOne(){        "_id" : ObjectId("5770ca42e90c1adc80040a08"),        "name" : "joe",        "emails" : [                "joe@example.com",                "joe@gmail.com",                "joe@yahoo.com",                "joe@itpub.com",                "joe@163.com",                "joe@python.com",                "joe@php.com"        ]}5.数组修改器 $pop--------------------------------------------------------------------------------$pop修改器从数组中删除元素,可以从数组中的任何一端删除元素,{$pop:{key:1}} 从数组末尾删除一个元素{$pop:{key:-1}} 从数组头部删除一个元素> db.users.findOne(){        "_id" : ObjectId("5770ca42e90c1adc80040a08"),        "name" : "joe",        "emails" : [                "joe@example.com",                "joe@gmail.com",                "joe@yahoo.com",                "joe@itpub.com",                "joe@163.com",                "joe@python.com",                "joe@php.com"        ]}> db.users.update({"name":"joe"},{$pop:{"emails":1}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.findOne(){        "_id" : ObjectId("5770ca42e90c1adc80040a08"),        "name" : "joe",        "emails" : [                "joe@example.com",                "joe@gmail.com",                "joe@yahoo.com",                "joe@itpub.com",                "joe@163.com",                "joe@python.com"        ]}> db.users.update({"name":"joe"},{$pop:{"emails":-1}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.findOne(){        "_id" : ObjectId("5770ca42e90c1adc80040a08"),        "name" : "joe",        "emails" : [                "joe@gmail.com",                "joe@yahoo.com",                "joe@itpub.com",                "joe@163.com",                "joe@python.com"        ]}6.数组修改器 $pull--------------------------------------------------------------------------------用来删除数组中的元素,pull可以按特定条件来删除元素,会将所有匹配到的数据全部删掉。但是删除条件只能有一个> db.users.findOne(){        "_id" : ObjectId("5770ca42e90c1adc80040a08"),        "name" : "joe",        "emails" : [                "joe@gmail.com",                "joe@yahoo.com",                "joe@itpub.com",                "joe@163.com",                "joe@python.com"        ]}> db.users.update({"name":"joe"},{$pull:{"emails":["joe@163.com","joe@itpub.com"]}})   WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })--好像不能一次删除多个,没有起作用> db.users.findOne(){        "_id" : ObjectId("5770ca42e90c1adc80040a08"),        "name" : "joe",        "emails" : [                "joe@gmail.com",                "joe@yahoo.com",                "joe@itpub.com",                "joe@163.com",                "joe@python.com"        ]}> db.users.update({"name":"joe"},{$pull:{"emails":"joe@163.com"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.update({"name":"joe"},{$pull:{"emails":"joe@itpub.com"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.users.findOne(){        "_id" : ObjectId("5770ca42e90c1adc80040a08"),        "name" : "joe",        "emails" : [                "joe@gmail.com",                "joe@yahoo.com",                "joe@python.com"        ]}7.数组的定位修改器 $--------------------------------------------------------------------------------若是数组有多个值,而我们只想对其中一部分进行操作,mongodb提供了定位操作符"$",用来定位查询文档已经匹配的元素,并进行更新,定位符只更新第一个匹配的元素。方法一:使用数组下标> db.blog.findOne(){        "_id" : ObjectId("57709da84f533aa7535d46d3"),        "title" : "a blog post",        "comments" : [                {                        "name" : "joe",                        "email" : "joe@example.com",                        "content" : "nice post"                },                {                        "name" : "bob",                        "email" : "bob@example.com",                        "content" : "good post"                }        ]}> db.blog.update({"title":"a blog post"},{$set:{"comments.1.name":"livan"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.blog.findOne(){        "_id" : ObjectId("57709da84f533aa7535d46d3"),        "title" : "a blog post",        "comments" : [                {                        "name" : "joe",                        "email" : "joe@example.com",                        "content" : "nice post"                },                {                        "name" : "livan",                        "email" : "bob@example.com",                        "content" : "good post"                }        ]}--方法二:使用$> db.blog.findOne(){        "_id" : ObjectId("57709da84f533aa7535d46d3"),        "title" : "a blog post",        "comments" : [                {                        "name" : "joe",                        "email" : "joe@example.com",                        "content" : "nice post"                },                {                        "name" : "livan",                        "email" : "livan@example.com",                        "content" : "good post"                }        ]}> db.blog.update({"comments.name":"livan"},{$set:{"comments.$.email":"bob@example.com"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.blog.update({"comments.name":"livan"},{$set:{"comments.$.name":"bob"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.blog.findOne(){        "_id" : ObjectId("57709da84f533aa7535d46d3"),        "title" : "a blog post",        "comments" : [                {                        "name" : "joe",                        "email" : "joe@example.com",                        "content" : "nice post"                },                {                        "name" : "bob",                        "email" : "bob@example.com",                        "content" : "good post"                }        ]}


0 0
原创粉丝点击