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" ...
随机推荐
- 基于ffmpegSDK的开发
#include <stdio.h> #include <libavutil/avutil.h> #include <libavcodec/avcodec.h> # ...
- CF 914G Sum the Fibonacci——子集卷积
题目:http://codeforces.com/contest/914/problem/G 第一个括号可以子集卷积:第三个括号可以用 FWT 异或卷积:这样算出选两个数组成 x 的方案数:三个部分的 ...
- git回滚分支版本到指定版本
昨天提交代码时Eclipse凌乱了,本来拉了dev-20190201分支的,结果提交时竟然跑到dev分支了.为了把dev分支回滚,可以有两种方式:Eclipse和命令行. 先说简单的命令行方式,先用g ...
- ivy antlib shemalocation
<?xml version="1.0" encoding="UTF-8"?> <project name="yourproject& ...
- ASP.NET 获得当前网页名字
Code string currentFilePath = HttpContext.Current.Request.FilePath; string CurrentPageName = current ...
- 正则,以“this.Name”开头,以“;”结尾
string regex="this\\.Name(.*?);"; string regex="this\\.Name(.*?);"; 以size开头,以数字结 ...
- display:table; 也可以实现 div 始终和内包的图片大小相同
display:table; 也可以实现 div 始终和内包的图片大小相同
- android.support.v7.internal.widget.ActionBarOverlayLayout Couldn't Be Initialized
问题症状: Android Studio 1.2 (Build 141.1890965) 新建工程,自动build完成后,Layout Editor无法预览Layout文件,报错内容: Renderi ...
- Oracle数据库备份与恢复的三种方法
转自blueskys567原文Oracle数据库备份与恢复的三种方法, 2006-10. 有删改 Oracle数据库有三种标准的备份方法,它们分别是导出/导入(EXP/IMP).热备份和冷备份. 导出 ...
- js格式化时间 js格式化时间戳
一个js格式化时间和js格式化时间戳的例子. 代码:/** * 时间对象的格式化; */Date.prototype.format = function(format) { /* * eg:forma ...