[英]Casbah (Scala) Generating a query statement with nested conditions ($OR $AND)
Given items: [{ prop1 : "a", prop2 : "b" , prop3, "c",....},....]
and patterns: [{ prop1 : "a", prop2 : "Any"},...]
给定项目: [{ prop1 : "a", prop2 : "b" , prop3, "c",....},....]
和模式: [{ prop1 : "a", prop2 : "Any"},...]
I want to create a query to find all patterns that match the given items. 我想创建一个查询来查找与给定项目匹配的所有模式。
The resulting query is of form: 结果查询的形式为:
((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))
....
I've tried to build a DSL-form , but I get an ambiguous implicit error on the init: 我试图建立一个DSL形式,但是在初始化时得到了一个模棱两可的隐式错误:
Can this notation be used? 可以使用该符号吗? Or how could I implement this with DBObject.Builder or MongoDbObjects? 或者如何用DBObject.Builder或MongoDbObjects实现呢?
Thanks, Eli 谢谢,以利
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" )
var pattern_query = $or() // *causes a compilation error
things_list.foreach ( thing => {
var att_and_list = $and() // *causes a compilation error
atts_for_search.foreach ( att => {
att_and_list ++= $or(att $eq thing(att),att $exists false,att $eq "Any")
}) // foreach attribute
pattern_query ++= att_and_list
})
The $or
and $and
parts of the dsl require a traversable and can't initiated without one - hence the compile error. $or
和$and
部分dsl需要一个可遍历的函数,并且没有一个就不能启动-因此发生编译错误。
If you can just delay the creation of the $and
and $or
parts of the query until you have built the traversable it works: 如果您可以仅延迟查询的$and
和$or
的创建,直到您构建了遍历,它就会起作用:
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)
This returns the following output: 这将返回以下输出:
{"$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"}]}]}]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.