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);
}
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!
}
}
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);
}
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);
}
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);
}
Aggregations