Firestore exclude data serialization

后端 未结 3 1920
我在风中等你
我在风中等你 2021-01-01 16:30

I\'ve got several properties that should not be transferred to Firestore, such as metadata (\"id\" or \"parent\"), and with Firebase Realtime database, there was the option

相关标签:
3条回答
  • 2021-01-01 16:54

    To mark a field as excluded from the Firestore or Realtime database you can use the @Exclude annotation. For example:

    @IgnoreExtraProperties
    public class Model {
        @Exclude private String id;
    
        // ...
    }
    

    You can also use the @IgnoreExtraProperties annotation against the class to automatically ignore properties that don't map to class fields.

    0 讨论(0)
  • 2021-01-01 17:01

    Grimthorr's answer didn't work for me. So, I put @Exclude annotation before the getter method of the field I want to exclude from writing to Firestore db, and it worked:

    @Exclude
    public String getId() {
        return id;
    }
    
    0 讨论(0)
  • 2021-01-01 17:04

    For Kotlin you need to make sure that the getter is annotated with the @Exclude annotation and not just the appropriate field. You can do this by specifying the target of the annotation. For example:

    data class Model(@get:Exclude val id: String)
    

    @get specifies that the @Exclude annotation should be added to the value's getter. See Annotation Use-site Targets docs for more information.

    This also works for data classes.

    Credits to @mfulton26 for his answer https://stackoverflow.com/a/40117301/2739794.

    0 讨论(0)
提交回复
热议问题