Search in sources :

Example 31 with FindOptions

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

the class TestUriMapping method testURIField.

@Test
public void testURIField() throws URISyntaxException {
    final ContainsURI entity = new ContainsURI();
    final URI testURI = new URI("http://lamest.local/test.html");
    entity.uri = testURI;
    getDs().save(entity);
    final ContainsURI loaded = getDs().find(ContainsURI.class).iterator(new FindOptions().limit(1)).tryNext();
    assertNotNull(loaded.uri);
    assertEquals(testURI, loaded.uri);
}
Also used : FindOptions(dev.morphia.query.FindOptions) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 32 with FindOptions

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

the class TestUriMapping method testURIMap.

@Test
public void testURIMap() throws URISyntaxException {
    final ContainsURIKeyedMap entity = new ContainsURIKeyedMap();
    final URI testURI = new URI("http://lamest.local/test.html");
    entity.uris.put(testURI, "first");
    getDs().save(entity);
    final ContainsURIKeyedMap loaded = getDs().find(ContainsURIKeyedMap.class).iterator(new FindOptions().limit(1)).tryNext();
    assertNotNull(loaded.uris);
    assertEquals(loaded.uris.size(), 1);
    assertEquals(testURI, loaded.uris.keySet().iterator().next());
}
Also used : FindOptions(dev.morphia.query.FindOptions) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 33 with FindOptions

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

the class TestQuery method testAliasedFieldSort.

@Test
public void testAliasedFieldSort() {
    getDs().save(asList(new Rectangle(1, 10), new Rectangle(3, 8), new Rectangle(6, 10), new Rectangle(10, 10), new Rectangle(10, 1)));
    Rectangle r1 = getDs().find(Rectangle.class).iterator(new FindOptions().sort(ascending("w")).limit(1)).tryNext();
    assertNotNull(r1);
    assertEquals(r1.getWidth(), 1, 0);
    r1 = getDs().find(Rectangle.class).iterator(new FindOptions().sort(descending("w")).limit(1)).tryNext();
    assertNotNull(r1);
    assertEquals(r1.getWidth(), 10, 0);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Rectangle(dev.morphia.test.models.Rectangle) Test(org.testng.annotations.Test)

Example 34 with FindOptions

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

the class TestQuery method testReturnOnlyIndexedFields.

@Test
public void testReturnOnlyIndexedFields() {
    getMapper().map(Pic.class);
    getDs().ensureIndexes(Pic.class);
    getDs().save(asList(new Pic("pic1"), new Pic("pic2"), new Pic("pic3"), new Pic("pic4")));
    Pic foundItem = getDs().find(Pic.class).filter(eq("name", "pic2")).first(new FindOptions().limit(1).returnKey(true));
    assertNotNull(foundItem);
    assertThat("Name should be populated", foundItem.getName(), is("pic2"));
    assertNull(foundItem.getId(), "ID should not be populated");
}
Also used : FindOptions(dev.morphia.query.FindOptions) Test(org.testng.annotations.Test)

Example 35 with FindOptions

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

the class TestQuery method testProjectArrayField.

@Test
public void testProjectArrayField() {
    int[] ints = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 };
    IntVector vector = new IntVector(ints);
    getDs().save(vector);
    assertEquals(copy(ints, 0, 4), getDs().find(IntVector.class).iterator(new FindOptions().projection().project("scalars", new ArraySlice(4)).limit(1)).next().scalars);
    assertEquals(copy(ints, 5, 4), getDs().find(IntVector.class).iterator(new FindOptions().projection().project("scalars", new ArraySlice(5, 4)).limit(1)).next().scalars);
    assertEquals(copy(ints, ints.length - 10, 6), getDs().find(IntVector.class).iterator(new FindOptions().projection().project("scalars", new ArraySlice(-10, 6)).limit(1)).next().scalars);
    assertEquals(copy(ints, ints.length - 12, 12), getDs().find(IntVector.class).iterator(new FindOptions().projection().project("scalars", new ArraySlice(-12)).limit(1)).next().scalars);
}
Also used : FindOptions(dev.morphia.query.FindOptions) ArraySlice(dev.morphia.query.ArraySlice) 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