Implement partial document fetching for monger.collection/find-by-id

This commit is contained in:
Michael S. Klishin 2011-08-14 22:36:54 +04:00
parent 2620263103
commit 90daa7c967
2 changed files with 41 additions and 37 deletions

View file

@ -1,26 +1,16 @@
;; Copyright (c) 2011 Michael S. Klishin ;; Copyright (c) 2011 Michael S. Klishin
;; ;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy ;; The use and distribution terms for this software are covered by the
;; of this software and associated documentation files (the "Software"), to deal ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; in the Software without restriction, including without limitation the rights ;; which can be found in the file epl-v10.html at the root of this distribution.
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ;; By using this software in any fashion, you are agreeing to be bound by
;; copies of the Software, and to permit persons to whom the Software is ;; the terms of this license.
;; furnished to do so, subject to the following conditions: ;; You must not remove this notice, or any other, from this software.
;;
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
(ns monger.collection (ns monger.collection
(:import (com.mongodb Mongo DB WriteResult DBObject WriteConcern DBCursor) (java.util List Map)) (:import (com.mongodb Mongo DB DBCollection WriteResult DBObject WriteConcern DBCursor) (java.util List Map))
(:require [monger core convertion errors])) (:require [monger core errors])
(:use [monger.convertion]))
;; ;;
;; API ;; API
@ -30,30 +20,41 @@
(defn ^WriteResult insert (defn ^WriteResult insert
([^String collection, ^DBObject doc] ([^String collection, ^DBObject doc]
(.insert (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object doc) monger.core/*mongodb-write-concern*)) (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.insert coll (to-db-object doc) monger.core/*mongodb-write-concern*)))
([^String collection, ^DBObject doc, ^WriteConcern concern] ([^String collection, ^DBObject doc, ^WriteConcern concern]
(.insert (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object doc) concern))) (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.insert coll (to-db-object doc) concern))))
(defn ^WriteResult insert-batch (defn ^WriteResult insert-batch
([^String collection, ^List docs] ([^String collection, ^List docs]
(.insert (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object docs) WriteConcern/NORMAL)) (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.insert coll (to-db-object docs) WriteConcern/NORMAL)))
([^String collection, ^List docs, ^WriteConcern concern] ([^String collection, ^List docs, ^WriteConcern concern]
(.insert (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object docs) concern))) (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.insert coll (to-db-object docs) concern))))
;; monger.collection/find ;; monger.collection/find
(defn ^DBCursor find (defn ^DBCursor find
([^String collection] ([^String collection]
(.find (.getCollection monger.core/*mongodb-database* collection))) (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.find coll)))
([^String collection, ^Map ref] ([^String collection, ^Map ref]
(.find (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object ref))) (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.find coll (to-db-object ref))))
) )
(defn ^DBObject find-by-id (defn ^DBObject find-by-id
([^String collection, ^String id] ([^String collection, ^String id]
(.findOne (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object { :_id id }))) (.findOne (.getCollection monger.core/*mongodb-database* collection) (to-db-object { :_id id })))
([^String collection, ^String id, ^List fields]
(let [n (count fields)
ones (replicate 10 1)
map-of-fields (zipmap fields ones)]
(.findOne (.getCollection monger.core/*mongodb-database* collection) (to-db-object { :_id id }) (to-db-object map-of-fields))))
) )
@ -63,7 +64,8 @@
;; monger.collection/count ;; monger.collection/count
(defn ^long count (defn ^long count
[^String collection] [^String collection]
(.count (.getCollection monger.core/*mongodb-database* collection))) (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.count coll)))
;; monger.collection/update ;; monger.collection/update
;; monger.collection/update-multi ;; monger.collection/update-multi
@ -71,9 +73,11 @@
(defn ^WriteResult remove (defn ^WriteResult remove
([^String collection] ([^String collection]
(.remove (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object {}))) (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
([^String collection, ^DBObject conditions] (.remove coll (to-db-object {}))))
(.remove (.getCollection monger.core/*mongodb-database* collection) (monger.convertion/to-db-object conditions))) ([^String collection, ^Map conditions]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.remove coll (to-db-object conditions))))
) )
;; monger.collection/ensure-index ;; monger.collection/ensure-index

View file

@ -93,10 +93,10 @@
(monger.collection/insert collection doc) (monger.collection/insert collection doc)
(is (= (doc (monger.collection/find-by-id collection doc-id)))))) (is (= (doc (monger.collection/find-by-id collection doc-id))))))
;; (deftest find-partial-document-by-id-when-document-exists (deftest find-partial-document-by-id-when-document-exists
;; (let [collection "libraries" (let [collection "libraries"
;; doc-id (monger.util/random-str 140 16) doc-id (monger.util/random-str 140 16)
;; doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }] doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }]
;; (monger.collection/remove collection) (monger.collection/remove collection)
;; (monger.collection/insert collection doc) (monger.collection/insert collection doc)
;; (is (= (doc (monger.collection/find-by-id collection doc-id [])))))) (is (= ({ :language "Clojure" } (monger.collection/find-by-id collection doc-id [ :language ]))))))