1

I already went through the link: https://jira.spring.io/browse/DATAMONGO-1107 and also I dont see option to set spring.data.mongodb.persist-null-fields=true in application.properties file.

Spring Data Mongo doesn't even persist a field having value null. How to allow to save null value if I want to do so ?

Person.java

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document
public class Person {
    private String id;
    private String firstName;
    private String lastName;
    private String emailId;
    @Field
    private List<Hobbies> hobbies;
}

Hobbies.java

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hobbies {
    private String interest;
    private String sports;
}

save() - This method doesn't persist data.

private void savePersons() {
        Hobbies hobbies1 = Hobbies.builder().interest("Indoor").sports("Chess").build();
        Hobbies hobbies2 = Hobbies.builder().interest("Loveoor").sports("Table Tennis").build();
        Hobbies hobbies3 = Hobbies.builder().interest("Gamedoor").sports("Cricket").build();
        Hobbies hobbies4 = Hobbies.builder().interest("Happydoor").sports("Lawn Tennis").build();


        Person john = Person.builder().firstName("John")
                .emailId("john.kerr@gmail.com").hobbies(Arrays.asList(hobbies1, hobbies2)).build();

        Person neha = Person.builder().firstName("Neha").lastName("Parate")
                .emailId("john.kerr@gmail.com").hobbies(Arrays.asList(hobbies1, hobbies2, hobbies4)).build();

        repository.saveAll(Arrays.asList(john, neha));
    }

I dont see lastName has been saved in the document. Also, how to save Date as null value?

PAA
  • 1
  • 46
  • 174
  • 282
  • Possible duplicate of [Spring Data mongo to insert null values to DB](https://stackoverflow.com/questions/41112607/spring-data-mongo-to-insert-null-values-to-db) – Triet Doan Apr 09 '19 at 17:06
  • @Triet Doan - I had checked that solution already. Is there any way if we can do it using Spring Data Mongo instead of BSONObject –  Apr 09 '19 at 17:22
  • I'm not sure. Can you try annotating your `Person` class with `@JsonInclude(JsonInclude.Include.ALWAYS)`? The annotation comes from Jackson library. – Triet Doan Apr 10 '19 at 12:58
  • @Triet Doan - Still not working. I wonder why dont see provision to save null into MongoDB ? –  Apr 10 '19 at 17:02
  • Could you please share the reason why you need to store `null` in your MongoDB? Normally, I think it doesn't make sense to do so. But maybe I'm wrong. – Triet Doan Apr 11 '19 at 10:09
  • @Triet Doan - We have anlytics team which analyses data within shared and integrated syste, When they look at mongo documents or mongo reports, they dont see either those fields and its creating lot of confusions for them. Any workaround for the same ? –  Apr 12 '19 at 17:52
  • In your case, I think it's better to share with them the database schema. With it, they will know what to expect. If some fields are missing in the reports, clearly state that missing fields means `null`. IMHO, this problem should be solved by communication instead of re-inventing the technical wheels. – Triet Doan Apr 13 '19 at 13:13

2 Answers2

0

One sollution is maybe to use defaults.

Your schemas can define default values for certain paths. If you create a new document without that path set, the default will kick in.

So if you want to "allow null", just make it the default value by adding to your schema definition lastName: { type: String, default: null }. When there is no lastName (aka null) you get the default value (null).
I would rethink allowing null because it would make querying complicated: Query on key: null will retrieve you all document where key is null or where key doesn't exist.

Moad Ennagi
  • 1,058
  • 1
  • 11
  • 19
  • Thank You ! Could you post the answer from the perspective of Spring Data Mongo ? What changes do I need to make ? –  Apr 09 '19 at 16:35
  • I have added an example for Spring Data Mongo. I'm not very familiar with the framework, could this `private String field = null;` do it ? – Moad Ennagi Apr 09 '19 at 16:40
  • I did like `@Field private String lastName = null;`, but still its not working –  Apr 09 '19 at 16:46
  • `@Field private String lastName = '';` what do you get with an empty string ? – Moad Ennagi Apr 09 '19 at 16:47
  • Even If I set `@Field private String lastName = "David";` I dont get default value. In that caseI am saving document like this: `Person john = Person.builder().firstName("John") .emailId("john.kerr@gmail.com").hobbies(Arrays.asList(hobbies1, hobbies2)).build();` –  Apr 09 '19 at 16:50
  • https://stackoverflow.com/questions/41344173/is-there-any-spring-annotation-to-set-default-value-for-a-field-mongo/41344414 – Moad Ennagi Apr 09 '19 at 16:57
  • I already checked that, this doesn't works. I've also mentioned the same in my comments –  Apr 09 '19 at 17:09
  • null is treated differently in expressions and there are performance issues for lookup-with-pipeline: https://jira.mongodb.org/browse/SERVER-40362 – herman Feb 28 '22 at 09:30
0

This is now possible starting with Spring Data MongoDB 3.3.. For example:

@Field(write = Field.Write.ALWAYS)
private String lastName;
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
mcont
  • 1,749
  • 1
  • 22
  • 33