0

我希望使用这样的搜索模式从 clojure 访问 mongo db:

find({Keywords: /search-pattern/})

我有一个名为“soulflyer”的数据库,其中包含一个“图像”集合,每个成员都有一个“关键字”字段,其中包含来自它所代表的图像的 exif 关键字数组。要从 mongo java shell 中搜索我自己的图像,我这样做:

db.getCollection('images').find({Keywords: "Iain Wood"})

我得到一个包含关键字“Iain Wood”的所有条目的列表。如果我在 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 搜索简单的模式匹配?

4

1 回答 1

1

我不确定 monger 是否支持这种开箱即用的子字符串模式匹配,但您可以轻松使用正则表达式。这记录在mongers 查询文档中。您需要使用$regex运算符。像下面这样的东西应该可以工作:

  (mc/find db "images" {"Keywords" {$regex ".*Iain.*"}})
于 2015-12-02T10:26:17.107 回答