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