Created
May 28, 2013 18:33
-
-
Save nicholashagen/5664999 to your computer and use it in GitHub Desktop.
MongoDB plugin in Grails and Criteria queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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
i have this problem now. Where you able to fix it?
thanks