XenoN
XenoN

Reputation: 995

Grails Gorm MongoDB index Embedded String Map

I wanted to make some translatable content with rest service so i decided to create collection with this structure. But I can't find BSON by value from String Map. class LocalizableString{

   static mapWith = "mongo"
   ObjectId id
   Map<String, String> values = new HashMap<String, String>();
}

Then i wanted to get like this. But it works like join query.

def list = LocalizableString.createCriteria().list {
        values{ like('value',"%${value}%") }
    }

Here is similar plain mongo example. But how can i implement it with gorm mongoDB http://www.mongodb.org/display/DOCS/Schema+Design#SchemaDesign-Example

Is there any solution for that ?

Upvotes: 1

Views: 1021

Answers (2)

XenoN
XenoN

Reputation: 995

class BaseService {

    def findByLocalizableString(def domainClass ,def query , def field ,def params = null) {

       def q = new BasicDBObject()
       def queryList = []
       def allowedLanguages = ConfigurationHolder.config.grails.localizableString.allowedLanguages
       allowedLanguages.each { locale ->
          queryList.add(new BasicDBObject("values.${locale}", new BasicDBObject('$regex', /.*${query}.*/)))
    }
       q.put('$or',queryList)
       def lsc = LocalizableString.collection.find(q)

       def list = lsc.hasNext() ? domainClass.createCriteria().list(params) {
          or {
              while (lsc.hasNext()) {
                  def n = lsc.next()
                  eq("${field}",n._id)
              }
          }
       } : null
       return list
   }
 }

Upvotes: 2

Will Buck
Will Buck

Reputation: 1510

I'm not 100% on this, but I'm fairly certain the Mongo GORM plugin does not work with criteria relation traversal, which is what this looks like (despite not really being like that).

From mongoGorm website (http://blog.mongodb.org/post/18510469058/grails-in-the-land-of-mongodb):

Some of the GORM features that are not supported include:

Criteria queries on associations

HQL

Groovy SQL

So you may need to rethink the Map structure you have as a data model here :/ Anyone more experienced can weigh in?

Upvotes: 0

Related Questions