0

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

akash777.sharma
  • 702
  • 10
  • 30

2 Answers2

1

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"])
Lalit Agarwal
  • 2,354
  • 1
  • 14
  • 18
1

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

R. S.
  • 402
  • 5
  • 17
  • Thanks. Following code is also working : ArrayList userList = User.createCriteria().list() { eq("baseAddr.city", "New Delhi") } – akash777.sharma Apr 21 '14 at 07:20