在 monger 中使用搜索模式
Use of search patterns with monger
我希望使用如下搜索模式从 clojure 访问 mongo 数据库:
find({Keywords: /search-pattern/})
我有一个名为 "soulflyer" 的数据库,其中包含一个 "images" 集合,每个成员都有一个 "Keywords" 字段,其中包含来自它所代表的图像的一组 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/"}))
returns 一个空列表
(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 搜索简单的模式匹配?
我不确定 monger 是否支持开箱即用的子字符串模式匹配,但您可以轻松使用正则表达式。这记录在 mongers query documentation 中。您需要使用 $regex
运算符。像下面这样的东西应该可以工作:
(mc/find db "images" {"Keywords" {$regex ".*Iain.*"}})
我希望使用如下搜索模式从 clojure 访问 mongo 数据库:
find({Keywords: /search-pattern/})
我有一个名为 "soulflyer" 的数据库,其中包含一个 "images" 集合,每个成员都有一个 "Keywords" 字段,其中包含来自它所代表的图像的一组 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/"}))
returns 一个空列表
(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 搜索简单的模式匹配?
我不确定 monger 是否支持开箱即用的子字符串模式匹配,但您可以轻松使用正则表达式。这记录在 mongers query documentation 中。您需要使用 $regex
运算符。像下面这样的东西应该可以工作:
(mc/find db "images" {"Keywords" {$regex ".*Iain.*"}})