(Quick Reference)

4 Querying - Reference Documentation

Authors: Juri Kuehn

Version: 0.8.0

4 Querying

You have two options for getting objects out of your MongoDB: by using queries/filters or the dynamic finders.

Filters

The mongo domain classes provide find and findAll methods that accept a map with filtering constraints and optional query options. Every filter consists of a fieldname, operator and a value. If no operator is given, = is assumed.

Examples

Exampledomain.findAll(["yearsOfOperation >": 5], [max:20, sort:'yearsOfOperation'])
Exampledomain.findAll(["rooms.maxBeds >=": 2, "rooms.occupied": false])
Exampledomain.findAll(["rooms.bathrooms exists": 1])
Exampledomain.findAll(["stars in": [3,4]])
Exampledomain.findAll(["age >=": age])
Exampledomain.findAll(["age =": age])
Exampledomain.findAll(["age": age]) // (if no operator, = is assumed)
Exampledomain.findAll(["age !=": age])
Exampledomain.findAll(["age in": ageList])
Exampledomain.findAll(["customers.loyaltyYears in", yearsList])

Valid operators are "=", "==","!=", "<>", ">", "<", ">=", "<=", "in", "nin", "all", "size", "exists". Under the covers the plugin uses Morhias Datastore and its Query implementation

Query options

You can pass query options to find / findAll for skipping, limiting and sorting your resultset. The query options are represented by a map like max: 30, offset: 10, sort:"lastname,firstname"

max Limits the result set to given number of entries

offset Skips the given number of results

sort Sorts the results. You can provide multiple fieldnames delimited by comma. If you want the results to be sorted in descending order, you have to prepend the fieldname with a minus: sort:"-lastname,-firstname,age"

Dynamic finders

The dynamic finders provided by mongodb-morphia work just as the default gorm finders (example: Car.findAllByNameAndPsGreaterThan("Polo", 50, sort: "-ps")).