【问题标题】:how to set default value for mongodb by using springboot?如何使用spring boot为mongodb设置默认值?
【发布时间】:2019-02-15 16:41:50
【问题描述】:

我想在mongodb中插入一个像下面这样的Bean,我使用springboot 2.0.4.RELEASE,我希望a字段有一个默认值,比如100.00,怎么做?

 public class Bean{
    String uid;
    double a;
}

【问题讨论】:

    标签: mongodb spring-boot


    【解决方案1】:

    你必须注释你的 Bean,最好在你的集合中有 id

    import org.springframework.data.annotation.Id;
    import org.springframework.data.mongodb.core.mapping.Document;
    
    @Document(collection = "your_collection_name")
    public class Bean {
    
        @Id
        private String id;
    
        private String uid;
        private double a = 100.00;
    
        //getter and setter methods
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-31
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-08
      • 1970-01-01
      相关资源
      最近更新 更多