Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store null fields with Morphia

Morphia does not store null/empty fields by default. Is there a way to enable this?

like image 351
user2066880 Avatar asked Mar 17 '23 08:03

user2066880


2 Answers

In the interests of answering the question asked, you can:

  • store nulls by configuring MapperOptions.setStoreNulls(boolean)
  • store empty values by configuring MapperOptions.setStoreEmpties(boolean).

You can get a MapperOptions reference from the mapper instance by Mapper.getOptions().

Deprecation Update

However, the Mapper class is internal only and is deprecated:

this class will be internalized in 2.0

Same deprecation applies for before mentioned setter methods in class MapperOptions, instead the JavaDocs recommend:

use the Builder instead

like image 88
evanchooly Avatar answered Mar 27 '23 17:03

evanchooly


This is a feature and one of the points of a document store. Don't waste space (both on RAM, disk, and for transfers) on missing data.

If you use primitive types instead of object types, a value will be saved, since there's no null value. For example long instead of Long. For String you'd need to store an empty (or some dummy) value.

Out of interest: Why would you want to explicitly store null / empty fields? Maybe there's a better solution for what you are trying to do.

like image 35
xeraa Avatar answered Mar 27 '23 18:03

xeraa