How to implement mongo project stage compute in java driver?

793 Views Asked by At

I want to compute a new field using project stage in aggregation pipeline in Java driver. I want create new field by just multiplying value (that is coming from previous stages) with 100.

 Bson projectGroup=   Aggregates.project(
                    Projections.fields(
                    Projections.computed("computed", "{'$multiply':[100,'value']}}}")
                    )
                    );

Result has the computed field but its value is static expression

""{'$multiply':[100,'value']}}}""

How to get run the expression and get the value.

1

There are 1 best solutions below

0
On BEST ANSWER

Answering my own question for others. We need to parse the expression as follows

 Bson projectGroup=   Aggregates.project(
                Projections.fields(
                Projections.computed("computed",  Document.parse("{'$multiply':[100,'value']}}}"))
                )
                );