Search in sources :

Example 1 with UpdateOptions

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

the class TestUpdateOperations method testIncDec.

@Test
public void testIncDec() {
    final Rectangle[] array = { new Rectangle(1, 10), new Rectangle(1, 10), new Rectangle(1, 10), new Rectangle(10, 10), new Rectangle(10, 10) };
    for (Rectangle rect : array) {
        getDs().save(rect);
    }
    final Query<Rectangle> heightOf1 = getDs().find(Rectangle.class).filter(eq("height", 1D));
    final Query<Rectangle> heightOf2 = getDs().find(Rectangle.class).filter(eq("height", 2D));
    final Query<Rectangle> heightOf35 = getDs().find(Rectangle.class).filter(eq("height", 3.5D));
    assertThat(heightOf1.count(), is(3L));
    assertThat(heightOf2.count(), is(0L));
    UpdateResult results = heightOf1.update(inc("height")).execute(new UpdateOptions().multi(true));
    assertUpdated(results, 3);
    assertThat(heightOf1.count(), is(0L));
    assertThat(heightOf2.count(), is(3L));
    heightOf2.update(dec("height")).execute(new UpdateOptions().multi(true));
    assertThat(heightOf1.count(), is(3L));
    assertThat(heightOf2.count(), is(0L));
    heightOf1.update(inc("height", 2.5D)).execute(new UpdateOptions().multi(true));
    assertThat(heightOf1.count(), is(0L));
    assertThat(heightOf35.count(), is(3L));
    heightOf35.update(dec("height", 2.5D)).execute(new UpdateOptions().multi(true));
    assertThat(heightOf1.count(), is(3L));
    assertThat(heightOf35.count(), is(0L));
    getDs().find(Rectangle.class).filter(eq("height", 1D)).update(set("height", 1D), inc("width", 20D)).execute();
    MatcherAssert.assertThat(getDs().find(Rectangle.class).count(), is(5L));
    MatcherAssert.assertThat(getDs().find(Rectangle.class).filter(eq("height", 1D)).iterator(new FindOptions().limit(1)).next(), is(notNullValue()));
    MatcherAssert.assertThat(getDs().find(Rectangle.class).filter(eq("width", 30D)).iterator(new FindOptions().limit(1)).next(), is(notNullValue()));
    getDs().find(Rectangle.class).filter(eq("width", 30D)).update(set("height", 2D), set("width", 2D)).execute();
    MatcherAssert.assertThat(getDs().find(Rectangle.class).filter(eq("width", 1D)).iterator(new FindOptions().limit(1)).tryNext(), is(nullValue()));
    MatcherAssert.assertThat(getDs().find(Rectangle.class).filter(eq("width", 2D)).iterator(new FindOptions().limit(1)).next(), is(notNullValue()));
    heightOf35.update(dec("height", 1)).execute();
    heightOf35.update(dec("height", Long.MAX_VALUE)).execute();
    heightOf35.update(dec("height", 1.5f)).execute();
    heightOf35.update(dec("height", Double.MAX_VALUE)).execute();
    try {
        heightOf35.update(dec("height", new AtomicInteger(1)));
        Assert.fail("Wrong data type not recognized.");
    } catch (IllegalArgumentException ignore) {
    }
}
Also used : FindOptions(dev.morphia.query.FindOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Rectangle(dev.morphia.test.models.Rectangle) UpdateResult(com.mongodb.client.result.UpdateResult) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 2 with UpdateOptions

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

the class TestUpdateOperations method doUpdates.

@SuppressWarnings("rawtypes")
private void doUpdates(ContainsIntArray updated, ContainsIntArray control, Update update, Integer[] target) {
    assertUpdated(update.execute(new UpdateOptions()), 1);
    assertThat(getDs().find(ContainsIntArray.class).filter(eq("_id", updated.id)).first().values, is(target));
    assertThat(getDs().find(ContainsIntArray.class).filter(eq("_id", control.id)).first().values, is(new Integer[] { 1, 2, 3 }));
    Assert.assertEquals(update.execute(new UpdateOptions()).getMatchedCount(), 1);
    assertThat(getDs().find(ContainsIntArray.class).filter(eq("_id", updated.id)).first().values, is(target));
    assertThat(getDs().find(ContainsIntArray.class).filter(eq("_id", control.id)).first().values, is(new Integer[] { 1, 2, 3 }));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UpdateOptions(dev.morphia.UpdateOptions)

Example 3 with UpdateOptions

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

the class TestUpdateOperations method testExistingUpdates.

@Test
public void testExistingUpdates() {
    getDs().save(new Circle(100D));
    getDs().save(new Circle(12D));
    Query<Circle> circle = getDs().find(Circle.class);
    assertUpdated(circle.update(inc("radius", 1D)).execute(), 1);
    assertUpdated(circle.update(inc("radius")).execute(new UpdateOptions().multi(true)), 2);
    // test possible data type change.
    final Circle updatedCircle = circle.filter(eq("radius", 13)).iterator(new FindOptions().limit(1)).next();
    assertThat(updatedCircle, is(notNullValue()));
    MatcherAssert.assertThat(updatedCircle.getRadius(), is(13D));
}
Also used : Circle(dev.morphia.test.models.Circle) FindOptions(dev.morphia.query.FindOptions) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 4 with UpdateOptions

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

the class TestUpdateOperations method testUpsert.

@Test
public void testUpsert() {
    ContainsIntArray cIntArray = new ContainsIntArray();
    ContainsIntArray control = new ContainsIntArray();
    getDs().save(asList(cIntArray, control));
    Query<ContainsIntArray> query = getDs().find(ContainsIntArray.class);
    doUpdates(cIntArray, control, query.update(addToSet("values", 4)), new Integer[] { 1, 2, 3, 4 });
    doUpdates(cIntArray, control, query.update(addToSet("values", asList(4, 5))), new Integer[] { 1, 2, 3, 4, 5 });
    assertInserted(getDs().find(ContainsIntArray.class).filter(eq("values", new Integer[] { 4, 5, 7 })).update(addToSet("values", 6)).execute(new UpdateOptions().upsert(true)));
    query = getDs().find(ContainsIntArray.class).filter(eq("values", new Integer[] { 4, 5, 7, 6 }));
    FindOptions options = new FindOptions().logQuery();
    assertNotNull(query.first(options), query.getLoggedQuery());
}
Also used : FindOptions(dev.morphia.query.FindOptions) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 5 with UpdateOptions

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

the class TestUpdateOperations method testMinWithDates.

@Test
public void testMinWithDates() {
    List<User> entities = List.of(new User("User 1", LocalDate.of(2003, 7, 13)), new User("User 2", LocalDate.of(2009, 12, 1)), new User("User 3", LocalDate.of(2015, 8, 19)));
    getDs().save(entities);
    UpdateResult updated = getDs().find(User.class).update(min("joined", LocalDate.of(1985, 10, 12))).execute(new UpdateOptions().multi(true));
    assertEquals(updated.getModifiedCount(), 3);
    getDs().find(User.class).delete();
    getDs().save(entities);
    updated = getDs().find(User.class).update(min("joined", Instant.now().minus(5000, DAYS))).execute(new UpdateOptions().multi(true));
    assertEquals(updated.getModifiedCount(), 2);
    getDs().find(User.class).delete();
    getDs().save(entities);
    Calendar instance = Calendar.getInstance();
    instance.set(86, Calendar.MAY, 13);
    Date date = instance.getTime();
    updated = getDs().find(User.class).update(min("joined", date)).execute(new UpdateOptions().multi(true));
    assertEquals(updated.getModifiedCount(), 3);
}
Also used : User(dev.morphia.test.models.User) Calendar(java.util.Calendar) UpdateResult(com.mongodb.client.result.UpdateResult) UpdateOptions(dev.morphia.UpdateOptions) Date(java.util.Date) LocalDate(java.time.LocalDate) UpdateOperators.currentDate(dev.morphia.query.experimental.updates.UpdateOperators.currentDate) 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