@Test
  public void testIssue260() {
    createContentAndVersionData();
    String map = "function () { emit(this.document_id, this.version); }";
    String reduce = "function (key, values) { return Math.max.apply(Math, values); }";
    MapReduceResults<ContentAndVersion> results =
        mongoTemplate.mapReduce(
            "jmr2",
            map,
            reduce,
            new MapReduceOptions().outputCollection("jmr2_out"),
            ContentAndVersion.class);

    int size = 0;
    for (ContentAndVersion cv : results) {
      if (cv.getId().equals("Resume")) {
        assertEquals(6, cv.getValue().longValue());
      }
      if (cv.getId().equals("Schema")) {
        assertEquals(2, cv.getValue().longValue());
      }
      if (cv.getId().equals("mongoDB How-To")) {
        assertEquals(2, cv.getValue().longValue());
      }
      size++;
    }
    assertEquals(3, size);
  }
 @Test
 public void testIssue260Part2() {
   createNumberAndVersionData();
   String map = "function () { emit(this.number, this.version); }";
   String reduce = "function (key, values) { return Math.max.apply(Math, values); }";
   MapReduceResults<NumberAndVersion> results =
       mongoTemplate.mapReduce(
           "jmr2",
           map,
           reduce,
           new MapReduceOptions().outputCollection("jmr2_out"),
           NumberAndVersion.class);
   int size = 0;
   for (NumberAndVersion nv : results) {
     if (nv.getId().equals("1")) {
       assertEquals(2, nv.getValue().longValue());
     }
     if (nv.getId().equals("2")) {
       assertEquals(6, nv.getValue().longValue());
     }
     if (nv.getId().equals("3")) {
       assertEquals(2, nv.getValue().longValue());
     }
     size++;
   }
   assertEquals(3, size);
 }
Пример #3
1
  @Override
  public Map<String, Integer> analyseIngredientsByPopularity() {
    MapReduceResults<IngredientAnalysis> results =
        mongoTemplate.mapReduce(
            "menu",
            "classpath:ingredientsmap.js",
            "classpath:ingredientsreduce.js",
            IngredientAnalysis.class);

    Map<String, Integer> analysis = new HashMap<>();

    for (IngredientAnalysis ingredientAnalysis : results) {
      analysis.put(ingredientAnalysis.getId(), ingredientAnalysis.getValue());
    }

    return analysis;
  }
 private void performMapReduce(boolean inline, boolean withQuery) {
   createMapReduceData();
   MapReduceResults<ValueObject> results;
   if (inline) {
     if (withQuery) {
       results =
           mongoTemplate.mapReduce(
               new Query(), "jmr1", "classpath:map.js", "classpath:reduce.js", ValueObject.class);
     } else {
       results = mongoTemplate.mapReduce("jmr1", mapFunction, reduceFunction, ValueObject.class);
     }
   } else {
     if (withQuery) {
       results =
           mongoTemplate.mapReduce(
               new Query(),
               "jmr1",
               mapFunction,
               reduceFunction,
               options().outputCollection("jmr1_out"),
               ValueObject.class);
     } else {
       results =
           mongoTemplate.mapReduce(
               "jmr1",
               mapFunction,
               reduceFunction,
               new MapReduceOptions().outputCollection("jmr1_out"),
               ValueObject.class);
     }
   }
   Map<String, Float> m = copyToMap(results);
   assertMapReduceResults(m);
 }
 @Test
 @Ignore
 public void testForDocs() {
   createMapReduceData();
   MapReduceResults<ValueObject> results =
       mongoTemplate.mapReduce("jmr1", mapFunction, reduceFunction, ValueObject.class);
   for (ValueObject valueObject : results) {
     System.out.println(valueObject);
   }
 }
  @Test
  public void testMapReduceExcludeQuery() {
    createMapReduceData();

    Query query = new Query(where("x").ne(new String[] {"a", "b"}));
    MapReduceResults<ValueObject> results =
        mongoTemplate.mapReduce(query, "jmr1", mapFunction, reduceFunction, ValueObject.class);

    Map<String, Float> m = copyToMap(results);
    assertEquals(3, m.size());
    assertEquals(1, m.get("b").intValue());
    assertEquals(2, m.get("c").intValue());
    assertEquals(1, m.get("d").intValue());
  }
  @Test
  public void testMapReduceInlineWithScope() {
    createMapReduceData();

    Map<String, Object> scopeVariables = new HashMap<String, Object>();
    scopeVariables.put("exclude", "a");

    String mapWithExcludeFunction =
        "function(){ for ( var i=0; i<this.x.length; i++ ){ if(this.x[i] != exclude) emit( this.x[i] , 1 ); } }";

    MapReduceResults<ValueObject> results =
        mongoTemplate.mapReduce(
            "jmr1",
            mapWithExcludeFunction,
            reduceFunction,
            new MapReduceOptions().scopeVariables(scopeVariables).outputTypeInline(),
            ValueObject.class);
    Map<String, Float> m = copyToMap(results);
    assertEquals(3, m.size());
    assertEquals(2, m.get("b").intValue());
    assertEquals(2, m.get("c").intValue());
    assertEquals(1, m.get("d").intValue());
  }