简体   繁体   English

Scala MongoDB Casbah需要构建动态$ or查询

[英]Scala MongoDB Casbah need to build a dynamic $or query

Using Scala, MongoDB, Casbah. 使用Scala,MongoDB,Casbah。

Given a random list of strings: 给定一个随机的字符串列表:

  val names = {
    val listBuffer = new ListBuffer[String]
    for(n <- 1 to (new Random().nextInt(5) + 1)){
      val name = ((new Random().nextInt(26) + 65).asInstanceOf[Char]).toString
      listBuffer += name
    }
    listBuffer.toList
  }

Given a MongoDB document structure: 给定一个MongoDB文档结构:

"_id": <uuid>  
"name": <string>  

How do I find all documents that have a name equal to an entry in my list using a single single MongoDBCollection.find() statement? 如何使用单个 MongoDBCollection.find()语句查找名称与列表中条目相同的所有文档? (ie using $or) (即使用$ or)

Thanks, - Don 谢谢-唐

MongoDB has conditional operator $in that lets test if a field's value is in a list of values ( documentation ) MongoDB有条件运算符$in ,它可以测试字段的值是否在值列表中( 文档

collection.find({name: {$in: ["aaa", "bbb", "ccc"]}})

In Casbah this will look like 在卡斯巴,这看起来像

collection.find("name" $in names)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM