Tuesday, 24 April 2012

Spring-Data-MongoDB passing scope variables to map\reduce

Spring-Data-MongoDB passing scope variables to map\reduce:

It's becoming a little bit tricky if you want to pass something like wrapper that contains Map of Map's as a parameter to map\reduce.

Anyhow, If you'll find yourself in trouble passing parameters to map\reduce here is one way to handle it:


MapReduceOptions options = MapReduceOptions.options();
Map vars = new HashMap();
        try {
            ObjectMapper mapper = new ObjectMapper();
            DBObject dbo = new BasicDBObject(mapper.convertValue(myobject, Map.class));
            vars.put("myparamName",dbo);
        } catch (Exception e) {
            logger.error(e);
        }
        options.scopeVariables(vars);
this way scope parameter will be passed as a pure JSON map object, which will be automatically mapped to js object by MongoDB.

ObjectMapper is a Jackson framework class.

No comments:

Post a Comment