Search code examples
mongodbgrailsgrails-orm

GORM low level api finds object (Mongodb record) but its still NULL?


I have code like this in a controller method:

            DB db = mongoClient.getDB("twcdb");
        DBCollection coll = db.getCollection('countrycodes')
        println coll.findOne()
        println coll.findOne().class

and I get this output at the console:

[_id:539848b2119918654e7e90c3, Country:Bermuda, Alpha2:BM, Aplha3:BMU, Numeric:60, FIPS:BD, IGA:Model 2]
null

So how can it be that it finds a record but its class is null? Is this because this record isn't modeled by any of my domain classes? It does recognize the record's individual fields as Strings which I just tested but the record overall is classed NULL? How, why?


Solution

  • you should never call class on an object as there are scenarios where this could fail (e.g. getProperty('class') gets called or you are on a "mapish" object, which means that groovy will call get('class') for you -- which is the case for the BasicDBObject (subsubclass of a LinkedHashMap)). always use getClass()