Search in sources :

Example 76 with FindOptions

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

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

the class TestUpdateOperations method retainsClassName.

@Test
public void retainsClassName() {
    final MapsOfStuff mapsOfStuff = new MapsOfStuff();
    final Stuff1 stuff1 = new Stuff1();
    stuff1.foo = "narf";
    mapsOfStuff.map.put("k1", stuff1);
    final Stuff2 stuff2 = new Stuff2();
    stuff2.bar = "blarg";
    mapsOfStuff.map.put("k2", stuff2);
    getDs().save(mapsOfStuff);
    final Query<MapsOfStuff> query = getDs().find(MapsOfStuff.class);
    query.update(set("map.k2", stuff1)).execute();
    // fails due to type now missing
    getDs().find(MapsOfStuff.class).iterator(new FindOptions().limit(1)).next();
}
Also used : FindOptions(dev.morphia.query.FindOptions) Test(org.testng.annotations.Test)

Example 78 with FindOptions

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

the class TestUpdateOperations method testUpdateRef.

@Test
public void testUpdateRef() {
    final ContainsPic cp = new ContainsPic();
    cp.setName("cp one");
    getDs().save(cp);
    final Pic pic = new Pic();
    pic.setName("fist");
    getDs().save(pic);
    Query<ContainsPic> query = getDs().find(ContainsPic.class).filter(eq("name", cp.getName()));
    UpdateResult result = query.update(set("pic", pic)).execute();
    Assert.assertEquals(result.getModifiedCount(), 1);
    // test reading the object.
    final ContainsPic cp2 = getDs().find(ContainsPic.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cp2, is(notNullValue()));
    MatcherAssert.assertThat(cp.getName(), CoreMatchers.is(cp2.getName()));
    MatcherAssert.assertThat(cp2.getPic(), is(notNullValue()));
    MatcherAssert.assertThat(cp2.getPic().getName(), is(notNullValue()));
    MatcherAssert.assertThat(pic.getName(), CoreMatchers.is(cp2.getPic().getName()));
    // test reading the object.
    final ContainsPic cp3 = getDs().find(ContainsPic.class).iterator(new FindOptions().limit(1)).next();
    assertThat(cp3, is(notNullValue()));
    MatcherAssert.assertThat(cp.getName(), CoreMatchers.is(cp3.getName()));
    MatcherAssert.assertThat(cp3.getPic(), is(notNullValue()));
    MatcherAssert.assertThat(cp3.getPic().getName(), is(notNullValue()));
    MatcherAssert.assertThat(pic.getName(), CoreMatchers.is(cp3.getPic().getName()));
}
Also used : FindOptions(dev.morphia.query.FindOptions) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) 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 79 with FindOptions

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

the class TestGenerics method example.

@Test
public void example() {
    ChildEntity entity = new ChildEntity();
    entity.setEmbeddedList(asList(new Child("first"), new Child("second"), new Another("third")));
    getDs().save(entity);
    ChildEntity childEntity = getDs().find(ChildEntity.class).iterator(new FindOptions().limit(1)).next();
    Assert.assertEquals(childEntity, entity);
}
Also used : FindOptions(dev.morphia.query.FindOptions) ChildEntity(dev.morphia.test.models.generics.ChildEntity) Child(dev.morphia.test.models.generics.Child) Another(dev.morphia.test.models.generics.Another) Test(org.testng.annotations.Test)

Example 80 with FindOptions

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

the class TestMapping method testLoadOnly.

@Test
public void testLoadOnly() {
    getDs().save(new Normal("value"));
    Normal n = getDs().find(Normal.class).iterator(new FindOptions().limit(1)).next();
    assertNotNull(n);
    assertNotNull(n.name);
    getDs().delete(n);
    getDs().save(new NormalWithLoadOnly());
    n = getDs().find(Normal.class).iterator(new FindOptions().limit(1)).next();
    assertNotNull(n);
    assertNull(n.name);
    getDs().delete(n);
    getDs().save(new Normal("value21"));
    final NormalWithLoadOnly notSaved = getDs().find(NormalWithLoadOnly.class).iterator(new FindOptions().limit(1)).next();
    assertNotNull(notSaved);
    assertNotNull(notSaved.name);
    assertEquals(notSaved.name, "never");
}
Also used : FindOptions(dev.morphia.query.FindOptions) 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