One more query DSL composition example, introduce monger.query/partial-query
This commit is contained in:
parent
8300b7c0c4
commit
f3efbcec16
2 changed files with 34 additions and 12 deletions
|
|
@ -34,18 +34,19 @@
|
|||
;; deleted during the query, it may or may not be returned, even with snapshot mode). Note that short query responses
|
||||
;; (less than 1MB) are always effectively snapshotted. Currently, snapshot mode may not be used with sorting or explicit hints.
|
||||
(defn empty-query
|
||||
[^DBCollection coll]
|
||||
{
|
||||
:collection coll
|
||||
:query {}
|
||||
:sort {}
|
||||
:fields []
|
||||
:skip 0
|
||||
:limit 0
|
||||
:batch-size 256
|
||||
:hint nil
|
||||
:snapshot false
|
||||
})
|
||||
([]
|
||||
{
|
||||
:query {}
|
||||
:sort {}
|
||||
:fields []
|
||||
:skip 0
|
||||
:limit 0
|
||||
:batch-size 256
|
||||
:hint nil
|
||||
:snapshot false
|
||||
})
|
||||
([^DBCollection coll]
|
||||
(merge (empty-query) { :collection coll })))
|
||||
|
||||
(defn- fields-to-db-object
|
||||
[^List fields]
|
||||
|
|
@ -117,3 +118,7 @@
|
|||
~coll)]
|
||||
(let [query# (-> (empty-query *query-collection*) ~@body)]
|
||||
(exec query#))))
|
||||
|
||||
(defmacro partial-query
|
||||
[& body]
|
||||
`(-> {} ~@body))
|
||||
|
|
|
|||
|
|
@ -216,3 +216,20 @@
|
|||
(is (= [doc1 doc5 doc7] result1))
|
||||
(is (= [doc2 doc6 doc4] result2))
|
||||
(is (= [doc3] result3))))
|
||||
|
||||
|
||||
(deftest combined-querying-dsl-example1
|
||||
(let [coll "docs"
|
||||
ma-doc { :_id (ObjectId.) :name "Massachusetts" :iso "MA" :population 6547629 :joined_in 1788 :capital "Boston" }
|
||||
de-doc { :_id (ObjectId.) :name "Delaware" :iso "DE" :population 897934 :joined_in 1787 :capital "Dover" }
|
||||
ny-doc { :_id (ObjectId.) :name "New York" :iso "NY" :population 19378102 :joined_in 1788 :capital "Albany" }
|
||||
ca-doc { :_id (ObjectId.) :name "California" :iso "CA" :population 37253956 :joined_in 1850 :capital "Sacramento" }
|
||||
tx-doc { :_id (ObjectId.) :name "Texas" :iso "TX" :population 25145561 :joined_in 1845 :capital "Austin" }
|
||||
top3 (partial-query (limit 3))
|
||||
by-population-desc (partial-query (sort { :population -1 }))
|
||||
_ (mgcol/insert-batch coll [ma-doc de-doc ny-doc ca-doc tx-doc])
|
||||
result (with-collection coll
|
||||
(find {})
|
||||
(merge top3)
|
||||
(merge by-population-desc))]
|
||||
(is (= result [ca-doc tx-doc ny-doc]))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue