Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb Casbah(Scala)生成带有嵌套条件($或$和)的查询语句_Mongodb_Scala_Conditional Statements_Querydsl_Casbah - Fatal编程技术网

Mongodb Casbah(Scala)生成带有嵌套条件($或$和)的查询语句

Mongodb Casbah(Scala)生成带有嵌套条件($或$和)的查询语句,mongodb,scala,conditional-statements,querydsl,casbah,Mongodb,Scala,Conditional Statements,Querydsl,Casbah,给定的项目:[{prop1:a”,prop2:b,prop3,“c”,…},…] 和模式:[{prop1:“a”,prop2:“Any”},…] 我想创建一个查询来查找与给定项匹配的所有模式 结果查询的形式如下: ((A or B or C) AND (D or E or F) AND (G or H or J)) or ((A or B or C) AND (D or E or F) AND (G or H or J)) or ((A or B or C) AND (D or E or F)

给定的项目:
[{prop1:a”,prop2:b,prop3,“c”,…},…]
和模式:
[{prop1:“a”,prop2:“Any”},…]

我想创建一个查询来查找与给定项匹配的所有模式

结果查询的形式如下:

((A or B or C) AND (D or E or F) AND (G or H or J))
or
((A or B or C) AND (D or E or F) AND (G or H or J))
or
((A or B or C) AND (D or E or F) AND (G or H or J))
....
我尝试构建DSL表单,但在init上出现了一个模棱两可的隐式错误:

这个符号可以用吗?或者我如何用DBObject.Builder或MongoDbObjects实现这一点

谢谢, 伊莱


dsl的
$或
$和
部分需要一个可遍历文件,没有它就无法启动,因此出现编译错误

如果您可以延迟创建查询的
$和
部分和
$或
部分,直到您构建了可遍历对象,那么它就可以工作了:

import com.mongodb.casbah.query.Imports._

/* test data */
val thing1 = Map[String,String]("thing_type" -> "PC", "os"-> "Windows", "vendor"-> "lenova")
val thing2 = Map[String,String]("thing_type" -> "Tablet", "os"-> "iOS", "vendor"-> "Apple")
val things_list = List(thing1, thing2)
/* end test data */

val atts_for_search = List("thing_type", "os", "vendor" )
val ors = scala.collection.mutable.ListBuffer[DBObject]()
things_list.foreach ( thing => {
    var ands = scala.collection.mutable.ListBuffer[DBObject]()
    atts_for_search.foreach ( att => {
          ands += $or(att $eq thing(att),att $exists false,att $eq "Any")

    }) // foreach attribute
    ands  += $and(ands)
})
val pattern_query = $or(ors)
这将返回以下输出:

{"$or": [{ "$and": [{ "$or": [ { "thing_type" : "PC"} , { "thing_type" : { "$exists" : false}} , { "thing_type" : "Any"}]} , 
                    { "$or": [ { "os" : "Windows"} , { "os" : { "$exists" : false}} , { "os" : "Any"}]} , 
                    { "$or": [ { "vendor" : "lenova"} , { "vendor" : { "$exists" : false}} , { "vendor" : "Any"}]}]} , 
         { "$and": [{ "$or": [ { "thing_type" : "Tablet"} , { "thing_type" : { "$exists" : false}} , { "thing_type" : "Any"}]} , 
                    { "$or": [ { "os" : "iOS"} , { "os" : { "$exists" : false}} , { "os" : "Any"}]} , 
                    { "$or": [ { "vendor" : "Apple"} , { "vendor" : { "$exists" : false}} , { "vendor" : "Any"}]}]}]}
{"$or": [{ "$and": [{ "$or": [ { "thing_type" : "PC"} , { "thing_type" : { "$exists" : false}} , { "thing_type" : "Any"}]} , 
                    { "$or": [ { "os" : "Windows"} , { "os" : { "$exists" : false}} , { "os" : "Any"}]} , 
                    { "$or": [ { "vendor" : "lenova"} , { "vendor" : { "$exists" : false}} , { "vendor" : "Any"}]}]} , 
         { "$and": [{ "$or": [ { "thing_type" : "Tablet"} , { "thing_type" : { "$exists" : false}} , { "thing_type" : "Any"}]} , 
                    { "$or": [ { "os" : "iOS"} , { "os" : { "$exists" : false}} , { "os" : "Any"}]} , 
                    { "$or": [ { "vendor" : "Apple"} , { "vendor" : { "$exists" : false}} , { "vendor" : "Any"}]}]}]}