use of org.mongodb.morphia.query.UpdateResults in project morphia by mongodb.
the class DatastoreImpl method update.
@SuppressWarnings("unchecked")
private <T> UpdateResults update(final Query<T> query, final DBObject update, final UpdateOptions options) {
DBCollection dbColl = query.getCollection();
// TODO remove this after testing.
if (dbColl == null) {
dbColl = getCollection(query.getEntityClass());
}
if (query.getSortObject() != null && query.getSortObject().keySet() != null && !query.getSortObject().keySet().isEmpty()) {
throw new QueryException("sorting is not allowed for updates.");
}
if (query.getOffset() > 0) {
throw new QueryException("a query offset is not allowed for updates.");
}
if (query.getLimit() > 0) {
throw new QueryException("a query limit is not allowed for updates.");
}
DBObject queryObject = query.getQueryObject();
final MappedClass mc = getMapper().getMappedClass(query.getEntityClass());
final List<MappedField> fields = mc.getFieldsAnnotatedWith(Version.class);
if (!fields.isEmpty()) {
final MappedField versionMF = fields.get(0);
if (update.get(versionMF.getNameToStore()) == null) {
if (!update.containsField("$inc")) {
update.put("$inc", new BasicDBObject(versionMF.getNameToStore(), 1));
} else {
((Map<String, Object>) (update.get("$inc"))).put(versionMF.getNameToStore(), 1);
}
}
}
if (LOG.isTraceEnabled()) {
LOG.trace(format("Executing update(%s) for query: %s, ops: %s, multi: %s, upsert: %s", dbColl.getName(), queryObject, update, options.isMulti(), options.isUpsert()));
}
return new UpdateResults(dbColl.update(queryObject, update, enforceWriteConcern(options, query.getEntityClass()).getOptions()));
}
use of org.mongodb.morphia.query.UpdateResults in project morphia by mongodb.
the class TestUpdateOps method shouldUpdateAnArrayElement.
@Test
public void shouldUpdateAnArrayElement() {
// given
ObjectId parentId = new ObjectId();
String childName = "Bob";
String updatedLastName = "updatedLastName";
Parent parent = new Parent();
parent.id = parentId;
parent.children.add(new Child("Anthony", "Child"));
parent.children.add(new Child(childName, "originalLastName"));
getDs().save(parent);
// when
Query<Parent> query = getDs().find(Parent.class).field("_id").equal(parentId).field("children.first").equal(childName);
UpdateOperations<Parent> updateOps = getDs().createUpdateOperations(Parent.class).set("children.$.last", updatedLastName);
UpdateResults updateResults = getDs().update(query, updateOps);
// then
assertThat(updateResults.getUpdatedCount(), is(1));
assertThat(getDs().find(Parent.class).filter("id", parentId).get().children, hasItem(new Child(childName, updatedLastName)));
}
use of org.mongodb.morphia.query.UpdateResults in project morphia by mongodb.
the class TestUpdateOps method testUpdateWithDifferentType.
@Test
public void testUpdateWithDifferentType() throws Exception {
final ContainsInt cInt = new ContainsInt();
cInt.val = 21;
getDs().save(cInt);
final UpdateResults res = getDs().update(getDs().find(ContainsInt.class), getDs().createUpdateOperations(ContainsInt.class).inc("val", 1.1D), new UpdateOptions());
assertUpdated(res, 1);
assertThat(getDs().find(ContainsInt.class).get().val, is(22));
}
use of org.mongodb.morphia.query.UpdateResults in project morphia by mongodb.
the class TestUpdateOps method testIncDec.
@Test
public void testIncDec() throws Exception {
final Rectangle[] array = { new Rectangle(1, 10), new Rectangle(1, 10), new Rectangle(1, 10), new Rectangle(10, 10), new Rectangle(10, 10) };
for (final Rectangle rect : array) {
getDs().save(rect);
}
final Query<Rectangle> heightOf1 = getDs().find(Rectangle.class).filter("height", 1D);
final Query<Rectangle> heightOf2 = getDs().find(Rectangle.class).filter("height", 2D);
final Query<Rectangle> heightOf35 = getDs().find(Rectangle.class).filter("height", 3.5D);
assertThat(getDs().getCount(heightOf1), is(3L));
assertThat(getDs().getCount(heightOf2), is(0L));
final UpdateResults results = getDs().update(heightOf1, getDs().createUpdateOperations(Rectangle.class).inc("height"));
assertUpdated(results, 3);
assertThat(getDs().getCount(heightOf1), is(0L));
assertThat(getDs().getCount(heightOf2), is(3L));
getDs().update(heightOf2, getDs().createUpdateOperations(Rectangle.class).dec("height"));
assertThat(getDs().getCount(heightOf1), is(3L));
assertThat(getDs().getCount(heightOf2), is(0L));
getDs().update(heightOf1, getDs().createUpdateOperations(Rectangle.class).inc("height", 2.5D));
assertThat(getDs().getCount(heightOf1), is(0L));
assertThat(getDs().getCount(heightOf35), is(3L));
getDs().update(heightOf35, getDs().createUpdateOperations(Rectangle.class).dec("height", 2.5D));
assertThat(getDs().getCount(heightOf1), is(3L));
assertThat(getDs().getCount(heightOf35), is(0L));
getDs().update(getDs().find(Rectangle.class).filter("height", 1D), getDs().createUpdateOperations(Rectangle.class).set("height", 1D).inc("width", 20D));
assertThat(getDs().getCount(Rectangle.class), is(5L));
assertThat(getDs().find(Rectangle.class).filter("height", 1D).get(), is(notNullValue()));
assertThat(getDs().find(Rectangle.class).filter("width", 30D).get(), is(notNullValue()));
getDs().update(getDs().find(Rectangle.class).filter("width", 30D), getDs().createUpdateOperations(Rectangle.class).set("height", 2D).set("width", 2D));
assertThat(getDs().find(Rectangle.class).filter("width", 1D).get(), is(nullValue()));
assertThat(getDs().find(Rectangle.class).filter("width", 2D).get(), is(notNullValue()));
getDs().update(heightOf35, getDs().createUpdateOperations(Rectangle.class).dec("height", 1));
getDs().update(heightOf35, getDs().createUpdateOperations(Rectangle.class).dec("height", Long.MAX_VALUE));
getDs().update(heightOf35, getDs().createUpdateOperations(Rectangle.class).dec("height", 1.5f));
getDs().update(heightOf35, getDs().createUpdateOperations(Rectangle.class).dec("height", Double.MAX_VALUE));
try {
getDs().update(heightOf35, getDs().createUpdateOperations(Rectangle.class).dec("height", new AtomicInteger(1)));
fail("Wrong data type not recognized.");
} catch (IllegalArgumentException ignore) {
}
}
use of org.mongodb.morphia.query.UpdateResults in project morphia by mongodb.
the class VersionTest method testVersionsWithUpdate.
@Test
public void testVersionsWithUpdate() {
final ALongPrimitive initial = new ALongPrimitive();
Datastore ds = getDs();
ds.save(initial);
Query<ALongPrimitive> query = ds.find(ALongPrimitive.class).field("id").equal(initial.getId());
UpdateOperations<ALongPrimitive> update = ds.createUpdateOperations(ALongPrimitive.class).set("text", "some new value");
UpdateResults results = ds.update(query, update);
assertEquals(1, results.getUpdatedCount());
ALongPrimitive postUpdate = ds.get(ALongPrimitive.class, initial.getId());
Assert.assertEquals(initial.version + 1, postUpdate.version);
}
Aggregations