Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!