Search in sources :

Example 11 with UpdateOptions

use of dev.morphia.UpdateOptions in project morphia by mongodb.

the class TestUpdateOperations method testSetOnInsertWhenInserting.

@Test
public void testSetOnInsertWhenInserting() {
    ObjectId id = new ObjectId();
    Query<Circle> query = getDs().find(Circle.class).filter(eq("id", id));
    assertInserted(query.update(setOnInsert(Map.of("radius", 2D))).execute(new UpdateOptions().upsert(true)));
    final Circle updatedCircle = getDs().find(Circle.class).filter(eq("_id", id)).first();
    assertThat(updatedCircle, is(notNullValue()));
    MatcherAssert.assertThat(updatedCircle.getRadius(), is(2D));
}
Also used : Circle(dev.morphia.test.models.Circle) ObjectId(org.bson.types.ObjectId) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 12 with UpdateOptions

use of dev.morphia.UpdateOptions in project morphia by mongodb.

the class TestUpdateOperations method testSetUnset.

@Test
public void testSetUnset() {
    Datastore ds = getDs();
    final ObjectId key = ds.save(new Circle(1)).getId();
    Query<Circle> circle = ds.find(Circle.class).filter(eq("radius", 1D));
    assertUpdated(circle.update(set("radius", 2D)).execute(), 1);
    Query<Circle> idQuery = ds.find(Circle.class).filter(eq("_id", key));
    MatcherAssert.assertThat(idQuery.first().getRadius(), is(2D));
    circle = ds.find(Circle.class).filter(eq("radius", 2D));
    assertUpdated(circle.update(unset("radius")).execute(new UpdateOptions().multi(false)), 1);
    MatcherAssert.assertThat(idQuery.first().getRadius(), is(0D));
    Book article = new Book();
    ds.save(article);
    Query<Book> query = ds.find(Book.class);
    query.update(set("title", "Some Title")).execute();
    query.update(unset("title")).execute();
}
Also used : Circle(dev.morphia.test.models.Circle) Datastore(dev.morphia.Datastore) ObjectId(org.bson.types.ObjectId) Book(dev.morphia.test.models.Book) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 13 with UpdateOptions

use of dev.morphia.UpdateOptions in project morphia by mongodb.

the class TestVersioning method testIncVersionNotOverridingOtherInc.

@Test
public void testIncVersionNotOverridingOtherInc() {
    final Versioned version1 = new Versioned();
    version1.setCount(0);
    getDs().save(version1);
    assertEquals(version1.getVersion(), Long.valueOf(1));
    assertEquals(version1.getCount(), 0);
    Query<Versioned> query = getDs().find(Versioned.class);
    query.filter(eq("_id", version1.getId()));
    query.update(inc("count")).execute(new UpdateOptions().upsert(true));
    final Versioned version2 = getDs().find(Versioned.class).filter(eq("_id", version1.getId())).first();
    assertEquals(version2.getVersion(), Long.valueOf(2));
    assertEquals(version2.getCount(), 1);
}
Also used : Versioned(dev.morphia.test.models.versioned.Versioned) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 14 with UpdateOptions

use of dev.morphia.UpdateOptions in project morphia by mongodb.

the class TestDocumentValidation method update.

@Test
@SuppressWarnings("rawtypes")
public void update() {
    getMapper().map(DocumentValidation.class);
    getDs().enableDocumentValidation();
    getDs().save(new DocumentValidation("Harold", 100, new Date()));
    Query<DocumentValidation> query = getDs().find(DocumentValidation.class);
    UpdateOptions options = new UpdateOptions().bypassDocumentValidation(false);
    Update<DocumentValidation> update = query.update(set("number", 5));
    try {
        update.execute(options);
        fail("Document validation should have complained");
    } catch (MongoWriteException e) {
    // expected
    }
    options.bypassDocumentValidation(true);
    update.execute(options);
    Assert.assertNotNull(query.filter(eq("number", 5)).iterator(new FindOptions().limit(1)).tryNext());
}
Also used : FindOptions(dev.morphia.query.FindOptions) DocumentValidation(dev.morphia.test.models.DocumentValidation) MongoWriteException(com.mongodb.MongoWriteException) Date(java.util.Date) LocalDate(java.time.LocalDate) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 15 with UpdateOptions

use of dev.morphia.UpdateOptions in project morphia by mongodb.

the class TestVersioning method testVersionedUpsert.

@Test
public void testVersionedUpsert() {
    final Datastore datastore = getDs();
    datastore.find(Versioned.class).delete(new DeleteOptions().multi(true));
    Versioned entity = new Versioned();
    entity.setName("Value 1");
    Query<Versioned> query = datastore.find(Versioned.class);
    query.filter(eq("name", "Value 1"));
    query.update(set("name", "Value 3")).execute(new UpdateOptions().upsert(true));
    entity = datastore.find(Versioned.class).iterator(new FindOptions().limit(1)).tryNext();
    assertEquals(entity.getName(), "Value 3");
    assertEquals(entity.getVersion().longValue(), 1);
}
Also used : FindOptions(dev.morphia.query.FindOptions) DeleteOptions(dev.morphia.DeleteOptions) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) Versioned(dev.morphia.test.models.versioned.Versioned) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Aggregations

UpdateOptions (dev.morphia.UpdateOptions)16 Test (org.testng.annotations.Test)15 FindOptions (dev.morphia.query.FindOptions)7 Circle (dev.morphia.test.models.Circle)5 ObjectId (org.bson.types.ObjectId)5 UpdateResult (com.mongodb.client.result.UpdateResult)4 Datastore (dev.morphia.Datastore)4 LocalDate (java.time.LocalDate)3 Date (java.util.Date)3 DeleteOptions (dev.morphia.DeleteOptions)2 UpdateOperators.currentDate (dev.morphia.query.experimental.updates.UpdateOperators.currentDate)2 User (dev.morphia.test.models.User)2 Versioned (dev.morphia.test.models.versioned.Versioned)2 Calendar (java.util.Calendar)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 MongoWriteException (com.mongodb.MongoWriteException)1 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)1 Book (dev.morphia.test.models.Book)1 DocumentValidation (dev.morphia.test.models.DocumentValidation)1 FacebookUser (dev.morphia.test.models.FacebookUser)1