Search in sources :

Example 91 with FindOptions

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

Example 92 with FindOptions

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

Example 93 with FindOptions

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

Example 94 with FindOptions

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

Example 95 with FindOptions

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();
    });
}
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