MongoDB 更新篇

上一篇文档,我们说了基本的MongoDB命令。接下来,我们要重点写一下,更新的操作。MongoDB的更新非常多样灵活,基本上可以应对日常上大部分的使用场景,下面我将会一一细说。


1、基本的文档替换更新

直接通过update方法更新整份文档

db.product.update({product_name:"iPhoneX"},
{product_name:"iPhoneX",price:9100,description:"一台贵到666的手机",product_number:"9830829131",brand:"苹果"});


2、$inc 修改器

刚刚那个商品记录,其实我们只希望调整价格而已。但是我们却需要更新一份文档,事实我们可以使用$inc修改器去调整价格

db.product.update({product_name:"iPhoneX"},{$inc:{"price":500}});

通过$inc 修改器为price属性字段增加500块,当然如果我们需要减价199也可以使用负数进行递减:

db.product.update({product_name:"iPhoneX"},{$inc:{price:-199}});


3、$set修改器

$set修改器可以针对文档中某个字段进行修改,例如我们可以修改刚刚那个文档的brand,我们改成Apple

db.product.update({product_name:"iPhoneX"},{$set:{"brand":"Apple"}});

其实我们还可以通过$set修改器,添加新的字段:

db.product.update({product_name:"iPhoneX"},{$set:{"size":"143.6 X 70.9"}});

修改结果如下:

{ 
    "_id" :ObjectId("59e47fdb203e071a1b02e544"), 
    "product_name" : "iPhoneX", 
    "price" :9499.0, 
    "description" :"一台贵到666的手机", 
    "product_number" : "9830829131", 
    "brand" :"Apple", 
    "size" :"143.6 X 70.9"
}

当然我们也可以设置内嵌的文档,例如我希望添加最近一条的评论。

var newComment = {comment_content:"TEST NEW COMMENT",user_id:109382,create_date:new Date()};
db.product.update({product_name:"iPhoneX"},{$set:{newest_commment:newComment}});
db.product.findOne();
db.product.update({product_name:"iPhoneX"},{$set:{"newest_commment.comment_content":"this is newtest comment at that place!"}});
db.product.findOne();
需要注意的是,之前我一直没有再key当中打双引号是不规范的,如果需要调用内嵌文档(内嵌对象)的时候必须要在key写上双引号,不然无法执行。

结果如下:

{ 
    "_id" : ObjectId("59e47fdb203e071a1b02e544"), 
    "product_name" : "iPhoneX", 
    "price" : 9499.0, 
    "description" : "一台贵到666的手机", 
    "product_number" : "9830829131", 
    "brand" : "Apple", 
    "size" : "143.6 X 70.9", 
    "newest_commment" : {
        "comment_content" : "this is newtest comment at that place!", 
        "user_id" : 109382.0, 
        "create_date" : ISODate("2017-10-17T01:15:20.466+0000")
    }
}


4、数组修改器

我们希望在商品上添加规格参数,我们可以数组的方式对规格对象进行文档内嵌。但是规格会有多个,我们可以在文档中通过数组类型进行存储

我们可以使用$push 为文档中的数组属性的结尾添加一个新的item,如果$push的属性不存在,就会创建一个数组并添加你要添加的数据:

var iphonexSku64GSilver={capacity:"64G",style:"silver"};
var iphonexSku64GGray={capacity:"64G",style:"gray"};
db.product.update({product_name:"iPhoneX"},{$push:{sku:iphonexSku64GSilver}});
db.product.update({product_name:"iPhoneX"},{$push:{sku:iphonexSku64GGray}});
db.product.findOne();
结果:

{ 
    "_id" : ObjectId("59e47fdb203e071a1b02e544"), 
    "product_name" : "iPhoneX", 
    "price" : 9300.0, 
    "description" : "一台贵到666的手机", 
    "product_number" : "9830829131", 
    "brand" : "Apple", 
    "size" : "143.6 X 70.9", 
    "newest_commment" : {
        "comment_content" : "this is newtest comment at that place!", 
        "user_id" : 109382.0, 
        "create_date" : ISODate("2017-10-17T01:15:20.466+0000")
    }, 
    "sku" : [
        {
            "capacity" : "64G", 
            "style" : "silver"
        }, 
        {
            "capacity" : "64G", 
            "style" : "gray"
        }
    ]
}
如果我们需要连续添加两个item的话,我们还需要借助$each这个子操作符

下面我们就通过$push 和 $each 配合连续添加连个SKU,这次是256G的

db.product.update({product_name:"iPhoneX"},{$push:{sku:{$each:[iphonexSku256GSilver,iphonexSku256GGray]}}});
db.product.findOne();
结果如下:

{ 
    "_id" : ObjectId("59e47fdb203e071a1b02e544"), 
    "product_name" : "iPhoneX", 
    "price" : 9300.0, 
    "description" : "一台贵到666的手机", 
    "product_number" : "9830829131", 
    "brand" : "Apple", 
    "size" : "143.6 X 70.9", 
    "newest_commment" : {
        "comment_content" : "this is newtest comment at that place!", 
        "user_id" : 109382.0, 
        "create_date" : ISODate("2017-10-17T01:15:20.466+0000")
    }, 
    "sku" : [
        {
            "capacity" : "64G", 
            "style" : "silver"
        }, 
        {
            "capacity" : "64G", 
            "style" : "gray"
        }, 
        {
            "capacity" : "256G", 
            "style" : "silver"
        }, 
        {
            "capacity" : "256G", 
            "style" : "gray"
        }
    ]
}
有时我们希望$each添加数组的时候,希望得到长度的限制,例如$each 里面有5个item,但是我只需要3个item。为了我越界,我们可以通过$slice保留最后3个item,而且我们还可以使用$sort对数组进行先排序然后再截取最后3个item。

var commentA = {content:"Comment A content !",user_id:399,popularity_degree:89};
var commentB = {content:"Comment B content !",user_id:349,popularity_degree:39};
var commentC = {content:"Comment C content !",user_id:119,popularity_degree:99};
var commentD = {content:"Comment D content !",user_id:334,popularity_degree:76};
var commentE = {content:"Comment E content !",user_id:893,popularity_degree:103};
var commentArray = [commentA,commentB,commentC,commentD,commentE];
db.product.update({product_name:"iPhoneX"},
	{$push:
	  {popular_comment:
	    	{$each:commentArray,
	    	  $slice:-3,
	    	  $sort:{"popularity_degree":1}
	    	}
	  }
	 });
db.product.findOne();
插入结果:

{ 
    "_id" : ObjectId("59e47fdb203e071a1b02e544"), 
    "product_name" : "iPhoneX", 
    "price" : 9300.0, 
    "description" : "一台贵到666的手机", 
    "product_number" : "9830829131", 
    "brand" : "Apple", 
    "size" : "143.6 X 70.9", 
    "newest_commment" : {
        "comment_content" : "this is newtest comment at that place!", 
        "user_id" : 109382.0, 
        "create_date" : ISODate("2017-10-17T01:15:20.466+0000")
    }, 
    "sku" : [
        {
            "capacity" : "64G", 
            "style" : "silver"
        }, 
        {
            "capacity" : "64G", 
            "style" : "gray"
        }, 
        {
            "capacity" : "256G", 
            "style" : "silver"
        }, 
        {
            "capacity" : "256G", 
            "style" : "gray"
        }
    ], 
    "popular_comment" : [
        {
            "content" : "Comment A content !", 
            "user_id" : 399.0, 
            "popularity_degree" : 89.0
        }, 
        {
            "content" : "Comment C content !", 
            "user_id" : 119.0, 
            "popularity_degree" : 99.0
        }, 
        {
            "content" : "Comment E content !", 
            "user_id" : 893.0, 
            "popularity_degree" : 103.0
        }
    ]
}

5、数组作为SET集合

通常我们也有使用SET集合的场景,例如keyword,不希望每次添加的商品关键字会有重复,这样不利于查询。但是MongoDB只有数组类型,所以我们需要借助$addToSet去完成这一任务。

db.product.update({product_name:"iPhoneX"},{$addToSet:{keyword:"iphone"}});
db.product.findOne();
db.product.update({product_name:"iPhoneX"},{$addToSet:{keyword:{$each:["iphone","苹果","apple"]}}});
db.product.findOne();
结果:

{ 
    "_id" : ObjectId("59e47fdb203e071a1b02e544"), 
    "product_name" : "iPhoneX", 
    "price" : 9300.0, 
    "description" : "一台贵到666的手机", 
    "product_number" : "9830829131", 
    "brand" : "Apple", 
    "size" : "143.6 X 70.9", 
    "newest_commment" : {
        "comment_content" : "this is newtest comment at that place!", 
        "user_id" : 109382.0, 
        "create_date" : ISODate("2017-10-17T01:15:20.466+0000")
    }, 
    "sku" : [
        {
            "capacity" : "64G", 
            "style" : "silver"
        }, 
        {
            "capacity" : "64G", 
            "style" : "gray"
        }, 
        {
            "capacity" : "256G", 
            "style" : "silver"
        }, 
        {
            "capacity" : "256G", 
            "style" : "gray"
        }
    ], 
    "popular_comment" : [
        {
      
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值