Question

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

Was it helpful?

Solution

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

OTHER TIPS

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"])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top