2.7. Querying Mongodb

This chapter describes the querying functionality of the Mongodb module.

2.7.1. Maven integration

Add the following dependencies to your Maven project and make sure that the Maven 2 repo of Mysema Source (http://source.mysema.com/maven2/releases) is accessible from your POM if the version cannot yet be found in other public Maven repos :

<dependency>
  <groupId>com.mysema.querydsl</groupId>
  <artifactId>querydsl-mongodb</artifactId>
  <version>${querydsl.version}</version>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.6.1</version>
</dependency>    

And now, configure the Maven APT plugin which generates the query types used by Querydsl :

<project>
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>com.mysema.maven</groupId>
        <artifactId>maven-apt-plugin</artifactId>
        <version>1.0</version>
        <executions>
          <execution>
            <goals>
              <goal>process</goal>
            </goals>
            <configuration>
              <outputDirectory>target/generated-sources/java</outputDirectory>
              <processor>com.mysema.query.mongodb.MongodbAnnotationProcessor</processor>
            </configuration>
          </execution>
        </executions>
      </plugin>
    ...
    </plugins>
  </build>
</project>

The MongodbAnnotationProcessor finds domain types annotated with the com.google.code.morphia.annotations.Entity annotation and generates Querydsl query types for them.

Run clean install and you will get your Query types generated into target/generated-sources/java.

If you use Eclipse, run mvn eclipse:eclipse to update your Eclipse project to include target/generated-sources/java as a source folder.

Now you are able to construct Mongodb queries and instances of the query domain model.

2.7.2. Querying

Querying with Queryds Mongodb is as simple as this :

QUser user = new QUser("user");
MongodbQuery<User> query = new MongodbQuery<User>(morphia, datastore, user);
List<User> list = query
	.where(user.firstName.eq("Bob"))
	.list();

2.7.3. General usage

Use the the cascading methods of the MongodbQuery class like this

where : Define the query filters, either in varargs form separated via commas or cascaded via the and-operator. Supported operations are operations performed on PStrings except matches, indexOf, charAt. Currently in is not supported, but will be in the future.

orderBy : Define the ordering of the result as an varargs array of order expressions. Use asc() and desc() on numeric, string and other comparable expression to access the OrderSpecifier instances.

limit, offset, restrict : Define the paging of the result. Limit for max results, offset for skipping rows and restrict for defining both in one call.

2.7.4. Ordering

The syntax for declaring ordering is

 
query
    .where(doc.title.like("*"))
    .orderBy(doc.title.asc(), doc.year.desc())
    .list();

The results are sorted ascending based on title and year.

2.7.5. Limit

The syntax for declaring a limit is

 
query
    .where(doc.title.like("*"))
    .limit(10)
    .list();

2.7.6. Offset

The syntax for declaring an offset is

 
query
    .where(doc.title.like("*"))
    .offset(3)
    .list();