Skip to content

Commit

Permalink
Merge remote branch 'christophermaier/distinct'
Browse files Browse the repository at this point in the history
  • Loading branch information
purcell committed Nov 2, 2010
2 parents 06c5d5f + a7b8f7b commit 6d47169
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/somnium/congomongo.clj
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,23 @@ When with-mongo and set-connection! interact, last one wins"
(let [id (if (instance? ObjectId id) id (object-id id))]
(apply fetch col (concat options [:one? true :where {:_id id}]))))

(defunk insert!
(defunk distinct-values
"Queries a collection for the distinct values of a given key.
Returns a vector of the values by default (but see the :as keyword argument).
The key (a String) can refer to a nested object, using dot notation, e.g., \"foo.bar.baz\".
Optional arguments include
:where -> a query object. If supplied, distinct values from the result of the query on the collection (rather than from the entire collection) are returned.
:from -> specifies what form a supplied :where query is in (:clojure, :json, or :mongo). Defaults to :clojure. Has no effect if there is no :where query.
:as -> results format (:clojure, :json, or :mongo). Defaults to :clojure."
{:arglists
'([collection key :where :from :as])}
[coll k :where {} :from :clojure :as :clojure]
(let [query (coerce where [from :mongo])]
(coerce (.distinct (get-coll coll) k query)
[:mongo as])))

(defunk insert!
"Inserts a map into collection. Will not overwrite existing maps.
Takes optional from and to keyword arguments. To insert
as a side-effect only specify :to as nil."
Expand Down Expand Up @@ -249,7 +265,7 @@ When with-mongo and set-connection! interact, last one wins"
(alter-var-root #'*mongo-config* merge {:db db})
(throw (RuntimeException. (str "database with title " title " does not exist.")))))

;;;; go ahead and have these retucn seqs
;;;; go ahead and have these return seqs

(defn databases
"List databases on the mongo server" []
Expand Down
25 changes: 25 additions & 0 deletions test/congomongo_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,31 @@
:where {:_id point-id}))
"suffusion of yellow")))))


(deftest test-distinct-values
(with-test-mongo
(println "testing distinct-values")
(insert! :distinct {:genus "Pan" :species "troglodytes" :common-name "chimpanzee"})
(insert! :distinct {:genus "Pan" :species "pansicus" :common-name "bonobo"})
(insert! :distinct {:genus "Homo" :species "sapiens" :common-name "human"})
(insert! :distinct {:genus "Homo" :species "floresiensis" :common-name "hobbit"})

(is (= (set (distinct-values :distinct "genus"))
#{"Pan" "Homo"}))
(is (= (set (distinct-values :distinct "common-name"))
#{"chimpanzee" "bonobo" "human" "hobbit"}))
(is (= (set (distinct-values :distinct "species" :where {:genus "Pan"}))
#{"troglodytes" "pansicus"}))
(is (= (set (distinct-values :distinct "species" :where "{\"genus\": \"Pan\"}" :from :json))
#{"troglodytes" "pansicus"}))
(let [json (distinct-values :distinct "genus" :as :json)]
;; I don't think you can influence the order in which distinct results are returned,
;; so just check both possibilities
(is (or (= json "[Pan, Homo]")
(= json "[Homo, Pan]"))))
(println "finished with distinct-values")))


;; ;; mass insert chokes on excessively large inserts
;; ;; will need to implement some sort of chunking algorithm

Expand Down

0 comments on commit 6d47169

Please sign in to comment.