コード例 #1
0
  @Test
  public void shouldAggregateEmptyCollection() {

    Aggregation aggregation =
        newAggregation( //
            project("tags"), //
            unwind("tags"), //
            group("tags") //
                .count()
                .as("n"), //
            project("n") //
                .and("tag")
                .previousOperation(), //
            sort(DESC, "n") //
            );

    AggregationResults<TagCount> results =
        mongoTemplate.aggregate(aggregation, INPUT_COLLECTION, TagCount.class);

    assertThat(results, is(notNullValue()));
    assertThat(results.getServerUsed(), is("/127.0.0.1:27017"));

    List<TagCount> tagCount = results.getMappedResults();

    assertThat(tagCount, is(notNullValue()));
    assertThat(tagCount.size(), is(0));
  }
コード例 #2
0
  @Test
  public void shouldDetectResultMismatch() {

    createTagDocuments();

    Aggregation aggregation =
        newAggregation( //
            project("tags"), //
            unwind("tags"), //
            group("tags") //
                .count()
                .as("count"), // count field not present
            limit(2) //
            );

    AggregationResults<TagCount> results =
        mongoTemplate.aggregate(aggregation, INPUT_COLLECTION, TagCount.class);

    assertThat(results, is(notNullValue()));
    assertThat(results.getServerUsed(), is("/127.0.0.1:27017"));

    List<TagCount> tagCount = results.getMappedResults();

    assertThat(tagCount, is(notNullValue()));
    assertThat(tagCount.size(), is(2));
    assertTagCount(null, 0, tagCount.get(0));
    assertTagCount(null, 0, tagCount.get(1));
  }
コード例 #3
0
  @Test
  public void shouldAggregate() {

    createTagDocuments();

    Aggregation agg =
        newAggregation( //
            project("tags"), //
            unwind("tags"), //
            group("tags") //
                .count()
                .as("n"), //
            project("n") //
                .and("tag")
                .previousOperation(), //
            sort(DESC, "n") //
            );

    AggregationResults<TagCount> results =
        mongoTemplate.aggregate(agg, INPUT_COLLECTION, TagCount.class);

    assertThat(results, is(notNullValue()));
    assertThat(results.getServerUsed(), is("/127.0.0.1:27017"));

    List<TagCount> tagCount = results.getMappedResults();

    assertThat(tagCount, is(notNullValue()));
    assertThat(tagCount.size(), is(3));

    assertTagCount("spring", 3, tagCount.get(0));
    assertTagCount("mongodb", 2, tagCount.get(1));
    assertTagCount("nosql", 1, tagCount.get(2));
  }