1

Using monger, I am writing a document that contains a vector with a keyword item to a collection like

(monger.collection/insert-and-return db 
                                    "test-coll" {:_id 1 :some-vector [:a-keyword]})

which returns as expected

{:_id 1, :some-vector [:a-keyword]}

but then if I fetch the particular document like

(monger.collection/find-map-by-id db "test-coll" 1)

the keyword has been changed to a string

{:_id 1, :some-vector ["a-keyword"]}

Is that expected behaviour and if so why?

Micah Elliott
  • 9,600
  • 5
  • 51
  • 54
Aspasia
  • 1,521
  • 1
  • 13
  • 23

2 Answers2

4

This is expected behaviour as the mongo database store doesn't support keywords; it is essentially json. http://clojuremongodb.info/articles/inserting.html#serialization_of_clojure_data_types_to_dbobject_and_dblist

You will have to manually convert the values back to keywords using monger.conversion/from-db-object.

Sam
  • 2,427
  • 2
  • 23
  • 26
1

The method insert-and-return returns the same data that you have passed to it plus the created document id.

(defn insert-and-return 
    [db coll _]
 ...
  (let [doc (merge {:_id (ObjectId.)} document)]
   (insert db coll doc concern)
   doc))

The method find-map-by-id just fetches data from mongodb and uses the function from-db-object to convert the raw data to clojure data structure where only the keys of a map will be keywordized. The value of your map will not be keywordized.

(from-db-object ^DBObject (find-one db coll ref) true)

;;where
(defprotocol ConvertFromDBObject
  (from-db-object [input keywordize))
Minh Tuan Nguyen
  • 1,026
  • 8
  • 13