alonsodomin / enumeratum-mongodb

MongoDB Extension for Enumeratum

Version Matrix

Enumeratum MongoDB

Build Status License Maven Central

Extension for Enumeratum and official MongoDB Scala Driver

Getting started

Add the following to your build.sbt:

libraryDependencies += "com.github.alonsodomin" %% "enumeratum-mongodb" % <version>


Define an enumerated value following the guidelines stated in the Enumeratum documentation:

import enumeratum._
import enumeratum.mongodb._

sealed trait ShirtSize extends EnumEntry
object ShirtSize extends Enum[ShirtSize] with MongoDBEnum[ShirtSize] {
  case object Small  extends ShirtSize
  case object Medium extends ShirtSize
  case object Large  extends ShirtSize

  val values = findValues

To make it compatible with MongoDB Driver, we need to mix-in the MongoDBEnum trait into our enum definition as shown above, that will provide with the MongoDB codec generation code required to be abe to use this enum.

To have the codec available when communicating with MongoDB, we need to define a CodecRegistry as follows:

val enumCodecRegistry = CodecRegistry.fromProviders(ShirtSize.bsonCodecProvider)