The scala-mongo-driver has a decent documentation for working with case classes. http://mongodb.github.io/mongo-scala-driver/2.3/getting-started/quick-tour-case-classes/.
In current situation my case class fields are Option[T] values. So the values can be either None
or Some(T)
.The default codec is now serializing 'None' values as null. But I want to exclude the key if its value is None. How can I obtain this behavior? From my research I think we need to write a custom Codec or Codec Provider for the case class instead of the default case class codec.
Asked
Active
Viewed 2,045 times
5

Ambareesh B
- 499
- 1
- 5
- 14
-
I think you should leave `None` values as `null` in Mongo documents. Otherwise you will have to take care of it in custom codecs for your case classes. – sarveshseri May 23 '18 at 08:15
-
Ya I was thinking about the same. – Ambareesh B May 24 '18 at 04:39
1 Answers
2
There is an new macro helper since v2.1.0: Macros.createCodecProviderIgnoreNone
where you can define that None
values will be ignored from saving to the DB.

Ghashange
- 1,027
- 1
- 10
- 20
-
1Thanks. I have found this one in the docs but forgot to update here.:) – Ambareesh B Jul 23 '18 at 17:05