Search in sources :

Example 6 with Datastore

use of dev.morphia.Datastore 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 7 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class TestVersioning method testMethodMapping.

@Test
public void testMethodMapping() {
    Datastore datastore = createDatastore(getMongoClient(), TEST_DB_NAME, MapperOptions.builder().propertyDiscovery(PropertyDiscovery.METHODS).build());
    datastore.getMapper().map(MethodMappedUser.class);
    MethodMappedUser user = new MethodMappedUser();
    assertEquals(user.getVersion(), null);
    datastore.save(user);
    assertEquals(user.getVersion(), Long.valueOf(1L));
}
Also used : MethodMappedUser(dev.morphia.test.models.methods.MethodMappedUser) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) Test(org.testng.annotations.Test)

Example 8 with Datastore

use of dev.morphia.Datastore 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 9 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class TestArrayUpdates method testUpdatesWithArrayFilters.

@Test
public void testUpdatesWithArrayFilters() {
    getMapper().map(Student.class, Grade.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> grade80 = datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.marks", 80));
    Query<Student> grade90 = datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.marks", 90));
    assertNotNull(grade80.iterator().tryNext());
    assertNotNull(grade90.iterator().tryNext());
    Query<Student> student = datastore.find(Student.class).filter(eq("_id", 1L));
    student.update(inc("grades.$[elem].marks", 5)).execute(new UpdateOptions().arrayFilter(lt("elem.marks", 90)));
    assertNull(grade80.iterator().tryNext());
    assertNotNull(grade90.iterator().tryNext());
    assertNotNull(datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.marks", 85)).iterator().tryNext());
    assertNotNull(grade90.iterator().tryNext());
    student.update(inc("grades.$[elem].marks", 5)).execute(new UpdateOptions().arrayFilter(lt("elem.marks", 90).not()));
    assertNull(grade90.iterator().tryNext());
    assertNotNull(datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.marks", 95)).iterator().tryNext());
}
Also used : Datastore(dev.morphia.Datastore) Grade(dev.morphia.test.models.Grade) Student(dev.morphia.test.models.Student) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 10 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class PullOperator method toTarget.

@Override
public OperationTarget toTarget(PathTarget pathTarget) {
    return new OperationTarget(pathTarget, value()) {

        @Override
        public Object encode(Datastore datastore) {
            DocumentWriter writer = new DocumentWriter(datastore.getMapper());
            document(writer, () -> {
                ((Filter) getValue()).encode(datastore, writer, EncoderContext.builder().build());
            });
            return new Document(field(), writer.getDocument());
        }
    };
}
Also used : Datastore(dev.morphia.Datastore) Filter(dev.morphia.query.experimental.filters.Filter) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) OperationTarget(dev.morphia.query.OperationTarget) Document(org.bson.Document)

Aggregations

Datastore (dev.morphia.Datastore)46 Test (org.testng.annotations.Test)38 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)12 FindOptions (dev.morphia.query.FindOptions)12 ArrayList (java.util.ArrayList)9 Document (org.bson.Document)9 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)6 UpdateOptions (dev.morphia.UpdateOptions)5 UpdateResult (com.mongodb.client.result.UpdateResult)4 MapperOptions (dev.morphia.mapping.MapperOptions)4 ObjectId (org.bson.types.ObjectId)4 Mapper (dev.morphia.mapping.Mapper)3 DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)3 Circle (dev.morphia.test.models.Circle)3 Rectangle (dev.morphia.test.models.Rectangle)3 Versioned (dev.morphia.test.models.versioned.Versioned)3 Point (com.mongodb.client.model.geojson.Point)2 Position (com.mongodb.client.model.geojson.Position)2 DeleteOptions (dev.morphia.DeleteOptions)2 Key (dev.morphia.Key)2