我希望使用如下的搜索模式从clojure访问mongo db:
find({Keywords: /search-pattern/})我有一个名为"soulflyer“的数据库,包含一个”图像“集合,每个成员都有一个”关键字“字段,其中包含来自它所代表的图像的exif关键字数组。为了从mongo shell中搜索自己的图像,我执行以下操作:
db.getCollection('images').find({Keywords: "Iain Wood"})我得到了一个包含关键字"Iain“的所有条目的列表。如果我在repl中这样做,这在clojure中也很好:
(def connection (mg/connect))
(def db (mg/get-db connection "soulflyer"))
(seq (mc/find db "images" {"Keywords" "Iain Wood"}))但是,我想搜索关键字上的部分匹配。使用这样的命令,从java shell可以正常工作:
db.getCollection('images').find({Keywords: /Iain/})正如预期的那样,我用包含"Iain“的关键字返回所有图像。但是,我无法从clojure中找到如何使其工作。
(seq (mc/find db "images" {"Keywords" "/Iain/"}))返回空列表
(seq (mc/find db "images" {"Keywords" /Iain/}))
(seq (mc/find db "images" {"Keywords" '/Iain/'}))
(seq (mc/find db "images" {"Keywords" \/Iain\/}))
(seq (mc/find db "images" {"Keywords" "\/Iain\/"}))给我一个LispReader$ReaderException,或者冻结repl。
如何让clojure/monger搜索一个简单的模式匹配?
发布于 2015-12-02 10:26:17
我不确定monger是否支持这种子字符串模式匹配,但您可以很容易地使用正则表达式。这在商人查询文档中有记录。您需要使用$regex操作符。下面这样的东西应该能起作用:
(mc/find db "images" {"Keywords" {$regex ".*Iain.*"}})https://stackoverflow.com/questions/34038117
复制相似问题