use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestQuery 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).iterator(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.toJson(getDs().getCodecRegistry().get(Document.class)));
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestQuery method testNaturalSortAscending.
@Test
public void testNaturalSortAscending() {
getDs().save(asList(new Rectangle(6, 10), new Rectangle(3, 8), new Rectangle(10, 10), new Rectangle(10, 1)));
List<Rectangle> results = getDs().find(Rectangle.class).iterator(new FindOptions().sort(naturalAscending())).toList();
assertEquals(results.size(), 4);
Rectangle r;
r = results.get(0);
assertNotNull(r);
assertEquals(r.getHeight(), 6, 0);
assertEquals(r.getWidth(), 10, 0);
r = results.get(1);
assertNotNull(r);
assertEquals(r.getHeight(), 3, 0);
assertEquals(r.getWidth(), 8, 0);
r = results.get(2);
assertNotNull(r);
assertEquals(r.getHeight(), 10, 0);
assertEquals(r.getWidth(), 10, 0);
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestQuery method testQBE.
@Test
public void testQBE() {
final CustomId cId = new CustomId();
cId.setId(new ObjectId());
cId.setType("banker");
final UsesCustomIdObject object = new UsesCustomIdObject();
object.setId(cId);
object.setText("hllo");
getDs().save(object);
final UsesCustomIdObject loaded;
// Add back if/when query by example for embedded fields is supported (require dotting each field).
// CustomId exId = new CustomId();
// exId.type = cId.type;
// loaded = getDs().find(UsesCustomIdObject.class, "_id", exId).get();
// assertNotNull(loaded);
final UsesCustomIdObject ex = new UsesCustomIdObject();
ex.setText(object.getText());
loaded = getDs().queryByExample(ex).iterator(new FindOptions().limit(1)).next();
assertNotNull(loaded);
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestQuery method testProject.
@Test
public void testProject() {
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("first_name").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!
}
Query<ContainsRenamedFields> query = getDs().find(ContainsRenamedFields.class);
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestQuery method testNegativeBatchSize.
@Test
public void testNegativeBatchSize() {
getDs().find(PhotoWithKeywords.class).delete(new DeleteOptions().multi(true));
getDs().save(asList(new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("scott", "hernandez"), new PhotoWithKeywords("1", "2"), new PhotoWithKeywords("3", "4"), new PhotoWithKeywords("5", "6")));
assertEquals(getDs().find(PhotoWithKeywords.class).iterator(new FindOptions().batchSize(-2)).toList().size(), 2);
}
Aggregations