mongoTemplate更新一個Document里面的數組的一個記錄。


假如有一個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"    
     ]    
}    
>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM