|
|
|
|
@ -15,10 +15,11 @@
@@ -15,10 +15,11 @@
|
|
|
|
|
*/ |
|
|
|
|
package org.springframework.data.document.mongodb.query; |
|
|
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.LinkedHashMap; |
|
|
|
|
|
|
|
|
|
import org.springframework.dao.InvalidDataAccessApiUsageException; |
|
|
|
|
|
|
|
|
|
import com.mongodb.BasicDBObject; |
|
|
|
|
import com.mongodb.DBObject; |
|
|
|
|
|
|
|
|
|
@ -28,7 +29,7 @@ public class Update {
@@ -28,7 +29,7 @@ public class Update {
|
|
|
|
|
LAST, FIRST |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private HashMap<String, Object> criteria = new LinkedHashMap<String, Object>(); |
|
|
|
|
private HashMap<String, Object> modifierOps = new LinkedHashMap<String, Object>(); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Static factory method to create an Update using the provided key |
|
|
|
|
@ -48,7 +49,7 @@ public class Update {
@@ -48,7 +49,7 @@ public class Update {
|
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public Update set(String key, Object value) { |
|
|
|
|
criteria.put("$set", Collections.singletonMap(key, convertValueIfNecessary(value))); |
|
|
|
|
addMultiFieldOperation("$set", key, convertValueIfNecessary(value)); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -59,7 +60,7 @@ public class Update {
@@ -59,7 +60,7 @@ public class Update {
|
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public Update unset(String key) { |
|
|
|
|
criteria.put("$unset", Collections.singletonMap(key, 1)); |
|
|
|
|
addMultiFieldOperation("$unset", key, 1); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -71,7 +72,7 @@ public class Update {
@@ -71,7 +72,7 @@ public class Update {
|
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public Update inc(String key, Number inc) { |
|
|
|
|
criteria.put("$inc", Collections.singletonMap(key, inc)); |
|
|
|
|
addMultiFieldOperation("$inc", key, inc); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -83,7 +84,7 @@ public class Update {
@@ -83,7 +84,7 @@ public class Update {
|
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public Update push(String key, Object value) { |
|
|
|
|
criteria.put("$push", Collections.singletonMap(key, convertValueIfNecessary(value))); |
|
|
|
|
addMultiFieldOperation("$push", key, convertValueIfNecessary(value)); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -101,7 +102,7 @@ public class Update {
@@ -101,7 +102,7 @@ public class Update {
|
|
|
|
|
} |
|
|
|
|
DBObject keyValue = new BasicDBObject(); |
|
|
|
|
keyValue.put(key, convertedValues); |
|
|
|
|
criteria.put("$pushAll", keyValue); |
|
|
|
|
modifierOps.put("$pushAll", keyValue); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -113,7 +114,7 @@ public class Update {
@@ -113,7 +114,7 @@ public class Update {
|
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public Update addToSet(String key, Object value) { |
|
|
|
|
criteria.put("$addToSet", Collections.singletonMap(key, convertValueIfNecessary(value))); |
|
|
|
|
addMultiFieldOperation("$addToSet", key, convertValueIfNecessary(value)); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -125,7 +126,8 @@ public class Update {
@@ -125,7 +126,8 @@ public class Update {
|
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public Update pop(String key, Position pos) { |
|
|
|
|
criteria.put("$pop", Collections.singletonMap(key, (pos == Position.FIRST ? -1 : 1))); |
|
|
|
|
addMultiFieldOperation("$pop", key, |
|
|
|
|
(pos == Position.FIRST ? -1 : 1)); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -137,7 +139,7 @@ public class Update {
@@ -137,7 +139,7 @@ public class Update {
|
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public Update pull(String key, Object value) { |
|
|
|
|
criteria.put("$pull", Collections.singletonMap(key, convertValueIfNecessary(value))); |
|
|
|
|
addMultiFieldOperation("$pull", key, convertValueIfNecessary(value)); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -155,7 +157,7 @@ public class Update {
@@ -155,7 +157,7 @@ public class Update {
|
|
|
|
|
} |
|
|
|
|
DBObject keyValue = new BasicDBObject(); |
|
|
|
|
keyValue.put(key, convertedValues); |
|
|
|
|
criteria.put("$pullAll", keyValue); |
|
|
|
|
modifierOps.put("$pullAll", keyValue); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -167,18 +169,38 @@ public class Update {
@@ -167,18 +169,38 @@ public class Update {
|
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public Update rename(String oldName, String newName) { |
|
|
|
|
criteria.put("$rename", Collections.singletonMap(oldName, newName)); |
|
|
|
|
addMultiFieldOperation("$rename", oldName, newName); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public DBObject getUpdateObject() { |
|
|
|
|
DBObject dbo = new BasicDBObject(); |
|
|
|
|
for (String k : criteria.keySet()) { |
|
|
|
|
dbo.put(k, criteria.get(k)); |
|
|
|
|
for (String k : modifierOps.keySet()) { |
|
|
|
|
dbo.put(k, modifierOps.get(k)); |
|
|
|
|
} |
|
|
|
|
return dbo; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
protected void addMultiFieldOperation(String operator, String key, |
|
|
|
|
Object value) { |
|
|
|
|
Object existingValue = this.modifierOps.get(operator); |
|
|
|
|
LinkedHashMap<String, Object> keyValueMap; |
|
|
|
|
if (existingValue == null) { |
|
|
|
|
keyValueMap = new LinkedHashMap<String, Object>(); |
|
|
|
|
this.modifierOps.put(operator, keyValueMap); |
|
|
|
|
} else { |
|
|
|
|
if (existingValue instanceof LinkedHashMap) { |
|
|
|
|
keyValueMap = (LinkedHashMap<String, Object>) existingValue; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
throw new InvalidDataAccessApiUsageException("Modifier Operations should be a LinkedHashMap but was " + |
|
|
|
|
existingValue.getClass()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
keyValueMap.put(key, value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected Object convertValueIfNecessary(Object value) { |
|
|
|
|
if (value instanceof Enum) { |
|
|
|
|
return ((Enum<?>) value).name(); |
|
|
|
|
|