Browse Source

DATAMONGO-630 - Support $setOnInsert update operator.

Original pull request: #70.
pull/83/head
Becca Gaspard 12 years ago committed by Oliver Gierke
parent
commit
eebd49ab8d
  1. 13
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java
  2. 28
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java

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

@ -31,6 +31,7 @@ import com.mongodb.DBObject;
* @author Thomas Risberg * @author Thomas Risberg
* @author Mark Pollack * @author Mark Pollack
* @author Oliver Gierke * @author Oliver Gierke
* @author Becca Gaspard
*/ */
public class Update { public class Update {
@ -90,6 +91,18 @@ public class Update {
return this; return this;
} }
/**
* Update using the $setOnInsert update modifier
*
* @param key
* @param value
* @return
*/
public Update setOnInsert(String key, Object value) {
addMultiFieldOperation("$setOnInsert", key, value);
return this;
}
/** /**
* Update using the $unset update modifier * Update using the $unset update modifier
* *

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2011 the original author or authors. * Copyright 2010-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,9 +20,13 @@ import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.springframework.data.mongodb.core.query.BasicUpdate;
import org.springframework.data.mongodb.core.query.Update;
/**
* Test cases for {@link Update}.
*
* @author Oliver Gierke
* @author Becca Gaspard
*/
public class UpdateTests { public class UpdateTests {
@Test @Test
@ -135,4 +139,22 @@ public class UpdateTests {
Assert.assertEquals("{ \"$inc\" : { \"size\" : 1} , \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", u Assert.assertEquals("{ \"$inc\" : { \"size\" : 1} , \"$set\" : { \"directory\" : \"/Users/Test/Desktop\"}}", u
.getUpdateObject().toString()); .getUpdateObject().toString());
} }
/**
* @see DATAMONGO-630
*/
@Test
public void testSetOnInsert() {
Update u = new Update().setOnInsert("size", 1);
Assert.assertEquals("{ \"$setOnInsert\" : { \"size\" : 1}}", u.getUpdateObject().toString());
}
/**
* @see DATAMONGO-630
*/
@Test
public void testSetOnInsertSetOnInsert() {
Update u = new Update().setOnInsert("size", 1).setOnInsert("count", 1);
Assert.assertEquals("{ \"$setOnInsert\" : { \"size\" : 1 , \"count\" : 1}}", u.getUpdateObject().toString());
}
} }

Loading…
Cancel
Save