This chapter describes the querying functionality of the Mongodb module.
Add the following dependencies to your Maven project:
<dependency> <groupId>com.querydsl</groupId> <artifactId>querydsl-apt</artifactId> <version>${querydsl.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.querydsl</groupId> <artifactId>querydsl-mongodb</artifactId> <version>${querydsl.version}</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>apt-maven-plugin</artifactId> <version>1.1.3</version> <executions> <execution> <goals> <goal>process</goal> </goals> <configuration> <outputDirectory>target/generated-sources/java</outputDirectory> <processor>com.querydsl.apt.morphia.MorphiaAnnotationProcessor</processor> </configuration> </execution> </executions> </plugin> ... </plugins> </build> </project>
The MorphiaAnnotationProcessor 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.
Querying with Querydsl Mongodb with Morphia is as simple as this:
Morphia morphia; Datastore datastore; // ... QUser user = new QUser("user"); MorphiaQuery<User> query = new MorphiaQuery<User>(morphia, datastore, user); List<User> list = query .where(user.firstName.eq("Bob")) .fetch();
Use the the cascading methods of the MongodbQuery class like this
where: Add 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: Add 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: Set the paging of the result. Limit for max results, offset for skipping rows and restrict for defining both in one call.
The syntax for declaring ordering is
query .where(doc.title.like("*")) .orderBy(doc.title.asc(), doc.year.desc()) .fetch();
The results are sorted ascending based on title and year.
The syntax for declaring a limit is
query
.where(doc.title.like("*"))
.limit(10)
.fetch();
The syntax for declaring an offset is
query
.where(doc.title.like("*"))
.offset(3)
.fetch();
Support for geospatial queries is available for Double typed arrays (Double[]) via the near-method:
query .where(geoEntity.location.near(50.0, 50.0)) .fetch();
To select only relevant fields you can use the overloaded projection methods fetch, iterate, fetchOne and fetchFirst methods like this
query .where(doc.title.like("*")) .fetch(doc.title, doc.path);
This query will load only the title and path fields of the documents.