Initial implementation of monger.collection/find-one

This commit is contained in:
Michael S. Klishin 2011-08-29 19:36:11 +04:00
parent b8c545257f
commit 1ee5436841
2 changed files with 66 additions and 4 deletions

View file

@ -16,7 +16,9 @@
;; API
;;
;;
;; monger.collection/insert
;;
(defn ^WriteResult insert
([^String collection, ^DBObject document]
@ -35,7 +37,9 @@
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.insert #^DBCollection coll #^List (to-db-object documents) #^WriteConcern concern))))
;;
;; monger.collection/find
;;
(declare fields-to-db-object)
(defn ^DBCursor find
@ -48,10 +52,27 @@
([^String collection, ^Map ref, ^List fields]
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)
map-of-fields (fields-to-db-object fields)]
(.find #^DBCollection coll #^DBObject (to-db-object ref) #^DBObject (to-db-object map-of-fields))))
)
(.find #^DBCollection coll #^DBObject (to-db-object ref) #^DBObject (to-db-object map-of-fields)))))
;;
;; monger.collection/find-one
;;
(defn ^DBCursor find-one
([^String collection, ^Map ref]
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.find #^DBCollection coll #^DBObject (to-db-object ref))))
([^String collection, ^Map ref, ^List fields]
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)
map-of-fields (fields-to-db-object fields)]
(.find #^DBCollection coll #^DBObject (to-db-object ref) #^DBObject (to-db-object map-of-fields)))))
;;
;; monger.collection/find-by-id
;;
(defn ^DBObject find-by-id
([^String collection, ^String id]
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
@ -62,10 +83,16 @@
(.findOne #^DBCollection coll #^DBObject (to-db-object { :_id id }) #^DBObject (to-db-object map-of-fields)))))
;;
;; monger.collection/group
;;
;; TBD
;;
;; monger.collection/count
;;
(defn ^long count
([^String collection]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]

View file

@ -1,7 +1,7 @@
(set! *warn-on-reflection* true)
(ns monger.test.collection
(:import [com.mongodb WriteResult WriteConcern DBCursor])
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject])
(:require [monger core collection errors util] [clojure stacktrace])
(:use [clojure.test]))
@ -107,6 +107,41 @@
(is (instance? DBCursor cursor))))
;;
;; find-one
;;
(deftest find-one-full-document-when-collection-is-empty
(let [collection "docs"]
(monger.collection/remove collection)
(def cursor (monger.collection/find-one collection {}))
(is (instance? DBCursor cursor))
(is (empty? cursor))))
(deftest find-one-full-document-when-collection-has-matches
(let [collection "docs"
doc-id (monger.util/random-uuid)
doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }]
(monger.collection/remove collection)
(monger.collection/insert collection doc)
(def cursor (monger.collection/find-one collection { :language "Clojure" }))
(is (= (:_id doc) (.get (.next #^DBCursor cursor) "_id")))))
(deftest find-one-full-document-when-collection-has-matches
(let [collection "docs"
doc-id (monger.util/random-uuid)
doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }
fields [:language]]
(monger.collection/remove collection)
(monger.collection/insert collection doc)
(def cursor (monger.collection/find-one collection { :language "Clojure" } fields))
(def #^DBObject loaded (.next #^DBCursor cursor))
(is (nil? (.get #^DBObject loaded "data-stire")))
(is (= doc-id (.get #^DBObject loaded "_id")))
(is (= "Clojure" (.get #^DBObject loaded "language")))))
;;
;; find-by-id