Search in sources :

Example 21 with Datastore

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

the class SetEntityOperator method toTarget.

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

        @Override
        @SuppressWarnings("unchecked")
        public Object encode(Datastore datastore) {
            Object value = value();
            EntityModel entityModel = datastore.getMapper().getEntityModel(value.getClass());
            PropertyModel versionProperty = entityModel.getVersionProperty();
            if (versionProperty == null) {
                return super.encode(datastore);
            }
            Codec<Object> codec = datastore.getCodecRegistry().get((Class<Object>) value.getClass());
            DocumentWriter writer = new DocumentWriter(datastore.getMapper());
            codec.encode(writer, value, EncoderContext.builder().build());
            Document document = writer.getDocument();
            document.remove(versionProperty.getMappedName());
            return document;
        }
    };
}
Also used : Datastore(dev.morphia.Datastore) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) OperationTarget(dev.morphia.query.OperationTarget) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Document(org.bson.Document)

Example 22 with Datastore

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

the class TestGeoQueries method maxDistance.

@Test
public void maxDistance() {
    // given
    double latitude = 51.5286416;
    double longitude = -0.1015987;
    Datastore datastore = getDs();
    City london = datastore.save(new City("London", new Point(new Position(latitude, longitude))));
    datastore.save(List.of(new City("Manchester", new Point(new Position(53.4722454, -2.2235922))), new City("Sevilla", new Point(new Position(37.3753708, -5.9550582)))));
    getDs().ensureIndexes();
    // when
    List<City> cities = datastore.find(City.class).filter(near("location", new Point(new Position(latitude, longitude))).maxDistance(200000.0)).iterator().toList();
    // then
    assertThat(cities.size(), is(1));
    assertThat(cities.get(0), is(london));
}
Also used : Datastore(dev.morphia.Datastore) Position(com.mongodb.client.model.geojson.Position) Point(com.mongodb.client.model.geojson.Point) Test(org.testng.annotations.Test)

Example 23 with Datastore

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

the class TestMapperOptions method discriminator.

@Test
public void discriminator() {
    Datastore datastore = Morphia.createDatastore(getMongoClient(), getDatabase().getName(), MapperOptions.builder().discriminator(DiscriminatorFunction.lowerSimpleName()).build());
    datastore.getMapper().map(EntityDiscriminator.class, EmbeddedDiscriminator.class, HasMap.class);
    EntityModel entityModel = datastore.getMapper().getEntityModel(EntityDiscriminator.class);
    assertEquals(entityModel.getDiscriminatorKey(), "_t");
    assertEquals(entityModel.getDiscriminator(), "h");
    entityModel = datastore.getMapper().getEntityModel(EmbeddedDiscriminator.class);
    assertEquals(entityModel.getDiscriminatorKey(), "_e");
    assertEquals(entityModel.getDiscriminator(), "b");
    entityModel = datastore.getMapper().getEntityModel(HasMap.class);
    assertEquals(entityModel.getDiscriminatorKey(), "_t");
    assertEquals(entityModel.getDiscriminator(), HasMap.class.getSimpleName().toLowerCase());
}
Also used : Datastore(dev.morphia.Datastore) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Test(org.testng.annotations.Test)

Example 24 with Datastore

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

the class LocaleMappingTest method testLocaleMapping.

@Test
public void testLocaleMapping() {
    E e = new E();
    e.l1 = Locale.CANADA_FRENCH;
    e.l2 = Arrays.asList(Locale.GERMANY, Locale.TRADITIONAL_CHINESE);
    e.l3 = new Locale[] { Locale.TRADITIONAL_CHINESE, Locale.FRENCH };
    getDs().save(e);
    final Datastore datastore = getDs();
    e = datastore.find(E.class).filter(eq("_id", e.id)).first();
    assertEquals(e.l1, Locale.CANADA_FRENCH);
    assertEquals(e.l2.size(), 2);
    assertEquals(e.l2.get(0), Locale.GERMANY);
    assertEquals(e.l2.get(1), Locale.TRADITIONAL_CHINESE);
    assertEquals(e.l3.length, 2);
    assertEquals(e.l3[0], Locale.TRADITIONAL_CHINESE);
    assertEquals(e.l3[1], Locale.FRENCH);
}
Also used : Datastore(dev.morphia.Datastore) Test(org.testng.annotations.Test)

Example 25 with Datastore

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

the class TestIndexes method testIndexes.

@Test
public void testIndexes() {
    final Datastore datastore = getDs();
    datastore.ensureIndexes(TestWithIndexOption.class);
    List<Document> indexInfo = getIndexInfo(TestWithIndexOption.class);
    assertEquals(indexInfo.size(), 2);
    assertBackground(indexInfo);
    for (Document document : indexInfo) {
        if (document.get("name").equals("collated")) {
            assertEquals(document.get("partialFilterExpression"), parse("{ name : { $exists : true } }"));
            Document collation = (Document) document.get("collation");
            collation.remove("version");
            Document parse = parse("{ 'locale': 'en_US', " + "'alternate': 'shifted'," + "'backwards': true," + "'caseFirst': 'upper'," + "'caseLevel': true," + "'maxVariable': 'space'," + "'normalization': true," + "'numericOrdering': true," + "'strength': 5 }");
            assertEquals(collation, parse, collation.toJson());
        }
    }
    datastore.ensureIndexes(TestWithDeprecatedIndex.class);
    assertEquals(getIndexInfo(TestWithDeprecatedIndex.class).size(), 2);
    assertBackground(getIndexInfo(TestWithDeprecatedIndex.class));
    datastore.ensureIndexes(TestWithHashedIndex.class);
    assertEquals(getIndexInfo(TestWithHashedIndex.class).size(), 2);
    assertHashed(getIndexInfo(TestWithHashedIndex.class));
}
Also used : Datastore(dev.morphia.Datastore) Document(org.bson.Document) Test(org.testng.annotations.Test)

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