Reputation: 702
I am using mongodb plugin with grails (latest versions).
I have a domain similar to:
class User
{
HashMap baseAddr
static mapWith = "mongo"
}
The User data in DB is like:
{
"_id" : NumberLong(1),
"baseAddr" : {
"buildingNo" : "",
"level" : "",
"side" : "",
"street" : "asdfasdf",
"zipcode" : "asdfasdf",
"state" : null,
"municipality" : null,
"flatNo" : "adsfasdf",
"city" : "New Delhi",
"country" : "IN"
},
"version" : 0
}
I want to find all those Users who have baseAddr.city == "New Delhi" using grails dynamic finder or criteria. Please help
Upvotes: 0
Views: 578
Reputation: 400
You can use with criteria of gorm here is code
def testData = User.withCriteria {
eq("baseAddr.city", "New Delhi")
}
i tested this code this working fine
Upvotes: 1
Reputation: 2354
The way you have defined your domain model, I think it is not possible to do this with finder or criteria.
You should have created an Address domain and associated that domain here in User class instead.
class User
{
Address baseAddr
static mapWith = "mongo"
}
Class Address
{
String buildingNo
....
}
However with the current implementation which you have you could directly use Gmongo in your code and get the data out.
def mongo = new GMongo()
def db = mongo.getDB("db")
def citydoc = db.user.find(["baseAddr.city": "New Delhi"])
Upvotes: 1