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" ...
随机推荐
- Oracle数据库导入导出(备份还原)
一.数据库的导出 1 将数据库TEST完全导出,用户名system 密码manager 导出到D:\daochu.dmp中(全库导出) exp system/manager@TEST file=d:\ ...
- PHP安全性考虑
用户提交的数据 很多PHP 程序所存在的重大弱点并不是PHP 语言本身的问题,而是编程者的安全意识不高而导致的.因此,必须时时注意每一段代码可能存在的问题,去发现非正确数据提交时可能造成的影响. 例子 ...
- java 面向对象 — 类和对象
构造方法 1.构造器必须与类同名(如果一个源文件中有多个类,那么构造器必须与公共类同名) 2.每个类可以有一个以上的构造器 3.构造器可以有0个.1个或1个以上的参数 4.构造器没有返回值 5.构造器 ...
- mac上 自己安装的python 和 自带python 的可执行文件位置
- 走迷宫(用队列bfs并输出走的路径)
#include <iostream> #include <stack> #include <string.h> #include <stdio.h> ...
- linux下今天遇到的问题
之前由于测试需要,必须用mysql5.7的客户端, 现在由于产品完善,开始支持5.6,所以需要装5.6的客户端做测试,考虑到手工测试的效率及不可重复性,准备自动化执行原来的用例. 老的用例是用MySQ ...
- mysql-8 alter命令
当我们需要修改数据表名或者修改数据表字段时,就需要用到Mysql alter命令. 查看表结构: -- 以下2个命令是通用的 show columns from test_alter_tbl; des ...
- Oracle 取某100天的每一天的日期
SELECT TO_DATE('2016-01-01', 'yyyy-MM-dd') + ROWNUM - 1 as daylist,TO_DATE('2016-01-01', 'yyyy-MM-dd ...
- 在ubuntu 18.04下,无线网卡无驱动,连不上wifi,显示wifi没有适配器的解决方法
近来因为做东西要用到linux环境,所以自己的笔记本在win10的系统上又安装了ubuntu 18.04版本的双系统,但是安装好以后,没有无线网卡的驱动,显示wifi没有适配器等字样,很纠结,前后研究 ...
- selenium+python在mac环境上的搭建
前言 mac自带了python2.7的环境,所以在mac上安装selenium环境是非常简单的,输入2个指令就能安装好 需要安装的软件: 1.pip 2.selenium2.53.6 3.Firefo ...