diff --git a/src/monger/query.clj b/src/monger/query.clj index 916b7de..2b0092a 100644 --- a/src/monger/query.clj +++ b/src/monger/query.clj @@ -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)) diff --git a/test/monger/test/querying.clj b/test/monger/test/querying.clj index 44d5caa..494f740 100644 --- a/test/monger/test/querying.clj +++ b/test/monger/test/querying.clj @@ -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]))))