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" ...
随机推荐
- Array.new(5, [1, 2, 3]) or Array.new(5) { [1, 2, 3] }的差别
Array.new(5, [1, 2, 3]) or Array.new(5) { [1, 2, 3] } Array.new(size, default_object) creates an arr ...
- 每天进步一点点——Linux中的线程局部存储(一)
转载请说明出处:http://blog.csdn.net/cywosp/article/details/26469435 在Linux系统中使用C/C++进行多线程编程时,我们遇到最多的就是对同 ...
- flash Timer 性能优化,每几秒间隔一次
timer.stop后timer.currentCount没有重置,timer.reset后,currentCount重置了. package game.mananger { import flash ...
- 说说 PADS Layout 中的第 20 层和 第 25层
说说 PADS Layout 中的第 20 层和 第 25层 PADA Layout 有一个不成文的说明,第 20 层和第 25 层各有各的用途. 第 20 层是 Placement Outline ...
- mysql 统计一个列不同值的数量
SELECT count(status = 0 OR NULL) AS a, count(status = 1 OR NULL) AS b, count(status = 2 OR NULL) AS ...
- python-web 创建一个输入链接生成的网站
第一步:写一个自定义程序 #coding=utf-8 import os #Python的标准库中的os模块包含普遍的操作系统功能import re #引入正则表达式对象import urllib # ...
- [转]Aspose.Words.dll 将 Word 转换成 html
用于网站上,上传 Word 文档后显示文档内容(可看作在线阅读).代码适用于 .net 2.0 或以上版本 (使用的未注册 Aspose.Words.dll 并尝试消除试用标志) 下载地址 strin ...
- charles2 重写
重写 重写功能可以重写对应的内容,主要对某些匹配请求的header.host.URL.path.query param.body.response等参数删除.修改.增加. 和断点相比,适合做长期和批量 ...
- MySql 日期函数比较
查询当天数据 select * from tab where FROM_UNIXTIME(fabutime, '%Y%m%d') = 20121217; mysql TO_DAYS(date) 函 ...
- C++ - 常用的标准库函数
写在前面 C++是一门博大精深的语言,也是最难学的一门编程语言,每一位励志学好C++的程序员都需要从基本功开始,稳扎稳打. 自从1998年C++ standard定案以后,C++程序库便有了大幅扩 ...