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

151
Visualizações
MongoDB Scala Driver - Rendering BSON Documents

We currently have a Type-Safe query language at work, this custom DSL allows us to easily write database queries that are interpreted and converted into Mongo Queries.

We recently swapped over from Casbah to the new Mongo Scala Driver and rewrote our interpreter. I am however having some issues when dealing with optional values.

This is an example query:

dao.headOption(Order.id === orderId.some)

The type stored on the Order Object is an Option so we lift the provided id into an option as well. However whenever I try to render out the generated query for debugging as well as for test cases in the following manner:

import org.mongodb.scala.bson.{BsonDocument, codecs}

query.toBsonDocument(BsonDocument.getClass, codecs.DEFAULT_CODEC_REGISTRY)

The following exception ends up being thrown:

Can't find a codec for class scala.Some.
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class scala.Some.

I am not sure how to rectify this, or if I need to implement my own codec for Options, and if I do I have no idea how I would do that.

Any help would be gladly appreciated. Thank you in advance

Update

I have seen that I can try and implement the Codec Interface as shown here:

http://mongodb.github.io/mongo-java-driver/3.0/bson/codecs/

Would I need to implement it for each possible sub-type of Option?

Example Option[Int], Option[UUID], Option[List[String]] etc.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

you could use something like this to fix the issue

class SomeCodec extends Codec[Some[_]] {
  override def encode(writer: BsonWriter, value: Some[_], encoderContext: EncoderContext): Unit = value match {
    case Some(v: String) ⇒ writer.writeString(v)
    case Some(v: Int) ⇒ writer.writeInt32(v)
    case Some(v: Long) ⇒ writer.writeInt64(v)
    case Some(v: Boolean) ⇒ writer.writeBoolean(v)
  }

  override def getEncoderClass: Class[Some[_]] = classOf[Some[_]]

  override def decode(reader: BsonReader, decoderContext: DecoderContext): Some[_] = {
    reader.getCurrentBsonType match {
      case BsonType.BOOLEAN ⇒ Some(reader.readBoolean())
      case BsonType.STRING ⇒ Some(reader.readString())
      case BsonType.INT64 ⇒ Some(reader.readInt64())
      case BsonType.INT32 ⇒ Some(reader.readInt32())
    }
  }
}
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