Search in sources :

Example 56 with FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class TestQueriesOnReferences method testQueryOverReference.

@Test
public void testQueryOverReference() {
    final ContainsPic cpk = new ContainsPic();
    final Pic p = new Pic();
    getDs().save(p);
    cpk.setPic(p);
    getDs().save(cpk);
    final Query<ContainsPic> query = getDs().find(ContainsPic.class);
    final ContainsPic object = query.filter(eq("pic", p)).iterator(new FindOptions().limit(1)).tryNext();
    assertNotNull(object);
}
Also used : FindOptions(dev.morphia.query.FindOptions) ContainsPic(dev.morphia.test.query.TestLegacyQuery.ContainsPic) ContainsPic(dev.morphia.test.query.TestLegacyQuery.ContainsPic) Pic(dev.morphia.test.query.TestLegacyQuery.Pic) Test(org.testng.annotations.Test)

Example 57 with FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class TestQueriesOnReferences method testQueryOverLazyReference.

@Test
public void testQueryOverLazyReference() {
    final ContainsPic cpk = new ContainsPic();
    final Pic p = new Pic();
    getDs().save(p);
    final PicWithObjectId withObjectId = new PicWithObjectId();
    getDs().save(withObjectId);
    cpk.setLazyPic(p);
    cpk.setLazyObjectIdPic(withObjectId);
    getDs().save(cpk);
    Query<ContainsPic> query = getDs().find(ContainsPic.class);
    assertNotNull(query.filter(eq("lazyPic", p)).iterator(new FindOptions().limit(1)).tryNext());
    query = getDs().find(ContainsPic.class);
    assertNotNull(query.filter(eq("lazyObjectIdPic", withObjectId)).iterator(new FindOptions().limit(1)).tryNext());
}
Also used : FindOptions(dev.morphia.query.FindOptions) ContainsPic(dev.morphia.test.query.TestLegacyQuery.ContainsPic) ContainsPic(dev.morphia.test.query.TestLegacyQuery.ContainsPic) Pic(dev.morphia.test.query.TestLegacyQuery.Pic) PicWithObjectId(dev.morphia.test.query.TestLegacyQuery.PicWithObjectId) Test(org.testng.annotations.Test)

Example 58 with FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class TestTextSearching method testTextSearchWithMeta.

@Test
public void testTextSearchWithMeta() {
    getMapper().map(Book.class);
    getDs().ensureIndexes();
    getDs().save(asList(new Book("The Banquet", "Dante"), new Book("Divine Comedy", "Dante"), new Book("Eclogues", "Dante"), new Book("The Odyssey", "Homer"), new Book("Iliad", "Homer")));
    List<Book> books = getDs().find(Book.class).filter(text("Dante")).iterator(new FindOptions().sort(Meta.textScore("score"))).toList();
    assertEquals(books.size(), 3);
    for (Book book : books) {
        assertEquals(book.author, "Dante");
    }
}
Also used : FindOptions(dev.morphia.query.FindOptions) Test(org.testng.annotations.Test)

Example 59 with FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class TestLazyCircularReference method testCircularReferences.

public void testCircularReferences() {
    RootEntity root = new RootEntity();
    ReferencedEntity first = new ReferencedEntity();
    ReferencedEntity second = new ReferencedEntity();
    getDs().save(asList(root, first, second));
    root.r = first;
    root.secondReference = second;
    first.parent = root;
    second.parent = root;
    getDs().save(asList(root, first, second));
    RootEntity rootEntity = getDs().find(RootEntity.class).iterator(new FindOptions().limit(1)).tryNext();
    assertEquals(first.getId(), rootEntity.getR().getId());
    assertEquals(second.getId(), rootEntity.getSecondReference().getId());
    assertEquals(root.getId(), rootEntity.getR().getParent().getId());
}
Also used : FindOptions(dev.morphia.query.FindOptions)

Example 60 with FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class TestSingleToMultipleConversion method testBasicType.

@Test
public void testBasicType() {
    getDs().find(HasSingleString.class).delete();
    getDs().save(new HasSingleString());
    Assert.assertNotNull(getDs().find(HasSingleString.class).iterator(new FindOptions().limit(1)).next());
    Assert.assertEquals(getDs().find(HasSingleString.class).count(), 1);
    final HasManyStringsArray hms = getDs().find(HasManyStringsArray.class).iterator(new FindOptions().limit(1)).next();
    Assert.assertNotNull(hms);
    Assert.assertNotNull(hms.strings);
    Assert.assertEquals(hms.strings.length, 1);
    final HasManyStringsList hms2 = getDs().find(HasManyStringsList.class).iterator(new FindOptions().limit(1)).next();
    Assert.assertNotNull(hms2);
    Assert.assertNotNull(hms2.strings);
    Assert.assertEquals(hms2.strings.size(), 1);
}
Also used : FindOptions(dev.morphia.query.FindOptions) 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