How to exclude nested empty objects when saving? #2402
-
I am migrating from 1.6.1 to 2.2.10 (still using legacy options) and there seems to be discrepancy when saving a document. Let's say I have the following.
In morphia 2.2.10 the saved document will have:
While in 1.6.1, the "person" field won't be saved. I found that there is MapperOption - storeEmpty but by default it's false and related only for lists/maps/sets/arrays. Is there a way where the empty nested objects are not saved? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I'm guessing that sample doc at the bottom was meant to be If you would like to recreate that behavior, you can use an |
Beta Was this translation helpful? Give feedback.
I'm guessing that sample doc at the bottom was meant to be
address
? At any rate, Morphia can't distinguish such a case. How Morphia, by way of how the driver works, persists an entity is that it writes each key/value pair in toBsonWriter
. This is designed, at the driver level, to be an efficient "straight to the wire" mechanism (to simplify a little). It's designed to eliminate double writes (once to memory, then to the network) as much as possible. In order to do what you want, we'd have to serialize out that property, check the doc for emptiness, and then write it out. Fundamentally, that's much less efficient and is part of why Morphia moved away from that in 2.0.If you would like to…