Archer
Archer

Reputation: 35

Aggregation error in Spring Data MongoDB

I am currently writing a webapp for my university project. It's a system that allows to create, modify and take language tests. My teacher was interested in my question search module. For DB I use MongoDB. He asked me what if there were 5kk records to view in the browser? He advised me to do some aggregation in MongoDB.

I have to make a search method which will allow me to search by 1 to 5 criteria from the collection documents: category, question, difficulty, question type, number of correct answers. And of course view it in the browser.

At first I tried basic operations like making a new query with criteria, but I need it to work on any number of criteria not all or one. If 1 or more fields are empty they shouldn't be considered in the search, so I'm trying to do an aggregation, but I can't get it to work and don't know if what I am trying to acomplish is even possible with this method.

And here's my problem. In DAO I try to define a aggregation but it doesn't recognize group and where methods for the class(method is undefined).

Here's my code :

    public List<Question> findQuestions(Question question) {
    String questionPattern = question.getQuestion().length() == 0 ? ".*" : ".*" + question.getQuestion() + ".*";

    TypedAggregation<Question> agg = newAggregation(Question.class, group("category"),
            match(where("category").is(question.getCategory()))

        );

        AggregationResults<Question> result = mongoTemplate.aggregate(agg, Question.class);
        List<Question> stateStatsList = result.getMappedResults();
}

and pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pgs.languageskillchecker</groupId>
<artifactId>LanguageSkillChecker</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>LanguageSkillChecker</name>
<url>http://maven.apache.org</url>

<properties>
    <spring.version>3.1.2.RELEASE</spring.version>
</properties>

<dependencies>

    <!-- Spring -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.2.3.RELEASE</version>
    </dependency>

    <!-- Velocity -->

    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity</artifactId>
        <version>1.7</version>
    </dependency>

    <!-- Jackson -->

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.12</version>
    </dependency>

    <!-- Servlet -->

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>

    <!-- MongoDB -->    

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.3.3.RELEASE</version>
    </dependency>

    <!-- Sitemesh -->

    <dependency>
        <groupId>org.sitemesh</groupId>
        <artifactId>sitemesh</artifactId>
        <version>3.0-alpha-1</version>
    </dependency>

    <!-- AspectJ -->

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.6.10</version>
    </dependency>

    <!-- poi -->

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.9</version>
    </dependency>

    <!-- File upload -->

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3</version>
    </dependency>

</dependencies>

<build>
    <finalName>LanguageSkillChecker</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

I of course import the required libraries:

import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.aggregation.Aggregation.*;

I took the aggregation code from Spring IO Docs, but I can't get it to work properly. I'll appreciate any help ^^

Upvotes: 2

Views: 3465

Answers (2)

ikumen
ikumen

Reputation: 11643

And here's my problem. In DAO I try to define a aggregation but it doesn't recognize group and where methods for the class(method is undefined).

Those methods (where and group) are static and defined in the Criteria and Aggregation classes - you're just missing a static import.

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
import static org.springframework.data.mongodb.core.query.Criteria.*;

or more specifically

import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
import static org.springframework.data.mongodb.core.query.Criteria.where;

Upvotes: 3

Vaelyr
Vaelyr

Reputation: 3166

You should take a look at Spring Data Specifications and Querydsl, google helps you to find examples how to create custom search based on dynamic criteria range.

http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/

Upvotes: 0

Related Questions