如何使用MongoDB中的两个析取(或)的连接(和)在Grails中创建一个条件?

时间:2013-05-13 15:16:41

标签: mongodb grails criteria grails-2.2

如果设置了某些属性,我试图让用户或其团队的所有活动也按某些类型进行过滤。

这就是我现在所拥有的:

Activity.withCriteria{
    and{
        or {
            eq 'user',myUser
            "in" 'team',userTeams
        }
        and{
            if (showA || showB || showC){
                or{
                    if (showA){
                        "in" "a", myAList
                    }
                    if (showB){
                        "in" "b", myBList
                    }
                    if (showC){
                        "in" "c",myCList
                    }
                }
            }
        }
    }
    order "date","desc"
    maxResults maxElements
}

执行它,我得到的是用户和团队块以及showA,showB,showC块的OR而不是这两个块的AND。

我正在使用grails 2.2.1(也使用没有Hibernate的MongoDB GORM 1.2.0)

编辑:

我已经能够看到发送到MongoDB的查询,并且它没有执行标准的第一部分。 这是传递给MongoDB的查询:

query: { query: { $or: [ { a: { $in: [ "5191e2c7c6c36183687df8b6", "5191e2c7c6c36183687df8b7", "5191e2c7c6c36183687df8b8" ] } }, { b: { $in: [ "5191e2c7c6c36183687df8b9", "5191e2c7c6c36183687df8ba", "5191e2c7c6c36183687df8bb" ] } }, { c: { $in: [ "5191e2c7c6c36183687df8b5" ] } } ] }, orderby: { date: -1 } }  ntoreturn: 10 ntoskip: 0

编辑:我刚刚看到JIRA已经被提升,似乎是一个MongoDB插件问题...... http://jira.grails.org/browse/GPMONGODB-296

2 个答案:

答案 0 :(得分:1)

您可以在SQL透视图中考虑您的标准。

and ((user = 'myUserValue'
 or   team in (...))
and(a in (...)
  or b in (...)
  or c in (...)))

因此,or已应用于userteam,但我认为您需要以下内容:

or {
  and {
    eq 'user',myUser
    "in" 'team',userTeams
  }
  and{
    if (showA || showB || showC){
      or{
        if (showA){
          "in" "a", myAList
        }
        if (showB){
          "in" "b", myBList
        }
        if (showC){
          "in" "c",myCList
        }
      }
    }
  }
}

所以这里的关键是你声明的块应用于你内在的块。

修改

检查标准的一个好建议是启用Hibernate生成的sql的输出。这可以在 DataSource.groovy

中完成
dataSource {
  logSql = true
}

hibernate {
  format_sql = true
}

答案 1 :(得分:0)

使用Mongo for Grails的新版本已修复,现在它正在使用版本1.3.0。

http://grails.org/plugin/mongodb