假如有一个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. coredns 代理consul 运行noamd 部署的应用

    nomad 是一个方便的应用调度平台,consul 一个很不错的服务发现工具,coredns 很不错, 扩展性比较强的dns 服务器,集成起来可能做很强大的事情 我的运行环境是mac,实际情况按需部署 ...

  2. kettle modified javascript 步骤的一个例子

    例子里用到的 org.htmlparser.Parser 是一个html 的解析器,可以在 sourceforge 上下载. 这个例子使用 org.htmlparser.Parser 包来解析一个 h ...

  3. 【python】Beautiful Soup的使用

    1. Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...

  4. 【python】smtp邮件发送

    纯文本: #!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.text import MIMEText from ...

  5. HTML第三讲(选择符)

    本次课程讲CSS中的选择符 1.基本选择符 基本选择符有三个 1.标记名选择符 所谓的标记名选择符就是直接在样式中使用标记名定义,譬如以下代码: (此种选择符的特点是所有相同的标记名可以同时定义不需要 ...

  6. Java 基于UDP的类似于QQ的循环通信

    package Day10; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetS ...

  7. [html]CSS 小贴士

    CSS 中用四个伪类来定义链接的样式,分别是:a:link.a:visited.a:hover 和 a : active,例如: a:link{font-weight : bold ;text-dec ...

  8. 管理Linux服务器的用户和组(续篇)

    用户切换 新建用户 useradd命令的选项 设置用户口令 passwd命令的选项 chage命令 修改用户帐户 禁用和恢复用户帐户 禁用和恢复用户帐户- Passwd命令 禁用和恢复用户帐户-直接修 ...

  9. php接收base64编码的文件内容并保存

    <?php header('Content-type:text/html;charset=utf-8'); //读取图片文件,转换成base64编码格式 $image_file = './fac ...

  10. 3_bootsrap布局容器

    3.布局容器 BootStrap必须需要至少一个布局容器,才能为页面内容进行封装和方便的样式控制. 相当于一个画板. 帮助手册位置:全局CSS样式------->概览------->布局容 ...