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

164
Vistas
Not persisting Scala None's instead of persisting as null value

I noticed that the scala driver (version 1.2.1) writes Option values of None as null to the corresponding field. I would prefer omitting the fieid completely in this case. Is this possible?

Example

case class Test(foo: Option[String])
persist(Test(None))

leads to

> db.test.find()
{ "_id": "...", "foo": null }

but I want to achieve

> db.test.find()
{ "_id": "..." }

When I used casbah, I think my intended behaviour was the default.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

http://mongodb.github.io/mongo-scala-driver/2.4/bson/macros/

Now you can use macros for it:

val testCodec = Macros.createCodecProviderIgnoreNone[Test]()

and in codec conf:

lazy val codecRegistry: CodecRegistry = fromRegistries(fromProviders(testCodec))
over 4 years ago · Santiago Trujillo Denunciar

0

Opened a feature request in the mongodb bug tracker (https://jira.mongodb.org/browse/SCALA-294), which was answered by Ross Lawley. He suggests to change the conversion code (from case class to document) from

def toDocument(t: Test) = Document("foo" -> t.foo)

to something like

def toDocument(t: Test) = {
  val d = Document()
  t.foo.foreach{ value =>
    d + ("foo" -> value)
  }
  d
}
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