假如有一个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); Listlist=new ArrayList (); 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" ] } >
|