Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to @Field annotation to control property write rules [DATAMONGO-2551] #3407

Closed
spring-projects-issues opened this issue May 17, 2020 · 1 comment
Assignees
Labels
in: mapping Mapping and conversion infrastructure status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

isopov opened DATAMONGO-2551 and commented

In mongo it is possible to completely omit writing a field in collection if it is not needed in this document. This has a benefit of using sparse indexes on such field (or their modern replacement indexes with partial filters on exists=false). Also it reduces storage size. It seems that it would be beneficial to have an boolean option for Field annotation named omitNull (or any other name) that will allow to skip writing this field if it has null value


No further details from DATAMONGO-2551

@christophstrobl
Copy link
Member

The mapping layer by default does not write null values to the resulting org.bson.Document (see: MappingMongoConverter.java#L615).
In some scenarios is might make sense to allow persist null values, so that an $exits operator would still match, though the actual value is null.

On field level something like the following might make sense.

public @interface Field {

	// ...

	/**
	 * If set to {@literal true} {@literal null} values will be omitted.
	 * Setting the value to {@literal false} explicitly adds an entry for the given field
	 * holding {@literal null} as a value {@code 'fieldName' : null }.
	 * <p />
	 * <strong>NOTE</strong> Setting the value to {@literal false} may lead to increased document size.
	 * @return {@literal true} by default.
	 */
	boolean omitNull() default true;
}

Same could be applied to the @Document annotation or set via the Conversions API.

Happy to gather/discuss requirements & thoughts here to get a clear picture of the enhancement.

@christophstrobl christophstrobl added in: mapping Mapping and conversion infrastructure status: pending-design-work Needs design work before any code can be developed and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 16, 2021
@mp911de mp911de linked a pull request Jun 14, 2021 that will close this issue
4 tasks
@mp911de mp911de changed the title Add an option to @Field annoatation to omit null values on write [DATAMONGO-2551] Add an option to @Field annotation to control property write rules [DATAMONGO-2551] Jun 14, 2021
@mp911de mp911de assigned mp911de and unassigned christophstrobl Jun 14, 2021
@mp911de mp911de added this to the 3.3 M1 (2021.1.0) milestone Jun 14, 2021
mp911de added a commit that referenced this issue Jun 14, 2021
Reorder methods and types. Rename MongoPersistentProperty.isOmitNullProperty to writeNullValues. Adapt caching MongoPersistentProperty and add tests.

Tweak Javadoc wording, add author and since tags.

See #3407
Original pull request: #3646.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: mapping Mapping and conversion infrastructure status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants