Search in sources :

Example 6 with UpdateResults

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()));
}
Also used : MappedField(org.mongodb.morphia.mapping.MappedField) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) QueryException(org.mongodb.morphia.query.QueryException) MappedClass(org.mongodb.morphia.mapping.MappedClass) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) UpdateResults(org.mongodb.morphia.query.UpdateResults)

Example 7 with UpdateResults

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)));
}
Also used : ObjectId(org.bson.types.ObjectId) UpdateResults(org.mongodb.morphia.query.UpdateResults) Test(org.junit.Test)

Example 8 with UpdateResults

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));
}
Also used : UpdateResults(org.mongodb.morphia.query.UpdateResults) Test(org.junit.Test)

Example 9 with UpdateResults

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) {
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Rectangle(org.mongodb.morphia.testmodel.Rectangle) UpdateResults(org.mongodb.morphia.query.UpdateResults) Test(org.junit.Test)

Example 10 with UpdateResults

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);
}
Also used : Datastore(org.mongodb.morphia.Datastore) UpdateResults(org.mongodb.morphia.query.UpdateResults) Test(org.junit.Test)

Aggregations

UpdateResults (org.mongodb.morphia.query.UpdateResults)16 Test (org.junit.Test)10 BasicDBObject (com.mongodb.BasicDBObject)5 DBObject (com.mongodb.DBObject)4 DBCollection (com.mongodb.DBCollection)3 LinkedHashMap (java.util.LinkedHashMap)3 MappedClass (org.mongodb.morphia.mapping.MappedClass)3 MappedField (org.mongodb.morphia.mapping.MappedField)3 WriteResult (com.mongodb.WriteResult)2 Date (java.util.Date)2 Datastore (org.mongodb.morphia.Datastore)2 MongoClient (com.mongodb.MongoClient)1 DBCollectionUpdateOptions (com.mongodb.client.model.DBCollectionUpdateOptions)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ObjectId (org.bson.types.ObjectId)1 Ignore (org.junit.Ignore)1 Morphia (org.mongodb.morphia.Morphia)1