From acdaffba99ad70d0790a01ba8e94e68f442d5fac Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Fri, 25 May 2012 14:39:35 +0400 Subject: [PATCH] Add $regex and $options operator macros --- ChangeLog.md | 4 +++- src/monger/operators.clj | 7 +++++-- test/monger/test/query_operators_test.clj | 13 +++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index fbaecf6..f9184b3 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,6 +1,8 @@ ## Changes between 1.0.0-beta7 and 1.0.0-beta8 -No changes yet. +### More Operators + +Two new operator macros: `$regex`, `$options`. diff --git a/src/monger/operators.clj b/src/monger/operators.clj index cbcf520..dcd5435 100644 --- a/src/monger/operators.clj +++ b/src/monger/operators.clj @@ -12,8 +12,9 @@ (defmacro ^{:private true} defoperator [operator] (let [op# (str operator) - op-sym# (symbol op#)] - `(def ~op-sym# ~op#))) + op-sym# (symbol op#) + meta {:const true}] + `(def ^{:const true} ~op-sym# ~op#))) ;; ;; QUERY OPERATORS @@ -66,6 +67,8 @@ ;; (monger.collection/find "comments" { $elemMatch { :text "Nice!" :rating { $gte 1 } } }) (defoperator $elemMatch) +(defoperator $regex) +(defoperator $options) ;; ;; LOGIC OPERATORS diff --git a/test/monger/test/query_operators_test.clj b/test/monger/test/query_operators_test.clj index 74cae52..544abf4 100644 --- a/test/monger/test/query_operators_test.clj +++ b/test/monger/test/query_operators_test.clj @@ -97,3 +97,16 @@ 1 { :comments { $elemMatch { :text "Nice!" :rating { $gte 1 } } } } 2 { "comments.rating" 1 } 1 { "comments.rating" { $gt 3 } }))) + +(deftest ^{:focus true} find-with-regex-operator + (let [collection "libraries"] + (mgcol/insert-batch collection [{ :language "Ruby", :name "Mongoid" :users 1} + { :language "Clojure", :name "Langohr" :users 5 } + { :language "Clojure", :name "Incanter" :users 15 } + { :language "Scala", :name "Akka" :users 150}]) + (are [query results] (is (= results (.count (mgcol/find collection query)))) + {:language {$regex "Clo.*" }} 2 + {:language {$regex "clo.*" $options "i"}} 2 + {:name {$regex "aK.*" $options "i"}} 1 + {:language {$regex ".*by" }} 1 + {:language {$regex ".*ala.*" }} 1)))