Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

265
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda