mongoTemplate更新一个Document里面的数组的一个记录

转:https://www.cnblogs.com/boshen-hzb/p/7115626.html

假如有一个Document如下:

复制代码

{
    "_id" : "69bca85a-5a61-4b04-81fb-ff6a71c3802a",
    "_class" : "cn.com.chinacloud.paas.mir2.application.model.bo.ApplicationInstanceBO",
    "slug" : "shawn-shawn-mysql-1",
    "version" : "1",
    "name" : "shawn-mysql-1",
    "status" : "UNHEALTHY",
    "resourceTask" : {
        "task" : "DEPLOY",
        "taskResult" : "DEPLOY_TIMEOUT"
    },
    "totalElementNum" : 1,
    "totalCellNum" : 1,
    "subElementNum" : 1,
    "elementInstanceBOs" : [ 
        {
            "_id" : "1347580a-02a1-41a6-9d29-c78850f948b5",
            "slug" : "shawn-shawn-mysql-1-mysql",
            "name" : "mysql",
            "namespace" : "shawn",
            "status" : "STARTING",
            "totalCellNum" : 1,
            "runningCellNum" : 0,
            "imageName" : "172.16.71.199/common/mysql",
            "imageVersion" : "5.6",
            "intranetAccessURL" : [ 
                {
                    "_id" : null,
                    "address" : "shawn-mysql-1-mysql.shawn",
                    "proxyPort" : 3306,
                    "targetPort" : 3306
                }
            ],
            "internetAccessURL" : [ 
                {
                    "_id" : null,
                    "address" : "172.16.71.200",
                    "targetPort" : 3306
                }
            ]
        }
    ],
    "applicationId" : "c27dbb31-20e9-40a2-94d9-e6a2df661604",
    "dependencyInstances" : [],
    "canStart" : false,
    "canStopAndReBuild" : false
}

复制代码

如果想要更新上面的紫色的status,由于elementInstanceBOs是数组结构,想要更新具体哪一个,可以用$表示数组的下标。

      Query query = new Query(Criteria.where("elementInstanceBOs.slug").is(pSlug));
        Update update = new Update().set("elementInstanceBOs.$.status", pElementInstanceStatusEnum)
                .set("elementInstanceBOs.$.runningCellNum", runningCell);
        mongoTemplate.updateFirst(query, update, ApplicationInstanceBO.class);

在上面的语句当中,Criteria.where("elementInstanceBOs.slug").is(pSlug)选择条件返回的是整个document,但是具体更新数组里面的哪个一还是得用下标$来表示。

mongoDB中数组的其他操作:

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

1

2

3

4

5

6

7

8

9

10

11

12

> db.post.findOne({"id":1})   

{    

    "_id" : ObjectId("54a530c3ff0df3732bac1680"),    

    "id" : 1,    

    "name" "joe",    

    "age" : 21,    

    "comments" : [    

        "test1",    

        "test2"    

    ]    

}    

>

 

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

> 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"    

    ]    

}    

>

        Query query = new Query( Criteria.where("id").is(id);
        Update update = new Update().push("comments", 'test3');

 

    

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

> 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"    

    ]    

}    

>

        Query query = new Query( Criteria.where("id").is(id);
        List<String> list=new ArrayList<String>();
        list.add("test3");
        list.add("test4");
        list.add("test4");
        Update update = new Update().pushAll("comments", list);

 

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

从数组末尾删除一个值:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

> 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"    

    ]    

}

从数组开头删除一个值:   

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

> 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"    

    ]    

}    

>

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

> 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"    

    ]    

}    

>

java程序:

        Query query = new Query( Criteria.where("_id").is(id);

        Update update = new BasicUpdate("{'$pull':{'comments':'test4'}}");

        mongoTemplate.updateFirst(query, update, Post.class);

 

 

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

> 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"    

    ]    

}    

>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值