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" : "",
"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里面的数组的一个记录。的更多相关文章
- 42.输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S, 如果有多对数字的和等于S,输出两个数的乘积最小的。
输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S, 如果有多对数字的和等于S,输出两个数的乘积最小的. 这道题有很多烟雾弹: 首先如果有多对,最前面的两个数就是乘积最小的, ...
- 【C语言】求旋转数组的最小数字,输入一个递增排序的数组的一个旋转,输出其最小元素
//求旋转数组的最小数字,输入一个递增排序的数组的一个旋转,输出其最小元素 #include <stdio.h> #include <string.h> int find_mi ...
- 【c语言】输入一个递增排序的数组的一个旋转,输出旋转数组中的最小元素
//旋转数组的最小数字 //题目:把一个数组最開始的若干个元素搬到数组的末尾.我们称之为数组的旋转. //输入一个递增排序的数组的一个旋转.输出旋转数组中的最小元素. //比如:数组{3.4,5,1, ...
- 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- 剑指offer42:数组和一个数字S,输出两个数的乘积最小的
1 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输出描述: 对应每个测试案例,输出两个数,小的先输出. ...
- document.getElementsByClassName返回的是一个数组
转载自:https://www.cnblogs.com/shark1100913/p/6713327.html document.getElementsByClassName("a&qu ...
- 多动手试试,其实List类型的变量在页面上取到的值可以直接赋值给一个js的Array数组变量
多动手试试,其实List类型的变量在页面上取到的值可以直接赋值给一个js的Array数组变量,并且数组变量可以直接取到每一个元素var array1 = '<%=yearList =>'; ...
- indexOf() 如何判断一个元素在指定数组中是否存在? 找出指定元素出现的所有位置? indexOf()方法 是正序查找,lastIndexOf()是倒叙查找
indexOf()方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1. let a = [2, 9, 7, 8, 9]; a.indexOf(2); // 0 a.indexOf ...
- js 获取数组最后一个元素
当然有很多中做法 我这边就随便写几个最常用 最简单的方法把 # shift 删除数组第一个元素,并返回该元素,跟pop差不多 var a = ["aa","bb" ...
随机推荐
- C#在64位操作系统上连接Oracle的问题和解决方案
C#使用System.Data.OracleClient连接Oracle数据库.之前在WinXP上正常运行的程序移植到Windows 2008 x64上之后就连不上数据库了.错误信息如下: 尝试加载O ...
- 写时复制和fork,vfork,clone
写时复制 原理: 用了“引用计数”,会有一个变量用于保存引用的数量.当第一个类构造时,string的构造函数会根据传入的参数从堆上分配内存,当有其它类需要这块内存时,这个计数为自动累加,当有类析构时, ...
- GIT-查看config配置信息
config 配置指令 1 git config config 配置有system级别 global(用户级别) 和local(当前仓库)三个 设置先从system->global->lo ...
- 【linux】Linux系统信息查看命令大全
系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # ho ...
- java微信 客服接口-发消息 中文乱码
/** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,请求参 ...
- C语言:宽字符集操作函数(unicode编码)
C语言:宽字符集操作函数(unicode编码) 字符分类: 宽字符函数 普通C函数描述 iswalnum() isalnum() 测试字符是否为数字或字母 iswalpha() isalpha() 测 ...
- java工具类-邮件发送
mail-1.4.jar package com.huawei.it.citools.mail; import java.util.Date;import java.util.List;import ...
- 除去a标签target="_blank"的方法
用Jquery:$(function(){$("div>a").attr("target","_blank");});先查找页面上的d ...
- libevent源码学习
怎么快速学习开源库比如libevent? libevent分析 - sparkliang的专栏 - 博客频道 - CSDN.NET Libevent源码分析 - luotuo44的专栏 - 博客频道 ...
- 在ubuntu 18.04下,无线网卡无驱动,连不上wifi,显示wifi没有适配器的解决方法
近来因为做东西要用到linux环境,所以自己的笔记本在win10的系统上又安装了ubuntu 18.04版本的双系统,但是安装好以后,没有无线网卡的驱动,显示wifi没有适配器等字样,很纠结,前后研究 ...