use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestLegacyQuery method testMixedProjection.
@Test
public void testMixedProjection() {
getDs().save(new ContainsRenamedFields("Frank", "Zappa"));
try {
getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().include("first_name").projection().exclude("last_name"));
fail("An exception should have been thrown indication a mixed projection");
} catch (ValidationException e) {
// all good
}
try {
getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().include("first_name", "last_name").projection().exclude("_id"));
} catch (ValidationException e) {
fail("An exception should not have been thrown indication a mixed projection because _id suppression is a special case");
}
try {
getDs().find(ContainsRenamedFields.class).execute(new FindOptions().projection().exclude("first_name", "last_name").projection().include("_id"));
fail("An exception should have been thrown indication a mixed projection");
} catch (ValidationException e) {
// all good
}
try {
getDs().find(IntVector.class).execute(new FindOptions().projection().exclude("name").projection().project("scalars", new ArraySlice(5)));
fail("An exception should have been thrown indication a mixed projection");
} catch (ValidationException e) {
// all good
}
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestLegacyQuery method testNegativeBatchSize.
@Test
public void testNegativeBatchSize() {
getDs().delete(getDs().find(PhotoWithKeywords.class));
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).execute(new FindOptions().batchSize(-2)).toList().size(), 2);
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestLegacyQuery 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).execute(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 TestLegacyQuery method testSimpleSort.
@Test
public void testSimpleSort() {
getMapper().map(Rectangle.class);
getDs().ensureIndexes();
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")).limit(1)).next();
assertNotNull(r1);
assertEquals(r1.getWidth(), 1, 0);
r1 = getDs().find(Rectangle.class).execute(new FindOptions().sort(descending("width")).limit(1)).next();
assertNotNull(r1);
assertEquals(r1.getWidth(), 10, 0);
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestLegacyQuery method testProjectArrayField.
@Test
public void testProjectArrayField() {
int[] ints = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 };
IntVector vector = new IntVector(ints);
getDs().save(vector);
assertEquals(copy(ints, 0, 4), getDs().find(IntVector.class).execute(new FindOptions().projection().project("scalars", new ArraySlice(4)).limit(1)).next().scalars);
assertEquals(copy(ints, 5, 4), getDs().find(IntVector.class).execute(new FindOptions().projection().project("scalars", new ArraySlice(5, 4)).limit(1)).next().scalars);
assertEquals(copy(ints, ints.length - 10, 6), getDs().find(IntVector.class).execute(new FindOptions().projection().project("scalars", new ArraySlice(-10, 6)).limit(1)).next().scalars);
assertEquals(copy(ints, ints.length - 12, 12), getDs().find(IntVector.class).execute(new FindOptions().projection().project("scalars", new ArraySlice(-12)).limit(1)).next().scalars);
}
Aggregations