0

i have a code like this on grails mongodb

//myDomain
class Plugin {
    List<String> projects = []
    //projects has name and description
}

//myController
def query = null
    if (params.q != null)
    {
        def q = '%'+ params.q +'%'
        query = Plugin.where {
            (projects.name =~ q || projects.name ==~ q)
        }
    }
//return null

why i can't find plugin by projects.name?

embedded.kyle
  • 10,976
  • 5
  • 37
  • 56
rsijaya
  • 159
  • 2
  • 14

1 Answers1

0

From the property definition

List<String> projects = []

the elements of projects are Strings, that's why you can not query projects.name, which is not known to GORM.

And in your last comment, the value in a projects instance "projects": { "0": { "description": "123", "name": "cms" }, "1": { "description": "cms", "name": "codebucks" } } looks like a Map, where the project number is the key and a inner map containing name and description is the value, rather than a List.

To serve your purpose, you need to modify your domain class definition to ensure consistency between your model and data.

coderLMN
  • 3,076
  • 1
  • 21
  • 26
  • hi. thx for your answer, i finish it with createCriteria, i get projects.name, thx anyway. – rsijaya Jan 24 '13 at 15:43
  • So you made it with `List projects = []`? I wonder how did you do that. Could you please post your solution as an answer? – coderLMN Jan 24 '13 at 17:14
  • yes, i use List projects, and my controller method def listPluginByProject() { def objectInstanceList = [] def q = '%'+ params.q +'%' def c = Plugin.createCriteria() def results = c.list { like('projects.name', q) } objectInstanceList = results } – rsijaya Jan 25 '13 at 02:29
  • i don't know why createCriteria still works without hibernate, i use grails 2.1.1, but maybe for safe i try with your suggest to modify domain class, or find another query. – rsijaya Jan 25 '13 at 03:17
  • Because mongodb gorm plugin provides similar functions. – coderLMN Jan 25 '13 at 03:27