Skip to content

Commit eebd49a

Browse files
beccagaspardodrotbohm
authored andcommitted
DATAMONGO-630 - Support $setOnInsert update operator.
Original pull request: spring-projects#70.
1 parent fb979b1 commit eebd49a

File tree

2 files changed

+38
-3
lines changed
  • spring-data-mongodb/src

2 files changed

+38
-3
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java

+13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* @author Thomas Risberg
3232
* @author Mark Pollack
3333
* @author Oliver Gierke
34+
* @author Becca Gaspard
3435
*/
3536
public class Update {
3637

@@ -90,6 +91,18 @@ public Update set(String key, Object value) {
9091
return this;
9192
}
9293

94+
/**
95+
* Update using the $setOnInsert update modifier
96+
*
97+
* @param key
98+
* @param value
99+
* @return
100+
*/
101+
public Update setOnInsert(String key, Object value) {
102+
addMultiFieldOperation("$setOnInsert", key, value);
103+
return this;
104+
}
105+
93106
/**
94107
* Update using the $unset update modifier
95108
*

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2011 the original author or authors.
2+
* Copyright 2010-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,9 +20,13 @@
2020

2121
import org.junit.Assert;
2222
import org.junit.Test;
23-
import org.springframework.data.mongodb.core.query.BasicUpdate;
24-
import org.springframework.data.mongodb.core.query.Update;
2523

24+
/**
25+
* Test cases for {@link Update}.
26+
*
27+
* @author Oliver Gierke
28+
* @author Becca Gaspard
29+
*/
2630
public class UpdateTests {
2731

2832
@Test
@@ -135,4 +139,22 @@ public void testBasicUpdateIncAndSet() {
135139
Assert.assertEquals("{ \"$inc\" : { \"size\" : 1} , \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", u
136140
.getUpdateObject().toString());
137141
}
142+
143+
/**
144+
* @see DATAMONGO-630
145+
*/
146+
@Test
147+
public void testSetOnInsert() {
148+
Update u = new Update().setOnInsert("size", 1);
149+
Assert.assertEquals("{ \"$setOnInsert\" : { \"size\" : 1}}", u.getUpdateObject().toString());
150+
}
151+
152+
/**
153+
* @see DATAMONGO-630
154+
*/
155+
@Test
156+
public void testSetOnInsertSetOnInsert() {
157+
Update u = new Update().setOnInsert("size", 1).setOnInsert("count", 1);
158+
Assert.assertEquals("{ \"$setOnInsert\" : { \"size\" : 1 , \"count\" : 1}}", u.getUpdateObject().toString());
159+
}
138160
}

0 commit comments

Comments
 (0)