Skip to content

Instantly share code, notes, and snippets.

@nicholashagen
Created May 28, 2013 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicholashagen/5664999 to your computer and use it in GitHub Desktop.
Save nicholashagen/5664999 to your computer and use it in GitHub Desktop.
MongoDB plugin in Grails and Criteria queries
/* ASSUME FOLLOWING OBJECT MODEL
class Address {
String street
String city
}
class Role {
boolean admin
String type
String description
}
class User {
static embedded = [ 'address' ]
String first
String last
Role role
Address address
}
class Face {
User user
Integer x
Integer y
Integer w
Integer h
String description
}
class Photo {
static embedded = [ 'faces' ]
User user
String description
List<String> tags
List<Face> faces
}
class Gallery {
static embedded = [ 'photos' ]
User user
String name
String description
List<Photo> photos
}
*/
// THIS WORKS AND PRODUCES MONGODB QUERY: query: { photos: { $elemMatch: { faces: { $elemMatch: { x: 1 } } } } }
def criteria = Gallery.createCriteria()
criteria.list {
photos {
faces {
eq('x', 1)
}
}
}
// THIS FAILS PRODUCING QUERY (NOTE MISSING elemMatch): query: { photos: { $elemMatch: { faces: { $elemMatch: {} } } } }
Gallery.findAll {
photos {
faces {
eq('x', 1)
}
}
}
// THIS ALSO FAILS PRODUCING SAME QUERY: query: { photos: { $elemMatch: { faces: { $elemMatch: {} } } } }
Gallery.where {
photos {
faces {
eq('x', 1)
}
}
}.list()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment