mongodb - 如何使用MongoDB在Grails中创建两个并置(或)并(和)的条件?

标签 mongodb grails criteria grails-2.2

我正在尝试从用户或其团队获得的所有 Activity (如果设置了某些属性)也按某些类型进行过滤。

这就是我现在所拥有的:

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

最佳答案

您可以从SQL Angular 考虑您的条件。

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
}

关于mongodb - 如何使用MongoDB在Grails中创建两个并置(或)并(和)的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16525519/

相关文章:

postgresql - 如何使用 jpa 条件生成器在 postgresql 中将 json 列作为字符串获取?

javascript - 使用 Mongoose 将非常大的 CSV 保存到 mongoDB

php - 如何禁用 ORM 并完全排除从 Symfony 应用程序到 mySQL 的任何连接?

javascript - 使用 tablekit 对从 AJAX 方法返回的表进行排序

grails - 使用 jquery UI 插件时,属性日期必须是有效的日期错误

jpa - 如何指定具有 "MEMBER IN"的 JPA 标准 API 以及映射父类(super class)中的多对多关系?

mongodb - 单个字段可以在 MongoDB 中设置为过期吗?

java - 使用 mongoTemplate 在嵌入式文档字段上进行 MongoDB 投影

java - Grails:堆栈红色区域的无效访问

hibernate - Grails:标准构建器中的嵌套关​​联问题