Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

264
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda