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 providefind
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.ExamplesExampledomain.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])
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 likemax: 30, offset: 10, sort:"lastname,firstname"
max
Limits the result set to given number of entriesoffset
Skips the given number of resultssort
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")
).