commit
f499079bf7
2 changed files with 69 additions and 5 deletions
|
|
@ -80,10 +80,16 @@
|
|||
;; (mgcol/find-maps "languages" { :tags { $nin [ "functional" ] } } )
|
||||
(defoperator $nin)
|
||||
|
||||
;; $eq is "equals" comparator
|
||||
;;
|
||||
;; EXAMPLES:
|
||||
;; (monger.collection/find "libraries" { :language { $eq "Clojure" }})
|
||||
(defoperator $eq)
|
||||
|
||||
;; $ne is "non-equals" comparator
|
||||
;;
|
||||
;; EXAMPLES:
|
||||
;; (monger.collection/find "libraries" {$ne { :language "Clojure" }})
|
||||
;; (monger.collection/find "libraries" { :language { $ne "Clojure" }})
|
||||
(defoperator $ne)
|
||||
|
||||
;; $elemMatch checks if an element in an array matches the specified expression
|
||||
|
|
@ -96,6 +102,13 @@
|
|||
(defoperator $regex)
|
||||
(defoperator $options)
|
||||
|
||||
;; Matches documents that satisfy a JavaScript expression.
|
||||
;;
|
||||
;; EXAMPLES:
|
||||
;;
|
||||
;; (monger.collection/find "people" { $where "this.placeOfBirth === this.address.city" })
|
||||
(defoperator $where)
|
||||
|
||||
;;
|
||||
;; LOGIC OPERATORS
|
||||
;;
|
||||
|
|
@ -275,9 +288,18 @@
|
|||
(defoperator $ifNull)
|
||||
(defoperator $cond)
|
||||
|
||||
;; Geospatial
|
||||
(defoperator $geoWithin)
|
||||
(defoperator $geoIntersects)
|
||||
(defoperator $near)
|
||||
(defoperator $nearSphere)
|
||||
(defoperator $geometry)
|
||||
(defoperator $maxDistance)
|
||||
(defoperator $minDistance)
|
||||
(defoperator $center)
|
||||
(defoperator $centerSphere)
|
||||
(defoperator $box)
|
||||
(defoperator $polygon)
|
||||
|
||||
(defoperator $slice)
|
||||
|
||||
|
|
@ -292,3 +314,9 @@
|
|||
;; EXAMPLES:
|
||||
;; (mgcol/update coll { :_id oid } { $currentDate { :lastModified true } })
|
||||
(defoperator $currentDate)
|
||||
|
||||
;; Isolates intermediate multi-document updates from other clients.
|
||||
;;
|
||||
;; EXAMPLES:
|
||||
;; (mgcol/update "libraries" { :language "Clojure", $isolated 1 } { $inc { :popularity 1 } } {:multi true})
|
||||
(defoperator $isolated)
|
||||
|
|
|
|||
|
|
@ -3,10 +3,28 @@
|
|||
[monger.collection :as mc]
|
||||
[monger.js :as js]
|
||||
[clojure.test :refer :all]
|
||||
[monger.operators :refer :all]))
|
||||
[clojure.set :refer [difference]]
|
||||
[monger.operators :refer :all])
|
||||
(:import [com.mongodb QueryOperators]))
|
||||
|
||||
;; (use-fixtures :each purge-people purge-docs purge-things purge-libraries)
|
||||
|
||||
(deftest every-query-operator-is-defined
|
||||
(let [driver-query-operators (->> (.getDeclaredFields QueryOperators) (map #(.get % nil)) set)
|
||||
monger-query-operators (->> (ns-publics 'monger.operators) (map (comp name first)) set)
|
||||
; $within is deprecated and replaced by $geoWithin since v2.4.
|
||||
; $uniqueDocs is deprecated since v2.6.
|
||||
deprecated-query-operators #{"$within" "$uniqueDocs"}
|
||||
; Query modifier operators that are deprecated in the mongo shell since v3.2
|
||||
deprecated-meta-operators #{"$comment" "$explain" "$hint" "$maxScan"
|
||||
"$maxTimeMS" "$max" "$min" "$orderby"
|
||||
"$returnKey" "$showDiskLoc" "$snapshot" "$query"}
|
||||
undefined-non-deprecated-operators (difference driver-query-operators
|
||||
deprecated-query-operators
|
||||
deprecated-meta-operators
|
||||
monger-query-operators)]
|
||||
(is (= #{} undefined-non-deprecated-operators))))
|
||||
|
||||
(let [conn (mg/connect)
|
||||
db (mg/get-db conn "monger-test")]
|
||||
(defn purge-collections
|
||||
|
|
@ -36,17 +54,29 @@
|
|||
2 {:users {$lte 5}}
|
||||
1 {:users {$gt 10 $lt 150}})))
|
||||
|
||||
;;
|
||||
;; $eq
|
||||
;;
|
||||
|
||||
(deftest find-with-eq-operator
|
||||
(let [collection "libraries"]
|
||||
(mc/insert-batch db collection [{:language "Ruby" :name "mongoid" :users 1 :displayName nil}
|
||||
{:language "Clojure" :name "langohr" :users 5}
|
||||
{:language "Clojure" :name "incanter" :users 15}
|
||||
{:language "Scala" :name "akka" :users 150}])
|
||||
(is (= 2 (.count (mc/find db collection {:language {$eq "Clojure"}}))))))
|
||||
|
||||
;;
|
||||
;; $ne
|
||||
;;
|
||||
|
||||
(deftest find-with-and-or-operators
|
||||
(deftest find-with-ne-operator
|
||||
(let [collection "libraries"]
|
||||
(mc/insert-batch db 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}])
|
||||
(is (= 2 (.count (mc/find db collection {$ne {:language "Clojure"}}))))))
|
||||
(is (= 2 (.count (mc/find db collection {:language {$ne "Clojure"}}))))))
|
||||
|
||||
|
||||
;;
|
||||
|
|
@ -107,4 +137,10 @@
|
|||
{:language {$regex "clo.*" $options "i"}} 2
|
||||
{:name {$regex "aK.*" $options "i"}} 1
|
||||
{:language {$regex ".*by"}} 1
|
||||
{:language {$regex ".*ala.*"}} 1))))
|
||||
{:language {$regex ".*ala.*"}} 1)))
|
||||
|
||||
(deftest find-with-js-expression
|
||||
(let [collection "people"]
|
||||
(mc/insert-batch db collection [{:name "Bob" :placeOfBirth "New York" :address {:city "New York"}}
|
||||
{:name "Alice" :placeOfBirth "New York" :address {:city "Los Angeles"}}])
|
||||
(is (= 1 (.count (mc/find db collection {$where "this.placeOfBirth === this.address.city"})))))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue