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));
}
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();
}
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);
}
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());
}
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);
}
Aggregations