Search in sources :

Example 1 with Stage

use of dev.morphia.aggregation.experimental.stages.Stage 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 2 with Stage

use of dev.morphia.aggregation.experimental.stages.Stage in project morphia by mongodb.

the class LookupCodec method encodeStage.

@SuppressWarnings("unchecked")
@Override
protected void encodeStage(BsonWriter writer, Lookup value, EncoderContext encoderContext) {
    document(writer, () -> {
        if (value.getFrom() != null) {
            writer.writeString("from", value.getFrom());
        } else {
            writer.writeString("from", getDatastore().getMapper().getEntityModel(value.getFromType()).getCollectionName());
        }
        List<Stage> pipeline = value.getPipeline();
        if (pipeline == null) {
            writer.writeString("localField", value.getLocalField());
            writer.writeString("foreignField", value.getForeignField());
        } else {
            ExpressionHelper.expression(getDatastore(), writer, "let", value.getVariables(), encoderContext);
            array(writer, "pipeline", () -> {
                for (Stage stage : pipeline) {
                    Codec<Stage> codec = (Codec<Stage>) getCodecRegistry().get(stage.getClass());
                    codec.encode(writer, stage, encoderContext);
                }
            });
        }
        writer.writeString("as", value.getAs());
    });
}
Also used : Codec(org.bson.codecs.Codec) Stage(dev.morphia.aggregation.experimental.stages.Stage)

Example 3 with Stage

use of dev.morphia.aggregation.experimental.stages.Stage in project morphia by mongodb.

the class FacetCodec method encodeStage.

@Override
protected void encodeStage(BsonWriter writer, Facet value, EncoderContext encoderContext) {
    document(writer, () -> {
        for (Entry<String, List<Stage>> entry : value.getFields().entrySet()) {
            array(writer, entry.getKey(), () -> {
                List<Stage> stages = entry.getValue();
                for (Stage stage : stages) {
                    Codec codec = getCodecRegistry().get(stage.getClass());
                    codec.encode(writer, stage, encoderContext);
                }
            });
        }
    });
}
Also used : Codec(org.bson.codecs.Codec) Stage(dev.morphia.aggregation.experimental.stages.Stage) List(java.util.List)

Aggregations

Stage (dev.morphia.aggregation.experimental.stages.Stage)3 Codec (org.bson.codecs.Codec)3 DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Document (org.bson.Document)1 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)1