Search in sources :

Example 1 with FindOptions

use of dev.morphia.query.FindOptions 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 FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class TestUpdateOperations 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).filter(eq("_id", parentId), eq("children.first", childName));
    UpdateResult updateResult = query.update(set("children.$.last", updatedLastName)).execute();
    // then
    assertThat(updateResult.getModifiedCount(), is(1L));
    assertThat(getDs().find(Parent.class).filter(eq("id", parentId)).iterator(new FindOptions().limit(1)).next().children, hasItem(new Child(childName, updatedLastName)));
}
Also used : FindOptions(dev.morphia.query.FindOptions) ObjectId(org.bson.types.ObjectId) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.testng.annotations.Test)

Example 3 with FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class TestUpdateOperations method testUpdateKeyList.

@Test
public void testUpdateKeyList() {
    final ContainsPicKey cpk = new ContainsPicKey();
    cpk.name = "cpk one";
    Datastore ds = getDs();
    ds.save(cpk);
    final Pic pic = new Pic();
    pic.setName("fist again");
    ds.save(pic);
    cpk.keys = MorphiaReference.wrap(List.of(pic));
    // test with Key<Pic>
    Query<ContainsPicKey> query = ds.find(ContainsPicKey.class).filter(eq("name", cpk.name));
    final UpdateResult res = query.update(set("keys", cpk.keys)).execute();
    assertThat(res.getModifiedCount(), is(1L));
    // test reading the object.
    final ContainsPicKey cpk2 = ds.find(ContainsPicKey.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cpk2, is(notNullValue()));
    assertThat(cpk.name, is(cpk2.name));
    MatcherAssert.assertThat(cpk2.keys.get(), Matchers.hasItem(pic));
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) Pic(dev.morphia.test.query.TestQuery.Pic) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.testng.annotations.Test)

Example 4 with FindOptions

use of dev.morphia.query.FindOptions 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 5 with FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class TestUpdateOperations method testUpdateKeyRef.

@Test
public void testUpdateKeyRef() {
    final ContainsPicKey cpk = new ContainsPicKey();
    cpk.name = "cpk one";
    Datastore ds = getDs();
    ds.save(cpk);
    final Pic pic = new Pic();
    pic.setName("fist again");
    ds.save(pic);
    Query<ContainsPicKey> query = ds.find(ContainsPicKey.class).filter(eq("name", cpk.name));
    assertThat(query.update(set("pic", pic)).execute().getModifiedCount(), is(1L));
    // test reading the object.
    final ContainsPicKey cpk2 = ds.find(ContainsPicKey.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cpk2, is(notNullValue()));
    assertThat(cpk.name, is(cpk2.name));
    assertThat(cpk2.pic, is(notNullValue()));
    MatcherAssert.assertThat(pic, CoreMatchers.is(cpk2.pic.get()));
    query.update(set("pic", pic)).execute();
    // test reading the object.
    final ContainsPicKey cpk3 = ds.find(ContainsPicKey.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cpk3, is(notNullValue()));
    assertThat(cpk.name, is(cpk3.name));
    assertThat(cpk3.pic, is(notNullValue()));
    MatcherAssert.assertThat(pic, CoreMatchers.is(cpk3.pic.get()));
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) Pic(dev.morphia.test.query.TestQuery.Pic) Test(org.testng.annotations.Test)

Aggregations

FindOptions (dev.morphia.query.FindOptions)118 Test (org.testng.annotations.Test)114 Point (com.mongodb.client.model.geojson.Point)16 Position (com.mongodb.client.model.geojson.Position)16 Document (org.bson.Document)16 Rectangle (dev.morphia.test.models.Rectangle)13 Datastore (dev.morphia.Datastore)12 UpdateResult (com.mongodb.client.result.UpdateResult)8 UpdateOptions (dev.morphia.UpdateOptions)8 ObjectId (org.bson.types.ObjectId)7 ValidationException (dev.morphia.query.ValidationException)6 User (dev.morphia.test.models.User)5 ContainsPic (dev.morphia.test.query.TestQuery.ContainsPic)5 Pic (dev.morphia.test.query.TestQuery.Pic)5 LocalDate (java.time.LocalDate)5 Date (java.util.Date)5 DeleteOptions (dev.morphia.DeleteOptions)4 Query (dev.morphia.query.Query)4 DocumentValidation (dev.morphia.test.models.DocumentValidation)4 ContainsPic (dev.morphia.test.query.TestLegacyQuery.ContainsPic)4