Search in sources :

Example 1 with DocumentWriter

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);
}
Also used : DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Sample(dev.morphia.aggregation.experimental.stages.Sample) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 2 with DocumentWriter

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);
}
Also used : DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Skip(dev.morphia.aggregation.experimental.stages.Skip) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 3 with DocumentWriter

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

Example 4 with DocumentWriter

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;
}
Also used : Codec(org.bson.codecs.Codec) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) ArrayList(java.util.ArrayList) Stage(dev.morphia.aggregation.experimental.stages.Stage) Document(org.bson.Document) CodecRegistry(org.bson.codecs.configuration.CodecRegistry)

Example 5 with DocumentWriter

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);
}
Also used : Codec(org.bson.codecs.Codec) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Document(org.bson.Document)

Aggregations

DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)23 Document (org.bson.Document)18 Test (org.testng.annotations.Test)9 Codec (org.bson.codecs.Codec)5 Datastore (dev.morphia.Datastore)3 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)2 OperationTarget (dev.morphia.query.OperationTarget)2 Filter (dev.morphia.query.experimental.filters.Filter)2 Sample (dev.morphia.aggregation.experimental.stages.Sample)1 Skip (dev.morphia.aggregation.experimental.stages.Skip)1 Stage (dev.morphia.aggregation.experimental.stages.Stage)1 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)1 PropertyHandler (dev.morphia.mapping.codec.pojo.PropertyHandler)1 ReferenceException (dev.morphia.mapping.lazy.proxy.ReferenceException)1 NearFilter (dev.morphia.query.experimental.filters.NearFilter)1 User (dev.morphia.test.models.User)1 ArrayList (java.util.ArrayList)1 EncoderContext (org.bson.codecs.EncoderContext)1 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)1