【问题标题】:Casbah/Salat: How to query a field that a part of a string is contained?Casbah/Salat:如何查询包含部分字符串的字段?
【发布时间】:2012-01-13 09:05:23
【问题描述】:

我尝试使用 Casbah 和 Salat 编写查询来查询包含名称部分的字段。 我尝试使用这样的正则表达式(在 SalatDAO 中):

val regexp = (""".*"""+serverName+""".*""").r
val query = "serverName" -> regexp
val result = find(MongoDBObject(query))

val regexp = ".*"+serverName+".*"

记录在 MongoDB 中,当我使用完整名称搜索它时,它可以工作。

告诉 casbah 搜索字符串的一部分的正确方法是什么?

我想修复的另一件事是参数的字符串连接。 有没有用casbah转义输入参数的默认方式,所以参数不是 解释为 javascript 命令?

最好的问候, 奥利弗

【问题讨论】:

  • 这是我查询的部分数据:case class Machine(_id: ObjectId = new ObjectId, serverName: Option[String],
  • 好的。看来我修好了。 'val query = "serverName" -> regexp.r'
  • 我做错的下一件事是使用正则表达式对列表进行查询。字段是:ips: List[String] = List[String](), 如果尝试使用:val regexp = ".*" + parameter + ".*" val nameQ = "serverName" -> regexp.r val ipsQ = "ips" $elemMatch regexp.r val query = $or (("serverName" -> regexp.r), ("ips" $elemMatch regexp.r)) 但这会破坏签名:No implicit view available from scala.util.matching.Regex => com.mongodb.DBObject

标签: scala mongodb casbah salat


【解决方案1】:

在 mongodb shell 中,您可以找到包含特定字符串的服务器名称

db.collection.find({serverName:/whatever/i})

我对casbah没有任何经验,我相信它一定是这样的。请测试

val regexp = ("""/"""+serverName+"""/i""").r  
find(MongoDBObject("serverName" -> regexp))

【讨论】:

  • 我认为您不需要提供 /es。对我来说,它现在适用于 ".*" + serverName + ".*"val result = find(MongoDBObject("serverName" -> regexp.r))
猜你喜欢
  • 1970-01-01
  • 2011-05-14
  • 1970-01-01
  • 2022-06-14
  • 2018-10-27
  • 2022-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多