我正在尝试从用户或其团队获得的所有 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
已应用于user
和team
,但我认为您需要以下内容: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/