假如有一个Document如下:

{
"_id" : "69bca85a-5a61-4b04-81fb-ff6a71c3802a",
"_class" : "cn.com.chinacloud.paas.mir2.application.model.bo.ApplicationInstanceBO",
"slug" : "shawn-shawn-mysql-1",
"version" : "",
"name" : "shawn-mysql-1",
"status" : "UNHEALTHY",
"resourceTask" : {
"task" : "DEPLOY",
"taskResult" : "DEPLOY_TIMEOUT"
},
"totalElementNum" : ,
"totalCellNum" : ,
"subElementNum" : ,
"elementInstanceBOs" : [
{
"_id" : "1347580a-02a1-41a6-9d29-c78850f948b5",
"slug" : "shawn-shawn-mysql-1-mysql",
"name" : "mysql",
"namespace" : "shawn",
"status" : "STARTING",
"totalCellNum" : ,
"runningCellNum" : ,
"imageName" : "172.16.71.199/common/mysql",
"imageVersion" : "5.6",
"intranetAccessURL" : [
{
"_id" : null,
"address" : "shawn-mysql-1-mysql.shawn",
"proxyPort" : ,
"targetPort" :
}
],
"internetAccessURL" : [
{
"_id" : null,
"address" : "172.16.71.200",
"targetPort" :
}
]
}
],
"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"    
    ]    
}    
>

 

mongoTemplate更新一个Document里面的数组的一个记录。的更多相关文章

  1. 42.输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S, 如果有多对数字的和等于S,输出两个数的乘积最小的。

    输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S, 如果有多对数字的和等于S,输出两个数的乘积最小的. 这道题有很多烟雾弹: 首先如果有多对,最前面的两个数就是乘积最小的, ...

  2. 【C语言】求旋转数组的最小数字,输入一个递增排序的数组的一个旋转,输出其最小元素

    //求旋转数组的最小数字,输入一个递增排序的数组的一个旋转,输出其最小元素 #include <stdio.h> #include <string.h> int find_mi ...

  3. 【c语言】输入一个递增排序的数组的一个旋转,输出旋转数组中的最小元素

    //旋转数组的最小数字 //题目:把一个数组最開始的若干个元素搬到数组的末尾.我们称之为数组的旋转. //输入一个递增排序的数组的一个旋转.输出旋转数组中的最小元素. //比如:数组{3.4,5,1, ...

  4. 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。

    // test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  5. 剑指offer42:数组和一个数字S,输出两个数的乘积最小的

    1 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输出描述: 对应每个测试案例,输出两个数,小的先输出. ...

  6. document.getElementsByClassName返回的是一个数组

    转载自:https://www.cnblogs.com/shark1100913/p/6713327.html   document.getElementsByClassName("a&qu ...

  7. 多动手试试,其实List类型的变量在页面上取到的值可以直接赋值给一个js的Array数组变量

    多动手试试,其实List类型的变量在页面上取到的值可以直接赋值给一个js的Array数组变量,并且数组变量可以直接取到每一个元素var array1 = '<%=yearList =>'; ...

  8. indexOf() 如何判断一个元素在指定数组中是否存在? 找出指定元素出现的所有位置? indexOf()方法 是正序查找,lastIndexOf()是倒叙查找

    indexOf()方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1. let a = [2, 9, 7, 8, 9]; a.indexOf(2); // 0 a.indexOf ...

  9. js 获取数组最后一个元素

    当然有很多中做法 我这边就随便写几个最常用 最简单的方法把 # shift 删除数组第一个元素,并返回该元素,跟pop差不多 var a = ["aa","bb" ...

随机推荐

  1. Loararunner录制脚本

    LoadRunner录制 1.启动LoadRunner,用管理员方式打开,选择 “Create/Edit Scripts” 2.从这两个方式任意方式打开脚本页面 3.选择协议,这里我们举例子,用“We ...

  2. Centos下telnet的安装和配置

    Centos下telnet的安装和配置 首先为Centos配置地址(192.168.0.1/24) 一.查看本机是否有安装telnetrpm -qa | grep telnetrpm -q telne ...

  3. JS 奇淫巧技 转

    1. 在 String.prototype.replace 方法中使用 /g 和 /i 标志位 令很多 JavaScript 初学者意外的是,字符串的 replace 方法并不会 替换所有匹配的子串— ...

  4. C#中XML文档注释编译DLL引用到其它项目

    引用地址:http://zhidao.baidu.com/link?url=jSGYEBysE4gBExtNsHCVk3vd2OK2cMlaf02cS79GdRuGueTBdFJB0btOdBYkg_ ...

  5. Java renameTo()重新命名此抽象路径名表示的文件

    Java手册 renameTo public boolean renameTo(File dest) 重新命名此抽象路径名表示的文件. 此方法行为的许多方面都是与平台有关的:重命名操作无法将一个文件从 ...

  6. PAT 甲级 1008 Elevator (20)(20 分)模拟水题

    题目翻译: 1008.电梯 在我们的城市里,最高的建筑物里只有一部电梯.有一份由N个正数组成的请求列表.这些数表示电梯将会以规定的顺序在哪些楼层停下.电梯升高一层需要6秒,下降一层需要4秒.每次停下电 ...

  7. Spring boot with Apache Hive

      5.29.1. Maven <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  8. 安装Android studio出现'tools.jar' seems to be not in Android Studio classpath......的解决方法

    安装Android studio出现'tools.jar' seems to be not in Android Studio classpath......的解决方法 原创 2015年07月31日 ...

  9. Changing the load order/delay the start of the Server service

    THE INFORMATION IN THIS ARTICLE APPLIES TO: Secure FTP Server (All Versions) EFT Server (All Version ...

  10. Win7+Ubuntu12.04(EasyBCD硬盘安装)

    安装双系统 Windows7 + Ubuntu12.04 软件准备 准备两个东西EasyBCD软件和iso镜像(我用的easybcd是2.2版,就下载1.7之后版就行,要那种安装版的,不要绿色版) E ...