mongodbgrailsgrails-ormmongodb-javagorm-mongodb

find by key in a map in grails domain mongodb


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


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