From 1ee5436841f6674e01a060be72f72f78eb61ebcb Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Mon, 29 Aug 2011 19:36:11 +0400 Subject: [PATCH] Initial implementation of monger.collection/find-one --- src/monger/collection.clj | 33 ++++++++++++++++++++++++++--- test/monger/test/collection.clj | 37 ++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 8bf9346..2cbae45 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -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)] diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index c8005bc..bb864e8 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -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