Search in sources :

Example 41 with FindOptions

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

the class TestQuery method testFluentNotQuery.

@Test
public void testFluentNotQuery() {
    checkMinServerVersion(4.0);
    final PhotoWithKeywords pwk = new PhotoWithKeywords("scott", "hernandez");
    getDs().save(pwk);
    final Query<PhotoWithKeywords> query = getDs().find(PhotoWithKeywords.class);
    query.filter(regex("keywords.keyword").pattern("^ralph").not());
    FindOptions options = new FindOptions().logQuery();
    query.iterator();
    assertEquals(query.count(), 1);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Test(org.testng.annotations.Test)

Example 42 with FindOptions

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

the class TestQuery method testRetrievedFields.

@Test
public void testRetrievedFields() {
    getDs().save(new ContainsRenamedFields("Frank", "Zappa"));
    ContainsRenamedFields found = getDs().find(ContainsRenamedFields.class).iterator(new FindOptions().projection().include("first_name").limit(1)).tryNext();
    assertNotNull(found.firstName);
    assertNull(found.lastName);
    found = getDs().find(ContainsRenamedFields.class).iterator(new FindOptions().projection().include("firstName").limit(1)).tryNext();
    assertNotNull(found.firstName);
    assertNull(found.lastName);
    try {
        getDs().find(ContainsRenamedFields.class).iterator(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 43 with FindOptions

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

the class TestQuery method testTailableCursors.

@Test
public void testTailableCursors() {
    getMapper().map(CappedPic.class);
    final Datastore ds = getDs();
    final Query<CappedPic> query = ds.find(CappedPic.class);
    getMapper().map(CappedPic.class);
    ds.ensureCaps();
    final List<CappedPic> found = new ArrayList<>();
    assertCapped(CappedPic.class, 1000, 1024 * 1024);
    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.iterator(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 44 with FindOptions

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

the class FiltersTest method testMeta.

@Test
public void testMeta() {
    MongoCollection<Document> articles = getDatabase().getCollection("articles");
    articles.insertMany(List.of(new Document("_id", 1).append("title", "cakes and ale"), new Document("_id", 2).append("title", "more cakes"), new Document("_id", 3).append("title", "bread"), new Document("_id", 4).append("title", "some cakes"), new Document("_id", 5).append("title", "two cakes to go"), new Document("_id", 6).append("title", "pie")));
    articles.createIndex(new Document("title", "text"));
    FindOptions options = new FindOptions().logQuery();
    Query<Document> query = getDs().find("articles", Document.class).disableValidation().filter(text("cake"));
    List<Document> list = query.iterator(options.projection().project(Meta.textScore("score"))).toList();
    assertEquals(list.size(), 4, query.getLoggedQuery());
    Document document = list.stream().filter(d -> d.get("_id").equals(4)).findFirst().orElseThrow();
    assertEquals(document.get("score"), 1.0, query.getLoggedQuery());
}
Also used : FindOptions(dev.morphia.query.FindOptions) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 45 with FindOptions

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

the class FiltersTest method testBitsAllClear.

@Test
public void testBitsAllClear() {
    MongoCollection<Document> collection = getDatabase().getCollection("users");
    collection.insertMany(asList(new Document("a", 54).append("binaryValueofA", "00110110").append("_t", "User"), new Document("a", 20).append("binaryValueofA", "00010100").append("_t", "User"), new Document("a", 20.0).append("binaryValueofA", "00010100").append("_t", "User")));
    FindOptions options = new FindOptions().logQuery();
    Query<User> query = getDs().find(User.class).disableValidation().filter(bitsAllClear("a", 35));
    assertEquals(query.iterator(options).toList().size(), 2, query.getLoggedQuery());
    query = getDs().find(User.class).disableValidation().filter(bitsAllClear("a", new int[] { 1, 5 }));
    assertEquals(query.iterator(options).toList().size(), 2, query.getLoggedQuery());
}
Also used : FindOptions(dev.morphia.query.FindOptions) User(dev.morphia.test.models.User) Document(org.bson.Document) 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