mongodb update array (续)

由于超过最大字数,续一下

PRIMARY> db.test.update({ "skyImsi" : "aaaaaaaaaaaaaaaaaaaaaa"},{$pull:{"usrDiscountBCList":"joe"}})  --$pull使用
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"
                },
                {
                        "name" : "joe",
                        "scores" : 90
                }
        ]
}

db.test.update({ "skyImsi" : "aaaaaaaaaaaaaaaaaaaaaa"},{$pullAll:{"usrDiscountBCList":[{name:"joe",scores:90}]}}) --$pullAll使用
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"
                }
        ]
}

PRIMARY> db.test.update({ "skyImsi" : "aaaaaaaaaaaaaaaaaaaaaa"},{$rename:{"usrDiscountBCList":"mycolumn"}}) --$rename使用
PRIMARY>  db.test.findOne()
{
        "_id" : 1,
        "mycolumn" : [
                {
                        "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"
                }
        ],
        "myfied" : [
                [
                        {
                                "name" : "joe",
                                "scores" : 90
                        }
                ]
        ],
        "skyImsi" : "aaaaaaaaaaaaaaaaaaaaaa",
        "usrComboVer" : "bbbbbbbbbbbbbb"
}

 

PRIMARY> db.test.update({ "mycolumn.sourceId":1},{$unset:{"mycolumn.$.sourceType":1}})
PRIMARY> db.test.findOne()
{
        "_id" : 1,
        "mycolumn" : [
                {
                        "billingCol0" : 0,
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "contentId" : "1",
                        "effTime" : "20120124000000",
                        "expTime" : "20120223235959",
                        "sourceId" : 1,
                        "usrContentId" : "5"
                },
                {
                        "usrContentId" : "6",
                        "sourceType" : 1,
                        "sourceId" : 1,
                        "contentId" : "1",
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "billingCol0" : 0,
                        "effTime" : "20120224000000",
                        "expTime" : "20120325235959"
                }
        ],
        "myfied" : [
                [
                        {
                                "name" : "joe",
                                "scores" : 90
                        }
                ]
        ],
        "skyImsi" : "aaaaaaaaaaaaaaaaaaaaaa",
        "usrComboVer" : "bbbbbbbbbbbbbb"
}
PRIMARY> db.test.update({ "mycolumn.sourceId":1},{$unset:{"mycolumn.1.sourceType":1}})
PRIMARY>  db.test.findOne()
{
        "_id" : 1,
        "mycolumn" : [
                {
                        "billingCol0" : 0,
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "contentId" : "1",
                        "effTime" : "20120124000000",
                        "expTime" : "20120223235959",
                        "sourceId" : 1,
                        "usrContentId" : "5"
                },
                {
                        "billingCol0" : 0,
                        "billingType" : 1,
                        "billingTypeValue" : 0,
                        "contentId" : "1",
                        "effTime" : "20120224000000",
                        "expTime" : "20120325235959",
                        "sourceId" : 1,
                        "usrContentId" : "6"
                }
        ],
        "myfied" : [
                [
                        {
                                "name" : "joe",
                                "scores" : 90
                        }
                ]
        ],
        "skyImsi" : "aaaaaaaaaaaaaaaaaaaaaa",
        "usrComboVer" : "bbbbbbbbbbbbbb"
}
PRIMARY> db.t.insert({x: [1,2,3,4,3,2,3,4]})
PRIMARY> db.t.find()
{ "_id" : ObjectId("50c19a7be2e3192b739d44eb"), "x" : [ 1, 2, 3, 4, 3, 2, 3, 4 ] }
PRIMARY> db.t.update({x:3}, {$unset:{"x.$":1}})
PRIMARY> db.t.find()
{ "_id" : ObjectId("50c19a7be2e3192b739d44eb"), "x" : [ 1, 2, null, 4, 3, 2, 3, 4 ] }

以上是$和$unset一起使用的结果,有些地方还是有区别的

你可能感兴趣的:(mongodb update array (续))