Search in sources :

Example 36 with FindOptions

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

Example 37 with FindOptions

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

Example 38 with FindOptions

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

Example 39 with FindOptions

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

Example 40 with FindOptions

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