Search in sources :

Example 51 with FindOptions

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

the class TestLegacyQuery method genericMultiKeyValueQueries.

@Test
@SuppressWarnings("rawtypes")
public void genericMultiKeyValueQueries() {
    getMapper().map(GenericKeyValue.class);
    getDs().ensureIndexes(GenericKeyValue.class);
    final GenericKeyValue<String> value = new GenericKeyValue<>();
    final List<Object> keys = Arrays.asList("key1", "key2");
    value.key = keys;
    getDs().save(value);
    Query<GenericKeyValue> query = getDs().find(GenericKeyValue.class).field("key").hasAnyOf(keys);
    final GenericKeyValue found = query.execute(new FindOptions().logQuery()).tryNext();
    String loggedQuery = query.getLoggedQuery();
    assertTrue(loggedQuery.contains("{\"$in\": [\"key1\", \"key2\"]"), loggedQuery);
    assertEquals(found.id, value.id);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Test(org.testng.annotations.Test)

Example 52 with FindOptions

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

the class TestLegacyQuery method testProject.

@Test
public void testProject() {
    getDs().save(new ContainsRenamedFields("Frank", "Zappa"));
    ContainsRenamedFields found = getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().include("first_name").limit(1)).tryNext();
    assertNotNull(found.firstName);
    assertNull(found.lastName);
    found = getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().include("first_name").limit(1)).tryNext();
    assertNotNull(found.firstName);
    assertNull(found.lastName);
    try {
        getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().include("bad field name").limit(1)).tryNext();
        fail("Validation should have caught the bad field");
    } catch (ValidationException e) {
    // success!
    }
}
Also used : FindOptions(dev.morphia.query.FindOptions) ValidationException(dev.morphia.query.ValidationException) Test(org.testng.annotations.Test)

Example 53 with FindOptions

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

the class TestLegacyQuery method testNaturalSortDescending.

@Test
public void testNaturalSortDescending() {
    getDs().save(asList(new Rectangle(6, 10), new Rectangle(3, 8), new Rectangle(10, 10), new Rectangle(10, 1)));
    List<Rectangle> results = getDs().find(Rectangle.class).execute(new FindOptions().sort(naturalDescending())).toList();
    assertEquals(results.size(), 4);
    Rectangle r;
    r = results.get(0);
    assertNotNull(r);
    assertEquals(r.getHeight(), 10, 0);
    assertEquals(r.getWidth(), 1, 0);
    r = results.get(1);
    assertNotNull(r);
    assertEquals(r.getHeight(), 10, 0);
    assertEquals(r.getWidth(), 10, 0);
    r = results.get(2);
    assertNotNull(r);
    assertEquals(r.getHeight(), 3, 0);
    assertEquals(r.getWidth(), 8, 0);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Rectangle(dev.morphia.test.models.Rectangle) Test(org.testng.annotations.Test)

Example 54 with FindOptions

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

the class TestLegacyQuery 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).execute(new FindOptions().sort(ascending("w")).limit(1)).tryNext();
    assertNotNull(r1);
    assertEquals(r1.getWidth(), 1, 0);
    r1 = getDs().find(Rectangle.class).execute(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 55 with FindOptions

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

the class TestLegacyQuery method multiKeyValueQueries.

@Test
public void multiKeyValueQueries() {
    getMapper().map(List.of(KeyValue.class));
    getDs().ensureIndexes(KeyValue.class);
    final KeyValue value = new KeyValue();
    final List<Object> keys = Arrays.asList("key1", "key2");
    value.key = keys;
    getDs().save(value);
    final Query<KeyValue> query = getDs().find(KeyValue.class).field("key").hasAnyOf(keys);
    query.execute(new FindOptions().logQuery());
    String loggedQuery = query.getLoggedQuery();
    assertTrue(loggedQuery.contains("{\"$in\": [\"key1\", \"key2\"]"), loggedQuery);
    assertEquals(query.execute(new FindOptions().limit(1)).tryNext().id, value.id);
}
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