Search in sources :

Example 16 with FindOptions

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

the class TestVersioning method testUpdate.

@Test
public void testUpdate() {
    Datastore ds = getDs();
    List<VersionedType> initial = List.of(new VersionedType(), new VersionedType());
    ds.save(initial);
    UpdateResult results = ds.find(VersionedType.class).update(set("text", "some new value")).execute();
    assertEquals(results.getModifiedCount(), 1);
    List<VersionedType> postUpdate = ds.find(VersionedType.class).filter(eq("text", "some new value")).iterator(new FindOptions().sort(Sort.ascending("_id"))).toList();
    for (int i = 0, postUpdateSize = postUpdate.size(); i < postUpdateSize; i++) {
        final VersionedType versionedType = postUpdate.get(i);
        assertEquals(versionedType.version, initial.get(i).version + 1);
    }
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.testng.annotations.Test)

Example 17 with FindOptions

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

the class TestArrayUpdates method testStudents.

@Test
public void testStudents() {
    getMapper().map(Student.class);
    final Datastore datastore = getDs();
    datastore.ensureIndexes();
    datastore.save(new Student(1L, new Grade(80, singletonMap("name", "Homework")), new Grade(90, singletonMap("name", "Test"))));
    Query<Student> testQuery = datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.data.name", "Test"));
    assertNotNull(testQuery.iterator(new FindOptions().limit(1)).tryNext());
    testQuery.update(set("grades.$.data.name", "Makeup Test")).execute();
    assertNull(testQuery.iterator(new FindOptions().limit(1)).tryNext());
    assertNotNull(datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.data.name", "Makeup Test")).iterator(new FindOptions().limit(1)).tryNext());
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) Grade(dev.morphia.test.models.Grade) Student(dev.morphia.test.models.Student) Test(org.testng.annotations.Test)

Example 18 with FindOptions

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

the class TestArrayUpdates method testUpdates.

@Test
public void testUpdates() {
    BatchData theBatch = new BatchData();
    theBatch.files.add(new Files(0, "fileName1", "fileHash1"));
    theBatch.files.add(new Files(0, "fileName2", "fileHash2"));
    getDs().save(theBatch);
    ObjectId id = theBatch.id;
    theBatch = new BatchData();
    theBatch.files.add(new Files(0, "fileName3", "fileHash3"));
    theBatch.files.add(new Files(0, "fileName4", "fileHash4"));
    getDs().save(theBatch);
    ObjectId id2 = theBatch.id;
    getDs().find(BatchData.class).filter(eq("_id", id), eq("files.fileName", "fileName1")).update(set("files.$.fileHash", "new hash")).execute();
    BatchData data = getDs().find(BatchData.class).filter(eq("_id", id)).iterator(new FindOptions().limit(1)).tryNext();
    assertEquals(data.files.get(0).fileHash, "new hash");
    assertEquals(data.files.get(1).fileHash, "fileHash2");
    data = getDs().find(BatchData.class).filter(eq("_id", id2)).iterator(new FindOptions().limit(1)).tryNext();
    assertEquals(data.files.get(0).fileHash, "fileHash3");
    assertEquals(data.files.get(1).fileHash, "fileHash4");
}
Also used : FindOptions(dev.morphia.query.FindOptions) ObjectId(org.bson.types.ObjectId) Test(org.testng.annotations.Test)

Example 19 with FindOptions

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

the class TestDatastore method testFindAndModify.

@Test
public void testFindAndModify() {
    getDs().save(asList(new FacebookUser(1, "John Doe"), new FacebookUser(2, "john doe")));
    Query<FacebookUser> query = getDs().find(FacebookUser.class).filter(eq("username", "john doe"));
    Modify<FacebookUser> modify = query.modify(inc("loginCount"));
    assertEquals(modify.execute().loginCount, 0);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 1)).first().loginCount, 0);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 2)).first().loginCount, 1);
    assertEquals(modify.execute(new ModifyOptions().returnDocument(AFTER)).loginCount, 2);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 1)).first().loginCount, 0);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 2)).first().loginCount, 2);
    assertNull(getDs().find(FacebookUser.class).filter(eq("id", 3L), eq("username", "Jon Snow")).modify(inc("loginCount", 4)).execute(new ModifyOptions().returnDocument(BEFORE).upsert(true)));
    FacebookUser user = getDs().find(FacebookUser.class).filter(eq("id", 3)).first();
    assertEquals(user.loginCount, 4);
    assertEquals(user.username, "Jon Snow");
    FacebookUser results = getDs().find(FacebookUser.class).filter(eq("id", 4L), eq("username", "Ron Swanson")).modify(inc("loginCount")).execute(new ModifyOptions().returnDocument(AFTER).upsert(true));
    assertEquals(results.loginCount, 1);
    assertEquals(results.username, "Ron Swanson");
    user = getDs().find(FacebookUser.class).filter(eq("id", 4)).iterator(new FindOptions().limit(1)).next();
    assertEquals(user.loginCount, 1);
    assertEquals(user.username, "Ron Swanson");
}
Also used : FindOptions(dev.morphia.query.FindOptions) ModifyOptions(dev.morphia.ModifyOptions) FacebookUser(dev.morphia.test.models.FacebookUser) Test(org.testng.annotations.Test)

Example 20 with FindOptions

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

the class TestLifecycles method testMultipleCallbackAnnotation.

@Test
public void testMultipleCallbackAnnotation() {
    final SomeEntity entity = new SomeEntity();
    Assert.assertFalse(entity.isPersistent());
    getDs().save(entity);
    Assert.assertTrue(entity.isPersistent());
    final SomeEntity reloaded = getDs().find(SomeEntity.class).filter(eq("id", entity.getId())).iterator(new FindOptions().limit(1)).tryNext();
    Assert.assertTrue(reloaded.isPersistent());
}
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