use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestLegacyQuery method testCompoundSort.
@Test
public void testCompoundSort() {
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("width"), descending("height")).limit(1)).tryNext();
assertNotNull(r1);
assertEquals(r1.getWidth(), 1, 0);
assertEquals(r1.getHeight(), 10, 0);
r1 = getDs().find(Rectangle.class).execute(new FindOptions().sort(descending("height"), descending("width")).limit(1)).tryNext();
assertNotNull(r1);
assertEquals(r1.getWidth(), 10, 0);
assertEquals(r1.getHeight(), 10, 0);
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestLegacyQuery method testCollations.
@Test
public void testCollations() {
getMapper().map(ContainsRenamedFields.class);
getDs().save(asList(new ContainsRenamedFields("first", "last"), new ContainsRenamedFields("First", "Last")));
Query query = getDs().find(ContainsRenamedFields.class).field("last_name").equal("last");
assertEquals(query.execute().toList().size(), 1);
assertEquals(query.execute(new FindOptions().collation(builder().locale("en").collationStrength(CollationStrength.SECONDARY).build())).toList().size(), 2);
assertEquals(query.count(), 1);
assertEquals(query.count(new CountOptions().collation(builder().locale("en").collationStrength(CollationStrength.SECONDARY).build())), 2);
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestLegacyQuery 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).field("name").equal("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");
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestLegacyQuery method testTailableCursors.
@Test
public void testTailableCursors() {
getMapper().map(CappedPic.class);
final Datastore ds = getDs();
ds.ensureCaps();
final Query<CappedPic> query = ds.find(CappedPic.class);
final List<CappedPic> found = new ArrayList<>();
final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
assertEquals(query.count(), 0);
ScheduledFuture<?> scheduledFuture = executorService.scheduleAtFixedRate(() -> ds.save(new CappedPic()), 0, 100, TimeUnit.MILLISECONDS);
Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> getDs().find(CappedPic.class).count() > 0);
final Iterator<CappedPic> tail = query.execute(new FindOptions().cursorType(CursorType.Tailable));
Awaitility.await().pollDelay(500, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS).until(() -> {
if (tail.hasNext()) {
found.add(tail.next());
}
return found.size() >= 10;
});
executorService.shutdownNow();
assertTrue(found.size() >= 10);
assertTrue(query.count() >= 10);
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestQuery method testCollations.
@Test
public void testCollations() {
getMapper().map(ContainsRenamedFields.class);
getDs().save(asList(new ContainsRenamedFields("first", "last"), new ContainsRenamedFields("First", "Last")));
Query query = getDs().find(ContainsRenamedFields.class).filter(eq("last_name", "last"));
assertEquals(query.iterator().toList().size(), 1);
assertEquals(query.iterator(new FindOptions().collation(builder().locale("en").collationStrength(CollationStrength.SECONDARY).build())).toList().size(), 2);
assertEquals(query.count(), 1);
assertEquals(query.count(new CountOptions().collation(builder().locale("en").collationStrength(CollationStrength.SECONDARY).build())), 2);
}
Aggregations