use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class CodecStructureTest method testSample.
@Test
public void testSample() {
DocumentWriter writer = new DocumentWriter(getMapper());
getDs().getCodecRegistry().get(Sample.class).encode(writer, Sample.sample(15L), EncoderContext.builder().build());
Document actual = writer.getDocument();
assertEquals(((Document) actual.get("$sample")).getLong("size").longValue(), 15L);
}
use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class CodecStructureTest method testSkip.
@Test
public void testSkip() {
DocumentWriter writer = new DocumentWriter(getMapper());
getDs().getCodecRegistry().get(Skip.class).encode(writer, Skip.skip(15L), EncoderContext.builder().build());
Document actual = writer.getDocument();
assertEquals(actual.getLong("$skip").longValue(), 15L);
}
use of dev.morphia.mapping.codec.writer.DocumentWriter 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());
}
};
}
use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class PipelineUpdate method toDocument.
@SuppressWarnings({ "unchecked", "rawtypes" })
private List<Document> toDocument() {
CodecRegistry registry = datastore.getCodecRegistry();
List<Document> documents = new ArrayList<>();
for (Stage update : updates) {
DocumentWriter writer = new DocumentWriter(datastore.getMapper());
Codec codec = registry.get(update.getClass());
codec.encode(writer, update, EncoderContext.builder().build());
documents.add(writer.getDocument());
}
return documents;
}
use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class Geo2dSphereCriteria method toDocument.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Document toDocument() {
Document query;
dev.morphia.query.FilterOperator operator = getOperator();
DocumentWriter writer = new DocumentWriter(getDatastore().getMapper());
((Codec) getDatastore().getCodecRegistry().get(geometry.getClass())).encode(writer, geometry, EncoderContext.builder().build());
Document document = new Document("$geometry", writer.getDocument());
if (operator == dev.morphia.query.FilterOperator.NEAR || operator == dev.morphia.query.FilterOperator.NEAR_SPHERE) {
if (options != null) {
document.putAll(options);
}
query = new Document(dev.morphia.query.FilterOperator.NEAR.val(), document);
} else if (operator == dev.morphia.query.FilterOperator.GEO_WITHIN || operator == dev.morphia.query.FilterOperator.INTERSECTS) {
query = new Document(operator.val(), document);
if (crs != null) {
((Document) document.get("$geometry")).put("crs", crs);
}
} else {
throw new UnsupportedOperationException(String.format("Operator %s not supported for geo-query", operator.val()));
}
return new Document(getField(), query);
}
Aggregations