 (defn search* [#^DBCollection collection query keys limit skip order-by count?]
   (let [cursor (if query
                  (if keys
-                   (.find collection #^DBObject (db-object query) #^DBObject (db-object keys))
-                   (.find collection #^DBObject (db-object query)))
+                   (.find collection
+                          #^DBObject (to-dbo query)
+                          #^DBObject (to-dbo keys))
+                   (.find collection
+                          #^DBObject (to-dbo query)))
                  (.find collection ))
-        cursor (if limit (.limit cursor limit) cursor)
-        cursor (if skip (.skip cursor skip) cursor)
-        cursor (if order-by (.sort cursor (db-object (apply merge order-by))) cursor)
-        cursor (if count? (.count cursor) cursor)]
-    (map clojure-object cursor)))
+        cursor (if limit
+                 (.limit cursor limit)
+                 cursor)
+        cursor (if skip
+                 (.skip cursor skip)
+                 cursor)
+        cursor (if order-by
+                 (.sort cursor (to-dbo
+                                (apply merge order-by)))
+                   cursor)]
+    (if count?
+      (.count cursor)
+      (map to-clj cursor))
+))
 
 (defnk fetch [collection query :limit nil :skip nil :include nil :exclude nil :order-by nil :count false]
   (search* collection


(defn insert [#^DBCollection collection & objs]
  (if (= 1 (count objs))
    (clojure-object
     (.insert collection #^DBObject (db-object (first objs))))
    (map clojure-object
         (.insert collection #^java.util.List (map db-object objs)))))

(defn update [#^DBCollection collection query obj & options]
  (let [o? #(has-option? options %)]
    (clojure-object (.update collection
                             (db-object query)
                             (db-object obj)
                             (o? :upsert)
                             (o? :multi)))))


(defn distinct-values [#^DBCollection collection s]
  (seq (.distinct collection (name s))))

(defn group [#^DBCollection collection keys cond initial reduce]
  (map to-clj (.group collection
                              (to-dbo (zipmap (map name keys)
                                                 (repeat true)))
                              (to-dbo cond)
                              (to-dbo initial)
                              #^String reduce)))

(defn delete [#^DBCollection collection obj]
  (.remove collection (to-dbo obj)))


(defn asc [k]  {k 1})
(defn desc [k] {k -1})
