Search in sources :

Example 46 with FindOptions

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
    }
}
Also used : FindOptions(dev.morphia.query.FindOptions) ValidationException(dev.morphia.query.ValidationException) ArraySlice(dev.morphia.query.ArraySlice) Test(org.testng.annotations.Test)

Example 47 with FindOptions

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);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Test(org.testng.annotations.Test)

Example 48 with FindOptions

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);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Rectangle(dev.morphia.test.models.Rectangle) Test(org.testng.annotations.Test)

Example 49 with FindOptions

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);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Rectangle(dev.morphia.test.models.Rectangle) Test(org.testng.annotations.Test)

Example 50 with FindOptions

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);
}
Also used : FindOptions(dev.morphia.query.FindOptions) ArraySlice(dev.morphia.query.ArraySlice) 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