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