Add $regex and $options operator macros

This commit is contained in:
Michael S. Klishin 2012-05-25 14:39:35 +04:00
parent ca67d9f4cb
commit acdaffba99
3 changed files with 21 additions and 3 deletions

View file

@ -1,6 +1,8 @@
## Changes between 1.0.0-beta7 and 1.0.0-beta8 ## Changes between 1.0.0-beta7 and 1.0.0-beta8
No changes yet. ### More Operators
Two new operator macros: `$regex`, `$options`.

View file

@ -12,8 +12,9 @@
(defmacro ^{:private true} defoperator (defmacro ^{:private true} defoperator
[operator] [operator]
(let [op# (str operator) (let [op# (str operator)
op-sym# (symbol op#)] op-sym# (symbol op#)
`(def ~op-sym# ~op#))) meta {:const true}]
`(def ^{:const true} ~op-sym# ~op#)))
;; ;;
;; QUERY OPERATORS ;; QUERY OPERATORS
@ -66,6 +67,8 @@
;; (monger.collection/find "comments" { $elemMatch { :text "Nice!" :rating { $gte 1 } } }) ;; (monger.collection/find "comments" { $elemMatch { :text "Nice!" :rating { $gte 1 } } })
(defoperator $elemMatch) (defoperator $elemMatch)
(defoperator $regex)
(defoperator $options)
;; ;;
;; LOGIC OPERATORS ;; LOGIC OPERATORS

View file

@ -97,3 +97,16 @@
1 { :comments { $elemMatch { :text "Nice!" :rating { $gte 1 } } } } 1 { :comments { $elemMatch { :text "Nice!" :rating { $gte 1 } } } }
2 { "comments.rating" 1 } 2 { "comments.rating" 1 }
1 { "comments.rating" { $gt 3 } }))) 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)))