iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >【MongoDB学习笔记12】深入MongoDB的更新(update)操作:数组修改
  • 551
分享到

【MongoDB学习笔记12】深入MongoDB的更新(update)操作:数组修改

2024-04-02 19:04:59 551人浏览 安东尼
摘要

查看一个文档的一个键值comments为一个数组[“test1”,”test2”]:> db.post.findOne({"id":1})    { 

查看一个文档的一个键值comments为一个数组[“test1”,”test2”]:

> db.post.findOne({"id":1})   
{    
    "_id" : ObjectId("54a530c3ff0df3732bac1680"),    
    "id" : 1,    
    "name" : "joe",    
    "age" : 21,    
    "comments" : [    
        "test1",    
        "test2"    
    ]    
}    
>

 

一、$push向数组末尾添加元素

> db.post.update({"id":1},{$push:{"comments": "test3"}})   
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })    
> db.post.findOne({"id":1})    
{    
    "_id" : ObjectId("54a530c3ff0df3732bac1680"),    
    "id" : 1,    
    "name" : "joe",    
    "age" : 21,    
    "comments" : [    
        "test1",    
        "test2",    
        "test3"    
    ]    
}    
>

   

使用$each一次性添加多个值:

> db.post.update({"id":1},{$push:{"comments":{$each:["test4","test5","test6"]}}})   
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })    
> db.post.findOne({"id":1})    
{    
    "_id" : ObjectId("54a530c3ff0df3732bac1680"),    
    "id" : 1,    
    "name" : "joe",    
    "age" : 21,    
    "comments" : [    
        "test1",    
        "test2",    
        "test3",    
        "test4",    
        "test5",    
        "test6"    
    ]    
}    
>

二、用$pop删除数组中的元素

从数组末尾删除一个值:

> db.post.update({"id":1},{$pop:{"comments":1}})   
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })    
> db.post.findOne({"id":1})    
{    
    "_id" : ObjectId("54a530c3ff0df3732bac1680"),    
    "id" : 1,    
    "name" : "joe",    
    "age" : 21,    
    "comments" : [    
        "test1",    
        "test2",    
        "test3",    
        "test4",    
        "test5"    
    ]    
}

从数组开头删除一个值:  

> db.post.update({"id":1},{$pop:{"comments":-1}})    
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })    
> db.post.findOne({"id":1})    
{    
    "_id" : ObjectId("54a530c3ff0df3732bac1680"),    
    "id" : 1,    
    "name" : "joe",    
    "age" : 21,    
    "comments" : [    
        "test2",    
        "test3",    
        "test4",    
        "test5"    
    ]    
}    
>

三、删除数组中一个指定的值:

> db.post.update({"id":1},{$pull:{"comments":"test3"}})   
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })    
> db.post.findOne({"id":1})    
{    
    "_id" : ObjectId("54a530c3ff0df3732bac1680"),    
    "id" : 1,    
    "name" : "joe",    
    "age" : 21,    
    "comments" : [    
        "test2",    
        "test4",    
        "test5"    
    ]    
}    
>

四、基于数组下标位置修改:

> db.post.update({"id":1},{$set:{"comments.1":"test9"}})   
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })    
> db.post.findOne({"id":1})    
{    
    "_id" : ObjectId("54a530c3ff0df3732bac1680"),    
    "id" : 1,    
    "name" : "joe",    
    "age" : 21,    
    "comments" : [    
        "test2",    
        "test9",    
        "test5"    
    ]    
}    
>



您可能感兴趣的文档:

--结束END--

本文标题: 【MongoDB学习笔记12】深入MongoDB的更新(update)操作:数组修改

本文链接: https://www.lsjlt.com/news/41596.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作