mongodb update array

之前一篇介绍了update的基本操作,今天介绍涉及更深一些的操作,数组操作以及一些函数的使用

$inc 对指定的字段增加值,只能操作数值类型 语法:{ $inc : { field : value } }给field增加value

$set 这个相当于SQL的set操作,指定某个字段的值,可以操作所有类型

$unset  语法:{ $unset : { field : 1} } 删除某一个字段
$push 语法:{ $push : { field : value } }  如果field存在,那么valuse追加到field,field一定要是一个数组,不存在,那么新增这个field

$pushAll 同上,只是这里可以追加的value是数组

$addToSet 语法:{ $addToSet : { field : value } } 如果field存在,并且value不在其中,那么把value加到数组,如果不存在field,把value赋值给field,这里操作的也是数组

$pop  语法:{ $pop : { field : 1 } }和{ $pop : { field : -1 } } 分别是删除最后一个元素和第一个元素

$pull 语法:$pull : { field : value } } 从field删除等于value的元素

$pullAll 语法:{ $pullAll : { field : value_array } } 意思同$pull,但是可以删除多个值

$rename 语法:{ $rename : { old_field_name : new_field_name } 重命名字段名

另外介绍一个$操作符,加上这个操作符,只会应用匹配第一条,后面的不再操作,另外和$unset操作的时候不是删除了,而是会留下一个null值,如果要删除null的记录,可以使用{$pull:{x:null}}

下面举例演示:

PRIMARY> db.test.find()
{ "_id" : ObjectId("50c1872fe2e3192b739d44e7"), "id" : 1, "name" : "hank", "scores" : 80 }
PRIMARY> db.test.update({id:1},{$inc:{scores:5}});                     --$inc使用
PRIMARY> db.test.find()
{ "_id" : ObjectId("50c1872fe2e3192b739d44e7"), "id" : 1, "name" : "hank", "scores" : 85 }
PRIMARY> db.test.update({id:1},{$set:{scores:60}});                  --$set使用
PRIMARY> db.test.find()
{ "_id" : ObjectId("50c1872fe2e3192b739d44e7"), "id" : 1, "name" : "hank", "scores" : 60 }
PRIMARY> db.test.update({id:1},{$unset:{scores:60}});              --$unset使用
PRIMARY>  db.test.find()
{ "_id" : ObjectId("50c1872fe2e3192b739d44e7"), "id" : 1, "name" : "hank" }

上面几个操作可以是数组里面的操作

PRIMARY> db.test.findOne()
{
        "_id" : 1,
        "skyImsi" : "aaaaaaaaaaaaaaaaaaaaaa",
        "usrComboVer" : "bbbbbbbbbbbbbb",
        "usrDiscountBCList" : [
                {
                        "usrContentId" : "4",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20111224000000",
                        "expTime" : "20120123235959"
                },
                {
                        "usrContentId" : "5",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20120124000000",
                        "expTime" : "20120223235959"
                },
                {
                        "usrContentId" : "6",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20120224000000",
                        "expTime" : "20120325235959"
                }
        ]
}

PRIMARY> db.test.update({ "usrDiscountBCList.usrContentId":"4"},{$push:{"usrDiscountBCList":"joe"}})    --$push使用
PRIMARY> db.test.findOne()
{
        "_id" : 1,
        "skyImsi" : "aaaaaaaaaaaaaaaaaaaaaa",
        "usrComboVer" : "bbbbbbbbbbbbbb",
        "usrDiscountBCList" : [
                {
                        "usrContentId" : "4",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20111224000000",
                        "expTime" : "20120123235959"
                },
                {
                        "usrContentId" : "5",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20120124000000",
                        "expTime" : "20120223235959"
                },
                {
                        "usrContentId" : "6",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20120224000000",
                        "expTime" : "20120325235959"
                },
                "joe"
        ]
}

db.test.update({ "usrDiscountBCList.usrContentId":"4"},{$pushAll:{"usrDiscountBCList":[{name:"joe",scores:90}]}})  --$pushAll使用
db.test.findOne()
{
        "_id" : 1,
        "skyImsi" : "aaaaaaaaaaaaaaaaaaaaaa",
        "usrComboVer" : "bbbbbbbbbbbbbb",
        "usrDiscountBCList" : [
                {
                        "usrContentId" : "4",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20111224000000",
                        "expTime" : "20120123235959"
                },
                {
                        "usrContentId" : "5",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20120124000000",
                        "expTime" : "20120223235959"
                },
                {
                        "usrContentId" : "6",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20120224000000",
                        "expTime" : "20120325235959"
                },
                "joe",
                {
                        "name" : "joe",
                        "scores" : 90
                }
        ]
}

db.test.update({ "usrDiscountBCList.usrContentId":"4"},{$addToSet:{"usrDiscountBCList":[{name:"joe",scores:90}]}})--$addToSet使用
db.test.findOne()
{
        "_id" : 1,
        "skyImsi" : "aaaaaaaaaaaaaaaaaaaaaa",
        "usrComboVer" : "bbbbbbbbbbbbbb",
        "usrDiscountBCList" : [
                {
                        "usrContentId" : "4",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20111224000000",
                        "expTime" : "20120123235959"
                },
                {
                        "usrContentId" : "5",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20120124000000",
                        "expTime" : "20120223235959"
                },
                {
                        "usrContentId" : "6",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20120224000000",
                        "expTime" : "20120325235959"
                },
                "joe",
                {
                        "name" : "joe",
                        "scores" : 90
                },
                [
                        {
                                "name" : "joe",
                                "scores" : 90
                        }
                ]
        ]
}

这里如果重复执行以上语句,那么不会再有操作,和原来是一致的,因为$addToSet如果存在,那么不做操作,不存在有相关字段那么添加

PRIMARY> db.test.update({ "usrDiscountBCList.usrContentId":"4"},{$addToSet:{"myfied":[{name:"joe",scores:90}]}})
PRIMARY>  db.test.findOne()
{
        "_id" : 1,
        "myfied" : [
                [
                        {
                                "name" : "joe",
                                "scores" : 90
                        }
                ]
        ],
        "skyImsi" : "aaaaaaaaaaaaaaaaaaaaaa",
        "usrComboVer" : "bbbbbbbbbbbbbb",
        "usrDiscountBCList" : [
                {
                        "usrContentId" : "4",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20111224000000",
                        "expTime" : "20120123235959"
                },
                {
                        "usrContentId" : "5",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20120124000000",
                        "expTime" : "20120223235959"
                },
                {
                        "usrContentId" : "6",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20120224000000",
                        "expTime" : "20120325235959"
                },
                "joe",
                {
                        "name" : "joe",
                        "scores" : 90
                },
                [
                        {
                                "name" : "joe",
                                "scores" : 90
                        }
                ]
        ]
}

PRIMARY> db.test.update({ "usrDiscountBCList.usrContentId":"4"},{$pop:{"usrDiscountBCList":1}})   --$pop使用 删除最后一个
PRIMARY> db.test.findOne()
{
        "_id" : 1,
        "myfied" : [
                [
                        {
                                "name" : "joe",
                                "scores" : 90
                        }
                ]
        ],
        "skyImsi" : "aaaaaaaaaaaaaaaaaaaaaa",
        "usrComboVer" : "bbbbbbbbbbbbbb",
        "usrDiscountBCList" : [
                {
                        "usrContentId" : "4",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20111224000000",
                        "expTime" : "20120123235959"
                },
                {
                        "usrContentId" : "5",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20120124000000",
                        "expTime" : "20120223235959"
                },
                {
                        "usrContentId" : "6",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20120224000000",
                        "expTime" : "20120325235959"
                },
                "joe",
                {
                        "name" : "joe",
                        "scores" : 90
                }
        ]
}

PRIMARY> db.test.update({ "usrDiscountBCList.usrContentId":"4"},{$pop:{"usrDiscountBCList":-1}})   --$pop 删除满足条件第一个元素
PRIMARY> db.test.findOne()
{
        "_id" : 1,
        "myfied" : [
                [
                        {
                                "name" : "joe",
                                "scores" : 90
                        }
                ]
        ],
        "skyImsi" : "aaaaaaaaaaaaaaaaaaaaaa",
        "usrComboVer" : "bbbbbbbbbbbbbb",
        "usrDiscountBCList" : [
                {
                        "usrContentId" : "5",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20120124000000",
                        "expTime" : "20120223235959"
                },
                {
                        "usrContentId" : "6",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20120224000000",
                        "expTime" : "20120325235959"
                },
                "joe",
                {
                        "name" : "joe",
                        "scores" : 90
                }
        ]

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值