use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestLegacyQuery method testCombinationQuery.
@Test
public void testCombinationQuery() {
getDs().save(asList(new Rectangle(1, 10), new Rectangle(4, 2), new Rectangle(6, 10), new Rectangle(8, 5), new Rectangle(10, 4)));
Query<Rectangle> q = getDs().find(Rectangle.class);
q.and(q.criteria("width").equal(10), q.criteria("height").equal(1));
List<Rectangle> list = q.execute(new FindOptions().logQuery()).toList();
assertEquals(q.count(), 1);
q = getDs().find(Rectangle.class);
q.or(q.criteria("width").equal(10), q.criteria("height").equal(10));
assertEquals(q.count(), 3);
q = getDs().find(Rectangle.class);
q.or(q.criteria("width").equal(10), q.and(q.criteria("width").equal(5), q.criteria("height").equal(8)));
q.execute(new FindOptions().logQuery()).toList();
assertEquals(q.count(), 3, q.getLoggedQuery());
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestLegacyQuery method testCommentsShowUpInLogs.
@Test
public void testCommentsShowUpInLogs() {
getDs().save(asList(new Pic("pic1"), new Pic("pic2"), new Pic("pic3"), new Pic("pic4")));
getDatabase().runCommand(new Document("profile", 2));
String expectedComment = "test comment";
getDs().find(Pic.class).execute(new FindOptions().comment(expectedComment)).toList();
MongoCollection<Document> profileCollection = getDatabase().getCollection("system.profile");
assertNotEquals(profileCollection.countDocuments(), 0);
Document query = new Document("op", "query").append("ns", getDs().getCollection(Pic.class).getNamespace().getFullName()).append("command.comment", new Document("$exists", true));
Document profileRecord = profileCollection.find(query).first();
assertEquals(getCommentFromProfileRecord(profileRecord), expectedComment, profileRecord.toString());
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestLegacyQuery method testQueryOverReference.
@Test(expectedExceptions = ValidationException.class)
public void testQueryOverReference() {
final ContainsPic cpk = new ContainsPic();
final Pic p = new Pic();
getDs().save(p);
cpk.pic = p;
getDs().save(cpk);
final Query<ContainsPic> query = getDs().find(ContainsPic.class);
assertEquals(query.field("pic").equal(p).count(), 1);
getDs().find(ContainsPic.class).filter("pic.name", "foo").execute(new FindOptions().limit(1)).next();
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestLegacyQuery method testQueryUnmappedData.
@Test
public void testQueryUnmappedData() {
getMapper().map(Class1.class);
getDs().ensureIndexes();
getDs().getDatabase().getCollection("user").insertOne(new Document().append("@class", Class1.class.getName()).append("value1", "foo").append("someMap", new Document("someKey", "value")));
Query<Class1> query = getDs().find(Class1.class);
query.disableValidation().criteria("someMap.someKey").equal("value");
Class1 retrievedValue = query.execute(new FindOptions().limit(1)).next();
assertNotNull(retrievedValue);
assertEquals(retrievedValue.value1, "foo");
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestLegacyQuery method testRetrievedFields.
@Test
public void testRetrievedFields() {
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("firstName").limit(1)).tryNext();
assertNotNull(found.firstName);
assertNull(found.lastName);
assertThrows(ValidationException.class, () -> {
getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().include("bad field name").limit(1)).tryNext();
});
}
Aggregations