Search in sources :

Example 96 with FindOptions

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);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Rectangle(dev.morphia.test.models.Rectangle) Test(org.testng.annotations.Test)

Example 97 with FindOptions

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);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Query(dev.morphia.query.Query) CountOptions(dev.morphia.query.CountOptions) Test(org.testng.annotations.Test)

Example 98 with FindOptions

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");
}
Also used : FindOptions(dev.morphia.query.FindOptions) Test(org.testng.annotations.Test)

Example 99 with FindOptions

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);
}
Also used : FindOptions(dev.morphia.query.FindOptions) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Datastore(dev.morphia.Datastore) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 100 with FindOptions

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);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Query(dev.morphia.query.Query) CountOptions(dev.morphia.query.CountOptions) 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