mongodbclojuremonger

Monger Missing codec for java.math.Bigdecimal


Trying to Insert a Document to Mongo using Monger With a Float/Big Decimal in it

(mc/insert db "products" {:name "Shirt" :Price 300.00M })

and gets the following Error .

ERROR compojure.api.exception - Can't find a codec for class java.math.BigDecimal.

The insert works fine when I remove the Price. Am I missing any codec dependency or what am I doing wrong ?. Google search did not help much. Thanks in Advance.


Solution

  • MongoDB does not support Java's BigDecimal serialization/deserialization. What I ended up doing is creating my own serialization/deserialization using string values.

    So the value 10123M would be stored as the string bigdec:10123 in MongoDB.

    So just create the clojure file myproject.data.big-decimal and require it once somewhere in your project:

    (ns myproject.data.big-decimal
      (:require  [monger.conversion :refer :all]
                 [clojure.string :as str]))
    
    (def prefix "bigdec:")
    (def prefix-count (count prefix))
    
    (defn big-dec-serialize [value]
      (str prefix value))
    
    (defn big-dec-deserialize [s]
      (if (and (> (count s) prefix-count)
               (= (subs s 0 prefix-count) prefix))
          (try 
            (bigdec (subs s prefix-count (count s)))
            (catch Exception e 
              s))
          s))
    
    (extend-protocol ConvertToDBObject
      java.math.BigDecimal 
      (to-db-object [^java.math.BigDecimal nr]
        (big-dec-serialize nr)))
    
    (extend-protocol ConvertFromDBObject
      String
      (from-db-object [^String input keywordize]
        (big-dec-deserialize input)))
    

    As long as you keep these serializations within your app, things will work out ok. If you need other apps to access your db, then they would have to have the same de/serialization functions, so that could become trickier.

    Also there is an added overhead for reading every string from your db, it could affect your performance if you have very high db usage, but I think it's negligible in normal cases.