mongodbaggregation-frameworkmongodb-javamongodb-java-3.3.0mongodb-java-3.8

How to implement mongo project stage compute in java driver?


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.


Solution

  • 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']}}}"))
                    )
                    );