本文Demo位于JavaPractice中的MongoTemplate
环境准备
pom依赖
1 | <dependency> |
SpringBoot 依赖
1 | <parent> |
mongo配置
1 | #单服务器,无用户名密码设置 |
基本操作
增
insert(Object objectToSave)
1 | /** |
请求参数:
1 | { |
执行结果:
源码查看:
1 | //MongoTemplate 784 |
由上方源码,ensureNotIterable(objectToSave)是用来确保objectToSave不是数组或者是可遍历的对象集合,如List、Collection、Iterator。追踪源码:
1 | ... |
通过类型校验之后调用了:
1 | /* |
可以看出我们将通过**determineEntityCollectionName(objectToSave)**得到将存储在mongo的collection名称,接着调用 doInsert完成数据插入。
//主要是再继续深入源码,作者的功力略显不够,只能到此为止,有兴趣的同学可以深入看看
综上,关于Insert我们得出:
1 | 1、 保存数据的对象类型不可为**List**、**Collection**、**Iterator**; |
以下为未展示源码追踪结论:
1 | 1、 若传入String,则必须为Json格式字符串,否则将会抛出MappingException |
insert(Object objectToSave, String collectionName)
1 | /** |
请求参数:
1 | { |
执行结果:
源码查看:
与2.1.1基本一致,跳过
insert(Collection batchToSave, Class entityClass)
1 | /** |
请求参数:
1 | [ |
执行结果:
源码查看:
1 | public void insert(Collection<? extends Object> batchToSave, Class<?> entityClass) { |
需注意此处的entityClass并没有要求和batchToSave类型一致,并且此处的entityClass仅是获取将要存储的collection名称用。(结论未经试验验证,仅阅读源码推断)
尚未研究透彻,待后续……
insert(Collection batchToSave, String collectionName)
1 | /** |
请求参数:
1 | [ |
执行结果:
insertAll(Collection objectsToSave)
1 |
|
请求参数:
1 | 无 |
执行结果:
1 | /** |
请求参数:
1 | [ |
执行结果:
debug调试:
1 |
|
请求参数:
1 | 无 |
执行结果:
save(Object objectToSave)
1 | /** |
请求参数:
1 | { |
执行结果:
1 | /** |
请求参数:
1 | [ |
执行结果:
源码查看:
异常位于MappingMongoConverter的393行
1 | ... |
此处不是很明白,如果不支持,可以选择与2.1.1中的ensureNotIterable进行类型过滤,此处没有类型过滤,但是却在**(BasicDBList) dbo强转的时候抛出异常,在writeInternal方法声明的时候,已经将dbo**定位DBObject,再根据异常提示信息
java.lang.ClassCastException: com.mongodb.BasicDBObject cannot be cast to com.mongodb.BasicDBList,就让人十分不解,不知道此处支持的集合类型有哪些。
save(Object objectToSave, String collectionName)
1 | /** |
请求参数:
1 | { |
执行结果:
查
查询数据基础
- jSONArray
1
2
3
4
5
6
7
8
9
10
11
12
13/* 1 */
{
"_id" : ObjectId("5a4f2c05e6bd285cc1c3ad0b"),
"_class" : "java.util.LinkedHashMap",
"name" : "1"
}
/* 2 */
{
"_id" : ObjectId("5a4f2c05e6bd285cc1c3ad0c"),
"_class" : "java.util.LinkedHashMap",
"name" : "2"
} - jSONObject
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33/* 1 */
{
"_id" : ObjectId("5a4f1a2de6bd285cc1c3ad09"),
"_class" : "com.alibaba.fastjson.JSONObject",
"testInsertMongo1" : "func1"
}
/* 2 */
{
"_id" : ObjectId("5a5034fee6bd28102724317e"),
"_class" : "com.alibaba.fastjson.JSONObject",
"testJsonobj" : "testValue1"
}
/* 3 */
{
"_id" : ObjectId("5a5034fee6bd28102724317f"),
"_class" : "com.alibaba.fastjson.JSONObject",
"testJsonobj" : "testValue2"
}
/* 4 */
{
"_id" : ObjectId("5a503d85e6bd282163fd99dd"),
"_class" : "com.alibaba.fastjson.JSONObject",
"name" : "saveObj1"
}
/* 5 */
{
"_id" : ObjectId("5a5f078082d92d967e043d75"),
"test" : []
} - student
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
} - teacher
1
2
3
4
5
6
7/* 1 */
{
"_id" : "0987654321",
"_class" : "com.janwarlen.entity.Teacher",
"name" : "老王",
"subject" : "physics"
} - test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40/* 1 */
{
"_id" : ObjectId("59bb97ede2da6021a6cd34ef"),
"name" : "tiger",
"sex" : "male",
"type" : "east north tiger"
}
/* 2 */
{
"_id" : ObjectId("59bb9803e2da6021a6cd34f0"),
"name" : "tiger",
"sex" : "male",
"type" : "east north tiger",
"addr" : "123132132"
}
/* 3 */
{
"_id" : ObjectId("59bb981ee2da6021a6cd34f1"),
"name" : "tiger",
"sex" : "male",
"locat" : "east north tiger",
"addr" : "123132132"
}
/* 4 */
{
"_id" : ObjectId("5a4f139ae6bd285395cb44ba"),
"_class" : "com.alibaba.fastjson.JSONObject",
"test" : "testValue123"
}
/* 5 */
{
"_id" : ObjectId("5a5f04d982d92d967e043d02"),
"testSet" : [
"tiger"
]
} - test2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20/* 1 */
{
"_id" : ObjectId("5a533668e6bd2813e6efb5ac"),
"_class" : "java.util.LinkedHashMap",
"testJsonobj" : "testValue1"
}
/* 2 */
{
"_id" : ObjectId("5a533668e6bd2813e6efb5ad"),
"_class" : "java.util.LinkedHashMap",
"testJsonobj" : "testValue2"
}
/* 3 */
{
"_id" : ObjectId("5a533d7be6bd2813e6efb5ae"),
"_class" : "com.alibaba.fastjson.JSONObject",
"save" : "valueTest"
}find(Query query, Class
entityClass) 查询条件:1
2
3
4
5
6
7
8
9
public String queryMongoFunc1() {
Query query = Common.getQueryNormal(1, "小明.号");
List<Student> jsonObjects = mongoTemplate.find(query, Student.class);
JSONArray array = new JSONArray();
array.addAll(jsonObjects);
return array.toJSONString();
}查询结果:1
db.getCollection('student').find({"grade":1,"name":/小明.号/,"birth":{$lt:ISODate("2018-02-10T12:04:51.367Z"), $gt:ISODate("2018-01-09T12:04:51.367Z")}})
1
2
3
4
5
6
7
8[
{
"birth": 1515585891367,
"grade": 1,
"id": "201801102003",
"name": "小明2号"
}
]find(final Query query, Class
entityClass, String collectionName) 查询条件:1
2
3
4
5
6
7
8
9
10
public String queryMongoFunc2() {
Query query = Common.getQueryNormal(2, "小明.号");
List<JSONObject> jsonObjects = mongoTemplate.find(query, JSONObject.class, "student");
JSONArray array = new JSONArray();
array.addAll(jsonObjects);
return array.toJSONString();
}查询结果1
db.getCollection('student').find({"grade":2,"name":/小明.号/,"birth":{$lt:ISODate("2018-02-10T12:04:51.367Z"), $gt:ISODate("2018-01-09T12:04:51.367Z")}})
1
2
3
4
5
6
7
8[
{
"grade": 2,
"name": "小明1号",
"birth": 1515585891367,
"_id": "1234567890"
}
]findById(Object id, Class
entityClass) 查询条件:1
2
3
4
5
6
7
8
9
public String queryMongoFunc3() {
Student res = mongoTemplate.findById("201801102003", Student.class);
JSONArray array = new JSONArray();
array.add(res);
return array.toJSONString();
}查询结果:1
db.getCollection('student').find({"_id":"201801102003"})
1
2
3
4
5
6
7
8[
{
"birth": 1515585891367,
"grade": 1,
"id": "201801102003",
"name": "小明2号"
}
]findById(Object id, Class
entityClass, String collectionName) 查询条件:1
2
3
4
5
6
7
8
9
public String queryMongoFunc4() {
JSONObject res = mongoTemplate.findById("1234567890", JSONObject.class, "student");
JSONArray array = new JSONArray();
array.add(res);
return array.toJSONString();
}查询结果1
db.getCollection('student').find({"_id":"1234567890"})
1
2
3
4
5
6
7
8[
{
"grade": 2,
"name": "小明1号",
"birth": 1515585891367,
"_id": "1234567890"
}
]findOne(Query query, Class
entityClass, String collectionName) 查询条件:1
2
3
4
5
6
7
8
9
public String queryMongoFunc6() {
JSONObject res = mongoTemplate.findOne(Common.getQueryNormal(1, "小明.号"), JSONObject.class, "student");
JSONArray array = new JSONArray();
array.add(res);
return array.toJSONString();
}查询结果:1
db.getCollection('student').find({"grade":1,"name":/小明.号/,"birth":{$lt:ISODate("2018-02-10T12:04:51.367Z"), $gt:ISODate("2018-01-09T12:04:51.367Z")}})
1
2
3
4
5
6
7
8[
{
"birth": 1515585891367,
"grade": 1,
"id": "201801102003",
"name": "小明2号"
}
]findAll(Class
entityClass) 查询条件:1
2
3
4
5
6
7
8
public String queryMongoFunc7() {
List<Student> res = mongoTemplate.findAll(Student.class);
JSONArray array = new JSONArray();
array.addAll(res);
return array.toJSONString();
}查询结果:1
db.getCollection('student').find({})
1
2
3
4
5
6
7
8
9
10
11
12
13
14[
{
"birth": 1515585891367,
"grade": 2,
"id": "1234567890",
"name": "小明1号"
},
{
"birth": 1515585891367,
"grade": 1,
"id": "201801102003",
"name": "小明2号"
}
]findAll(Class
entityClass, String collectionName) 查询条件:1
2
3
4
5
6
7
8
9
public String queryMongoFunc8() {
List<JSONObject> res = mongoTemplate.findAll(JSONObject.class, "student");
JSONArray array = new JSONArray();
array.addAll(res);
return array.toJSONString();
}查询结果:1
db.getCollection('student').find({})
1
2
3
4
5
6
7
8
9
10
11
12
13
14[
{
"birth": 1515585891367,
"grade": 2,
"id": "1234567890",
"name": "小明1号"
},
{
"birth": 1515585891367,
"grade": 1,
"id": "201801102003",
"name": "小明2号"
}
]改
updateFirst(Query query, Update update, Class<?> entityClass)
执行结果:1
2
3
4
5
6
7
8
9
public String updateMongoFunc1() {
Update update = new Update();
update.set("grade", 1);
WriteResult writeResult = mongoTemplate.updateFirst(Common.getQueryNormal(2, "小明2号"), update, JSONObject.class);
return Common.successMsg(writeResult.getN());
}
beforeafter1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}updateFirst(final Query query, final Update update, final String collectionName)
执行结果:1
2
3
4
5
6
7
8
9
public String updateMongoFunc2() {
Update update = new Update();
update.set("grade", 2);
WriteResult student = mongoTemplate.updateFirst(Common.getQueryNormal(1, "小明2号"), update, "student");
return Common.successMsg(student.getN());
}
beforeafter1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}updateFirst(Query query, Update update, Class<?> entityClass, String collectionName)
执行结果:1
2
3
4
5
6
7
8
9
10
public String updateMongoFunc3() {
Update update = new Update();
update.set("grade", 1);
WriteResult student = mongoTemplate.updateFirst(Common.getQueryNormal(2, "小明2号"), update, Student.class,
"student");
return Common.successMsg(student.getN());
}
beforeafter1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}updateMulti(Query query, Update update, Class<?> entityClass)
执行结果:1
2
3
4
5
6
7
8
9
public String updateMongoFunc4() {
Update update = new Update();
update.set("grade", 3);
WriteResult writeResult = mongoTemplate.updateMulti(Common.getQueryNormal(1, "小明.号"), update, Student.class);
return Common.successMsg(writeResult.getN());
}
beforeafter1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 4 */
{
"_id" : "1234567892",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明3号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 5 */
{
"_id" : "1234567894",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明4号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 3,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 4 */
{
"_id" : "1234567892",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明3号",
"grade" : 3,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 5 */
{
"_id" : "1234567894",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明4号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}updateMulti(final Query query, final Update update, String collectionName)
执行结果:1
2
3
4
5
6
7
8
9
public String updateMongoFunc5() {
Update update = new Update();
update.set("grade", 4);
WriteResult writeResult = mongoTemplate.updateMulti(Common.getQueryNormal(2, "小明.号"), update, "student");
return Common.successMsg(writeResult.getN());
}
beforeafter1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 3,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 4 */
{
"_id" : "1234567892",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明3号",
"grade" : 3,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 5 */
{
"_id" : "1234567894",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明4号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 4,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 3,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 4 */
{
"_id" : "1234567892",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明3号",
"grade" : 3,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 5 */
{
"_id" : "1234567894",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明4号",
"grade" : 4,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}updateMulti(final Query query, final Update update, Class<?> entityClass, String collectionName
执行结果:1
2
3
4
5
6
7
8
9
10
public String updateMongoFunc6() {
Update update = new Update();
update.set("grade", 1);
WriteResult writeResult = mongoTemplate.updateMulti(Common.getQueryNormal(3, "小明.号"), update, Student.class,
"student");
return Common.successMsg(writeResult.getN());
}
beforeafter1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 4,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 3,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 4 */
{
"_id" : "1234567892",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明3号",
"grade" : 3,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 5 */
{
"_id" : "1234567894",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明4号",
"grade" : 4,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 4,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 4 */
{
"_id" : "1234567892",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明3号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 5 */
{
"_id" : "1234567894",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明4号",
"grade" : 4,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}upsert(Query query, Update update, Class<?> entityClass)
执行结果:1
2
3
4
5
6
7
8
9
10
public String updateMongoFunc7() {
Update update = new Update();
update.set("grade", 3);
update.set("name", "upsert1");
WriteResult upsert = mongoTemplate.upsert(Common.getQueryNormal(3, "小明2号"), update, Student.class);
return Common.successMsg(upsert.getN());
}
beforeafter1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 4,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 4,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 4 */
{
"_id" : ObjectId("5a62e4deff7f0e1bec06ac49"),
"grade" : 3,
"name" : "upsert1"
}upsert(Query query, Update update, String collectionName)
执行结果:1
2
3
4
5
6
7
8
9
10
public String updateMongoFunc8() {
Update update = new Update();
update.set("grade", 3);
update.set("name", "upsert2");
WriteResult upsert = mongoTemplate.upsert(Common.getQueryNormal(3, "小明2号"), update, "student");
return Common.successMsg(upsert.getN());
}
beforeafter1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 4,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 4 */
{
"_id" : ObjectId("5a62e4deff7f0e1bec06ac49"),
"grade" : 3,
"name" : "小明2号",
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 4,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 4 */
{
"_id" : ObjectId("5a62e4deff7f0e1bec06ac49"),
"grade" : 3,
"name" : "upsert2",
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}upsert(Query query, Update update, Class<?> entityClass, String collectionName)
执行结果:1
2
3
4
5
6
7
8
9
10
public String updateMongoFunc9() {
Update update = new Update();
update.set("grade", 3);
update.set("name", "upsert3");
WriteResult upsert = mongoTemplate.upsert(Common.getQueryNormal(3, "小明2号"), update, Student.class, "student");
return Common.successMsg(upsert.getN());
}
beforeafter1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 3,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 4 */
{
"_id" : ObjectId("5a62e4deff7f0e1bec06ac49"),
"grade" : 3,
"name" : "小明2号",
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34/* 1 */
{
"_id" : "1234567890",
"_class" : "com.janwarlen.entity.Student",
"name" : "upsert3",
"grade" : 3,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 4 */
{
"_id" : ObjectId("5a62e4deff7f0e1bec06ac49"),
"grade" : 3,
"name" : "小明2号",
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}findAndModify(Query query, Update update, Class
entityClass) 执行结果:1
2
3
4
5
6
7
8
9
public String updateMongoFunc10() {
Update update = new Update();
update.set("grade", 2);
Student andModify = mongoTemplate.findAndModify(Common.getQueryNormal(1, "小明2号"), update, Student.class);
return Common.successMsg(andModify);
}
beforeafter1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25/* 1 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明1号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : ObjectId("5a62e4deff7f0e1bec06ac49"),
"grade" : 3,
"name" : "小明2号",
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}1
2
3
4{
"res": "success"
/*接口未返回data,表示未有数据修改*/
}findAndModify(Query query, Update update, Class
entityClass, String collectionName) 执行结果:1
2
3
4
5
6
7
8
9
10
11
public String updateMongoFunc11() {
Update update = new Update();
update.set("grade", 1);
Student andModify = mongoTemplate.findAndModify(Common.getQueryNormal(2, "小明2号"), update, Student.class,
"student");
return Common.successMsg(andModify);
}
beforeafter1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25/* 1 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : ObjectId("5a62e4deff7f0e1bec06ac49"),
"grade" : 3,
"name" : "小明2号",
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36/*接口返回数据*/
{
"res": "success",
"data": {
"birth": 1515585891367,
"grade": 2,
"id": "201801102003",
"name": "小明2号"
}
}
/**mongo数据变动**/
/* 1 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : ObjectId("5a62e4deff7f0e1bec06ac49"),
"grade" : 3,
"name" : "小明2号",
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}findAndModify(Query query, Update update, FindAndModifyOptions options, Class
entityClass) 执行结果:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public String updateMongoFunc12() {
Update update = new Update();
update.set("grade", 2);
FindAndModifyOptions options = new FindAndModifyOptions();
//默认false
// options.returnNew(true); //是否返回更新后数据
options.upsert(true); //是否更新(不存在则插入)
// options.remove(true); //是否删除,为true时不会更新操作,如果returnNew为true则返回旧数据,并且删除
Student andModify = mongoTemplate.findAndModify(Common.getQueryNormal(1, "小明2号"), update, options, Student
.class);
return Common.successMsg(andModify);
}
beforeafter1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25/* 1 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 1,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : ObjectId("5a62e4deff7f0e1bec06ac49"),
"grade" : 3,
"name" : "小明2号",
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37/**接口返回数据**/
{
"res": "success",
"data": {
"birth": 1515585891367,
"grade": 1,
"id": "201801102003",
"name": "小明2号"
}
//此时看到的数据时旧数据
}
/**mongo数据变动**/
/* 1 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : ObjectId("5a62e4deff7f0e1bec06ac49"),
"grade" : 3,
"name" : "小明2号",
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}findAndModify(Query query, Update update, FindAndModifyOptions options, Class
entityClass, String collectionName) 执行结果:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public String updateMongoFunc13() {
Update update = new Update();
update.set("grade", 2);
FindAndModifyOptions options = new FindAndModifyOptions();
//默认false
// options.returnNew(true); //是否返回更新后数据
options.upsert(true); //是否更新(不存在则插入)
// options.remove(true); //是否删除,为true时不会更新操作,如果returnNew为true则返回旧数据,并且删除
Student andModify = mongoTemplate.findAndModify(Common.getQueryNormal(1, "小明2号"), update, options, Student
.class, "student");
return Common.successMsg(andModify);
}
beforeafter1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25/* 1 */
{
"_id" : "1234567891",
"_class" : "com.janwarlen.entity.Student",
"name" : "小红",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 2 */
{
"_id" : "201801102003",
"_class" : "com.janwarlen.entity.Student",
"name" : "小明2号",
"grade" : 2,
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}
/* 3 */
{
"_id" : ObjectId("5a62e4deff7f0e1bec06ac49"),
"grade" : 3,
"name" : "小明2号",
"birth" : ISODate("2018-01-10T12:04:51.367Z")
}1
2
3
4
5/**接口返回数据**/
{
"res": "success"
}
//意味着未找到需要修改数据删