Search in sources :

Example 1 with Query

use of io.lumeer.engine.api.data.Query in project engine by Lumeer.

the class MongoDbStorageTest method testAggregate.

public void testAggregate() {
    mongoDbStorage.createCollection(COLLECTION_AGGREGATE);
    mongoDbStorage.createDocument(COLLECTION_AGGREGATE, getTestDocument("a", "1", "val1"));
    mongoDbStorage.createDocument(COLLECTION_AGGREGATE, getTestDocument("a", "2", "val2"));
    mongoDbStorage.createDocument(COLLECTION_AGGREGATE, getTestDocument("a", "3", "val3"));
    mongoDbStorage.createDocument(COLLECTION_AGGREGATE, getTestDocument("a", "4", "val6"));
    mongoDbStorage.createDocument(COLLECTION_AGGREGATE, getTestDocument("b", "1", "val4"));
    mongoDbStorage.createDocument(COLLECTION_AGGREGATE, getTestDocument("c", "1", "val5"));
    final DataDocument filters = new DataDocument();
    final DataDocument condition = new DataDocument();
    condition.put("$gt", 2);
    filters.put("param2", condition);
    final DataDocument grouping = new DataDocument();
    final DataDocument groupId = new DataDocument();
    final DataDocument aggregate = new DataDocument();
    groupId.put("param1", "$param1");
    aggregate.put("$sum", "$param2");
    grouping.put("_id", groupId);
    grouping.put("added", aggregate);
    final Query q = new Query(filters);
    q.setGrouping(grouping);
    q.setCollections(Collections.singleton(COLLECTION_AGGREGATE));
    List<DataDocument> result = mongoDbStorage.query(q);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getDataDocument("_id")).containsOnlyKeys("param1");
    assertThat(result.get(0).getString("_id.param1")).isEqualTo("a");
    assertThat(result.get(0).get("added")).isEqualTo(7);
    final DataDocument project = new DataDocument();
    final DataDocument multiply = new DataDocument();
    multiply.put("$multiply", Arrays.asList("$param2", 5));
    project.put("param4", multiply);
    final DataDocument sort = new DataDocument();
    sort.put("param4", -1);
    q.setGrouping(new DataDocument());
    q.setProjections(project);
    q.setSorting(sort);
    result = mongoDbStorage.query(q);
    assertThat(result).hasSize(2);
    assertThat(result.get(0).get("param4")).isEqualTo(20);
    assertThat(result.get(1).get("param4")).isEqualTo(15);
    q.setSkip(1);
    result = mongoDbStorage.query(q);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).get("param4")).isEqualTo(15);
    q.setSkip(0);
    q.setLimit(1);
    result = mongoDbStorage.query(q);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).get("param4")).isEqualTo(20);
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Query(io.lumeer.engine.api.data.Query)

Aggregations

DataDocument (io.lumeer.engine.api.data.DataDocument)1 Query (io.lumeer.engine.api.data.Query)1