DATAMONGO-944 - Add support for $currentDate to Update.
Added currentDate and currentTimestamp to Update. Original pull request: #200.
This commit is contained in:
committed by
Thomas Darimont
parent
6616d6788c
commit
d82fc22659
@@ -280,6 +280,34 @@ public class Update {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update given key to current date using {@literal $currentDate} modifier.
|
||||
*
|
||||
* @see http://docs.mongodb.org/manual/reference/operator/update/currentDate/
|
||||
* @param key
|
||||
* @return
|
||||
* @since 1.6
|
||||
*/
|
||||
public Update currentDate(String key) {
|
||||
|
||||
addMultiFieldOperation("$currentDate", key, true);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update given key to current date using {@literal $currentDate : { $type : "timestamp" }} modifier.
|
||||
*
|
||||
* @see http://docs.mongodb.org/manual/reference/operator/update/currentDate/
|
||||
* @param key
|
||||
* @return
|
||||
* @since 1.6
|
||||
*/
|
||||
public Update currentTimestamp(String key) {
|
||||
|
||||
addMultiFieldOperation("$currentDate", key, new BasicDBObject("$type", "timestamp"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public DBObject getUpdateObject() {
|
||||
|
||||
DBObject dbo = new BasicDBObject();
|
||||
|
||||
@@ -23,6 +23,9 @@ import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.BasicDBObjectBuilder;
|
||||
|
||||
/**
|
||||
* Test cases for {@link Update}.
|
||||
*
|
||||
@@ -342,4 +345,68 @@ public class UpdateTests {
|
||||
+ "\"$push\" : { \"authors\" : { \"name\" : \"Sven\"}} " //
|
||||
+ ", \"$pop\" : { \"authors\" : -1}}")); //
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-944
|
||||
*/
|
||||
@Test
|
||||
public void getUpdateObjectShouldReturnCurrentDateCorrectlyForSingleFieldWhenUsingDate() {
|
||||
|
||||
Update update = new Update().currentDate("foo");
|
||||
assertThat(update.getUpdateObject(),
|
||||
equalTo(new BasicDBObjectBuilder().add("$currentDate", new BasicDBObject("foo", true)).get()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-944
|
||||
*/
|
||||
@Test
|
||||
public void getUpdateObjectShouldReturnCurrentDateCorrectlyForMultipleFieldsWhenUsingDate() {
|
||||
|
||||
Update update = new Update().currentDate("foo").currentDate("bar");
|
||||
assertThat(update.getUpdateObject(),
|
||||
equalTo(new BasicDBObjectBuilder().add("$currentDate", new BasicDBObject("foo", true).append("bar", true))
|
||||
.get()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-944
|
||||
*/
|
||||
@Test
|
||||
public void getUpdateObjectShouldReturnCurrentDateCorrectlyForSingleFieldWhenUsingTimestamp() {
|
||||
|
||||
Update update = new Update().currentTimestamp("foo");
|
||||
assertThat(
|
||||
update.getUpdateObject(),
|
||||
equalTo(new BasicDBObjectBuilder().add("$currentDate",
|
||||
new BasicDBObject("foo", new BasicDBObject("$type", "timestamp"))).get()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-944
|
||||
*/
|
||||
@Test
|
||||
public void getUpdateObjectShouldReturnCurrentDateCorrectlyForMultipleFieldsWhenUsingTimestamp() {
|
||||
|
||||
Update update = new Update().currentTimestamp("foo").currentTimestamp("bar");
|
||||
assertThat(
|
||||
update.getUpdateObject(),
|
||||
equalTo(new BasicDBObjectBuilder().add(
|
||||
"$currentDate",
|
||||
new BasicDBObject("foo", new BasicDBObject("$type", "timestamp")).append("bar", new BasicDBObject("$type",
|
||||
"timestamp"))).get()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-944
|
||||
*/
|
||||
@Test
|
||||
public void getUpdateObjectShouldReturnCurrentDateCorrectlyWhenUsingMixedDateAndTimestamp() {
|
||||
|
||||
Update update = new Update().currentDate("foo").currentTimestamp("bar");
|
||||
assertThat(
|
||||
update.getUpdateObject(),
|
||||
equalTo(new BasicDBObjectBuilder().add("$currentDate",
|
||||
new BasicDBObject("foo", true).append("bar", new BasicDBObject("$type", "timestamp"))).get()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user