Search in sources :

Example 6 with UpdateOptions

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

the class TestUpdateOperations method testMaxKeepsCurrentDocumentValueWhenThisIsLargerThanSuppliedValue.

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

Example 7 with UpdateOptions

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

the class TestArrayUpdates method testUpdatesWithArrayFilters.

@Test
public void testUpdatesWithArrayFilters() {
    getMapper().map(Student.class, Grade.class);
    final Datastore datastore = getDs();
    datastore.ensureIndexes();
    datastore.save(new Student(1L, new Grade(80, singletonMap("name", "Homework")), new Grade(90, singletonMap("name", "Test"))));
    Query<Student> grade80 = datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.marks", 80));
    Query<Student> grade90 = datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.marks", 90));
    assertNotNull(grade80.iterator().tryNext());
    assertNotNull(grade90.iterator().tryNext());
    Query<Student> student = datastore.find(Student.class).filter(eq("_id", 1L));
    student.update(inc("grades.$[elem].marks", 5)).execute(new UpdateOptions().arrayFilter(lt("elem.marks", 90)));
    assertNull(grade80.iterator().tryNext());
    assertNotNull(grade90.iterator().tryNext());
    assertNotNull(datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.marks", 85)).iterator().tryNext());
    assertNotNull(grade90.iterator().tryNext());
    student.update(inc("grades.$[elem].marks", 5)).execute(new UpdateOptions().arrayFilter(lt("elem.marks", 90).not()));
    assertNull(grade90.iterator().tryNext());
    assertNotNull(datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.marks", 95)).iterator().tryNext());
}
Also used : Datastore(dev.morphia.Datastore) Grade(dev.morphia.test.models.Grade) Student(dev.morphia.test.models.Student) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 8 with UpdateOptions

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

the class TestUpdateOperations method testInsertWithRef.

@Test
public void testInsertWithRef() {
    final Pic pic = new Pic();
    pic.setName("fist");
    final ObjectId picKey = getDs().save(pic).getId();
    Query<ContainsPic> query = getDs().find(ContainsPic.class).filter(eq("name", "first"), eq("pic", picKey));
    assertInserted(query.update(set("name", "A")).execute(new UpdateOptions().upsert(true)));
    MatcherAssert.assertThat(getDs().find(ContainsPic.class).count(), is(1L));
    getDs().find(ContainsPic.class).delete(new DeleteOptions().multi(true));
    query = getDs().find(ContainsPic.class).filter(eq("name", "first"), eq("pic", pic));
    assertInserted(query.update(set("name", "second")).execute(new UpdateOptions().upsert(true)));
    MatcherAssert.assertThat(getDs().find(ContainsPic.class).count(), is(1L));
    // test reading the object.
    final ContainsPic cp = getDs().find(ContainsPic.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cp, is(notNullValue()));
    MatcherAssert.assertThat(cp.getName(), is("second"));
    MatcherAssert.assertThat(cp.getPic(), is(notNullValue()));
    MatcherAssert.assertThat(cp.getPic().getName(), is(notNullValue()));
    MatcherAssert.assertThat(cp.getPic().getName(), is("fist"));
}
Also used : FindOptions(dev.morphia.query.FindOptions) DeleteOptions(dev.morphia.DeleteOptions) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) ObjectId(org.bson.types.ObjectId) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) Pic(dev.morphia.test.query.TestQuery.Pic) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 9 with UpdateOptions

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

the class TestUpdateOperations method testSetOnInsertWhenUpdating.

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

Example 10 with UpdateOptions

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

the class TestUpdateOperations method testMaxWithDates.

@Test
public void testMaxWithDates() {
    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(max("joined", LocalDate.now())).execute(new UpdateOptions().multi(true));
    assertEquals(updated.getModifiedCount(), 3);
    getDs().find(User.class).delete();
    getDs().save(entities);
    updated = getDs().find(User.class).update(max("joined", Instant.now())).execute(new UpdateOptions().multi(true));
    assertEquals(updated.getModifiedCount(), 3);
    getDs().find(User.class).delete();
    getDs().save(entities);
    Calendar instance = Calendar.getInstance();
    instance.set(2136, Calendar.MAY, 13);
    Date date = instance.getTime();
    updated = getDs().find(User.class).update(max("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