Search in sources :

Example 6 with MapReduceOptions

use of org.springframework.data.mongodb.core.mapreduce.MapReduceOptions in project spring-data-mongodb by spring-projects.

the class MongoTemplateUnitTests method mapReduceShouldPickUpLimitFromOptionsEvenWhenQueryDefinesItDifferently.

// DATAMONGO-1334
@Test
void mapReduceShouldPickUpLimitFromOptionsEvenWhenQueryDefinesItDifferently() {
    MongoCursor cursor = mock(MongoCursor.class);
    MapReduceIterable output = mock(MapReduceIterable.class);
    when(output.limit(anyInt())).thenReturn(output);
    when(output.sort(any())).thenReturn(output);
    when(output.filter(any(Document.class))).thenReturn(output);
    when(output.iterator()).thenReturn(cursor);
    when(cursor.hasNext()).thenReturn(false);
    when(collection.mapReduce(anyString(), anyString(), eq(Document.class))).thenReturn(output);
    Query query = new BasicQuery("{'foo':'bar'}");
    query.limit(100);
    template.mapReduce(query, "collection", "function(){}", "function(key,values){}", new MapReduceOptions().limit(1000), Wrapper.class);
    verify(output, times(1)).limit(1000);
}
Also used : MapReduceOptions(org.springframework.data.mongodb.core.mapreduce.MapReduceOptions) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) NearQuery(org.springframework.data.mongodb.core.query.NearQuery) Query(org.springframework.data.mongodb.core.query.Query) BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) MapReduceIterable(com.mongodb.client.MapReduceIterable) MongoCursor(com.mongodb.client.MongoCursor) Document(org.bson.Document) Test(org.junit.jupiter.api.Test)

Example 7 with MapReduceOptions

use of org.springframework.data.mongodb.core.mapreduce.MapReduceOptions in project spring-data-mongodb by spring-projects.

the class MapReduceTests method performMapReduce.

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", MAP_FUNCTION, REDUCE_FUNCTION, ValueObject.class);
        }
    } else {
        if (withQuery) {
            results = mongoTemplate.mapReduce(new Query(), "jmr1", MAP_FUNCTION, REDUCE_FUNCTION, options().outputCollection("jmr1_out"), ValueObject.class);
        } else {
            results = mongoTemplate.mapReduce("jmr1", MAP_FUNCTION, REDUCE_FUNCTION, new MapReduceOptions().outputCollection("jmr1_out"), ValueObject.class);
        }
    }
    assertMapReduceResults(copyToMap(results));
}
Also used : MapReduceOptions(org.springframework.data.mongodb.core.mapreduce.MapReduceOptions) Query(org.springframework.data.mongodb.core.query.Query)

Aggregations

MapReduceOptions (org.springframework.data.mongodb.core.mapreduce.MapReduceOptions)7 Test (org.junit.jupiter.api.Test)5 Query (org.springframework.data.mongodb.core.query.Query)5 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)4 MapReduceIterable (com.mongodb.client.MapReduceIterable)3 MongoCursor (com.mongodb.client.MongoCursor)3 Document (org.bson.Document)3 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)2 HashMap (java.util.HashMap)1 Test (org.junit.Test)1