Initial version of the monger.query DSL
Here is what it looks like:
(with-collection "docs"
(find { :inception_year { $lt 2000 $gte 2011 } })
(fields { :inception_year 1 :name 1 })
(skip 10)
(limit 20)
(batch-size 50)
(hint "my-index-name")
(snapshot))
This commit is contained in:
parent
c40b0e25c1
commit
bd133c1afc
2 changed files with 172 additions and 72 deletions
89
src/monger/query.clj
Normal file
89
src/monger/query.clj
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
(ns monger.query
|
||||
(:refer-clojure :exclude [select find sort])
|
||||
(:require monger.core)
|
||||
(:import [com.mongodb DB DBCollection DBObject DBCursor]
|
||||
[java.util List])
|
||||
(:use [monger conversion operators]))
|
||||
|
||||
|
||||
;;
|
||||
;; Implementation
|
||||
;;
|
||||
|
||||
(def ^{ :dynamic true } *query-collection*)
|
||||
|
||||
(defn empty-query
|
||||
[^DBCollection coll]
|
||||
{
|
||||
:collection coll
|
||||
:query {}
|
||||
:sort {}
|
||||
:fields []
|
||||
:skip 0
|
||||
:limit 0
|
||||
:batch-size 256
|
||||
:hint nil
|
||||
:snapshot false
|
||||
})
|
||||
|
||||
(defn- fields-to-db-object
|
||||
[^List fields]
|
||||
(to-db-object (zipmap fields (repeat 1))))
|
||||
|
||||
(defn exec
|
||||
[{ :keys [collection query fields skip limit sort batch-size hint snapshot] :or { limit 0 batch-size 256 skip 0 } }]
|
||||
(let [cursor (doto ^DBCursor (.find ^DBCollection collection (to-db-object query) (fields-to-db-object fields))
|
||||
(.limit limit)
|
||||
(.skip skip)
|
||||
(.sort (to-db-object sort))
|
||||
(.batchSize batch-size)
|
||||
(.hint ^DBObject (to-db-object hint))
|
||||
)]
|
||||
(if snapshot
|
||||
(.snapshot cursor))
|
||||
(map (fn [x] (from-db-object x true))
|
||||
(seq cursor))))
|
||||
|
||||
;;
|
||||
;; API
|
||||
;;
|
||||
|
||||
(defn find
|
||||
[m query]
|
||||
(merge m { :query query }))
|
||||
|
||||
(defn fields
|
||||
[m flds]
|
||||
(merge m { :fields flds }))
|
||||
|
||||
(defn sort
|
||||
[m srt]
|
||||
(merge m { :sort srt }))
|
||||
|
||||
(defn skip
|
||||
[m ^long n]
|
||||
(merge m { :skip n }))
|
||||
|
||||
(defn limit
|
||||
[m ^long n]
|
||||
(merge m { :limit n }))
|
||||
|
||||
(defn batch-size
|
||||
[m ^long n]
|
||||
(merge m { :batch-size n }))
|
||||
|
||||
(defn hint
|
||||
[m h]
|
||||
(merge m { :hint h }))
|
||||
|
||||
(defn snapshot
|
||||
[m]
|
||||
(merge m { :snapshot true }))
|
||||
|
||||
(defmacro with-collection
|
||||
[^String coll & body]
|
||||
`(binding [*query-collection* (if (string? ~coll)
|
||||
(.getCollection ^DB monger.core/*mongodb-database* ~coll)
|
||||
~coll)]
|
||||
(let [query# (-> (empty-query *query-collection*) ~@body)]
|
||||
(exec query#))))
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
(set! *warn-on-reflection* true)
|
||||
|
||||
(ns monger.test.querying
|
||||
(:refer-clojure :exclude [select find])
|
||||
(:refer-clojure :exclude [select find sort])
|
||||
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject CommandResult$CommandFailure]
|
||||
[org.bson.types ObjectId]
|
||||
[java.util Date])
|
||||
|
|
@ -10,8 +10,7 @@
|
|||
[monger.result :as mgres])
|
||||
(:use [clojure.test]
|
||||
[monger.test.fixtures]
|
||||
;; [monger.query]
|
||||
[monger.conversion]))
|
||||
[monger conversion query operators]))
|
||||
|
||||
|
||||
(defn purge-locations-collection
|
||||
|
|
@ -78,15 +77,27 @@
|
|||
|
||||
;; < ($lt), <= ($lte), > ($gt), >= ($gte)
|
||||
|
||||
;; (deftest query-using-dsl-and-$lt-operator
|
||||
;; (let [coll "docs"
|
||||
;; doc1 { :language "Clojure" :_id (ObjectId.) :inception_year 2006 }
|
||||
;; doc2 { :language "Java" :_id (ObjectId.) :inception_year 1992 }
|
||||
;; doc3 { :language "Scala" :_id (ObjectId.) :inception_year 2003 }
|
||||
;; _ (mgcol/insert-batch coll [doc1 doc2])
|
||||
;; lt-result (in-collection "docs"
|
||||
;; (find { :inception_year { "$lt" 2000 } })]
|
||||
;; (is (= [doc2] lt-result))))
|
||||
(deftest query-using-dsl-and-$lt-operator-1
|
||||
(let [coll "docs"
|
||||
doc1 { :language "Clojure" :_id (ObjectId.) :inception_year 2006 }
|
||||
doc2 { :language "Java" :_id (ObjectId.) :inception_year 1992 }
|
||||
doc3 { :language "Scala" :_id (ObjectId.) :inception_year 2003 }
|
||||
_ (mgcol/insert-batch coll [doc1 doc2])
|
||||
lt-result (with-collection "docs"
|
||||
(find { :inception_year { $lt 2000 } })
|
||||
(limit 2))]
|
||||
(is (= [doc2] lt-result))))
|
||||
|
||||
(deftest query-using-dsl-and-$lt-operator-2
|
||||
(let [coll "docs"
|
||||
doc1 { :language "Clojure" :_id (ObjectId.) :inception_year 2006 }
|
||||
doc2 { :language "Java" :_id (ObjectId.) :inception_year 1992 }
|
||||
doc3 { :language "Scala" :_id (ObjectId.) :inception_year 2003 }
|
||||
_ (mgcol/insert-batch coll [doc1 doc2])
|
||||
lt-result (with-collection "docs"
|
||||
(find { :inception_year { $lt 2000 } }))]
|
||||
(is (= [doc2] lt-result))))
|
||||
|
||||
|
||||
|
||||
(deftest query-with-find-maps-using-$lt-operator
|
||||
|
|
|
|||
Loading…
Reference in a new issue