본문 바로가기
IT/Spring

Spring Mongo Database (Multi Database) #2

by 민쌍 2021. 2. 28.

MongoRepository

  • 기본적인 사용법은 JPA와 비슷.
  • 대소문자 구분

Document

  • @Field - MongoDB column명
  • json 데이터가 가끔 String으로 올때가 있는데, TypeException 발생 가능 
@Document(collection = "krxStockPrices")
@Data
public class **KrxStockPrice** {
    @Field("_id")
    private ObjectId id;
    @Field("createdAt")
    public LocalDateTime createdAt;
    @Field("creationDate")
    public String creationDate;
    @Field("openPrice")
    public String openPrice;
    @Field("highPrice")
    public String highPrice;
    @Field("lowPrice")
    public String lowPrice;
    @Field("closePrice")
    public String closePrice;
    @Field("market")
    public String market;
    @Field("marketCap")
    public String marketCap;
    @Field("marketCode")
    public String marketCode;
    @Field("numberOfShares")
    public String numberOfShares;
    @Field("stockCode")
    public String stockCode;
    @Field("stockName")
    public String stockName;
    @Field("tradingValue")
    public String tradingValue;
    @Field("tradingVolume")
    public String tradingVolume;

    public BigDecimal getOpenPrice() {
        return new BigDecimal(openPrice.replace(",", ""));
    }

    public BigDecimal getHighPrice() {
        return new BigDecimal(highPrice.replace(",", ""));
    }

    public BigDecimal getLowPrice() {
        return new BigDecimal(lowPrice.replace(",", ""));
    }

    public BigDecimal getClosePrice() {
        return new BigDecimal(closePrice.replace(",", ""));
    }

    public BigDecimal getMarketCap() {
        return new BigDecimal(marketCap.replace(",", ""));
    }

    public BigDecimal getNumberOfShares() {
        return new BigDecimal(numberOfShares.replace(",", ""));
    }

    public BigDecimal getTradingValue() {
        return new BigDecimal(tradingValue.replace(",", ""));
    }

    public BigDecimal getTradingVolume() {
        return new BigDecimal(tradingVolume.replace(",", ""));
    }

}

@Document
@Data
@NoArgsConstructor
public class **KrxStockCodes** {
    @Field("_id")
    @Expose
    public ObjectId id;
    @Field("createdAt")
    @Expose
    public LocalDateTime createdAt;
    @Field("creationDate")
    @Expose
    public String creationDate;

    @Field("market")
    @Expose
    public String market;
    @Field("marketCode")
    @Expose
    public String marketCode;

    @Field("contents")
    @Expose
    public List<KrxStockCode> krxStockCodes = null;

    public class KrxStockCode {

        @Field("standardCode")
        @Expose
        public String standardCode;
        @Field("stockCode")
        @Expose
        public String stockCode;
        @Field("stockName")
        @Expose
        public String stockName;

    }
}

Repository


@Repository
public interface KrxStockPriceRepo extends MongoRepository<**KrxStockPrice**, ObjectId> {

    List<KrxStockPrice>  findByStockCode(String stockCode);
    List<KrxStockPrice>  findByStockCodeAndCreationDate(String stockCode, String creationDate);
    List<KrxStockPrice>  findByCreationDate(String creationDate);
}

@Repository
public interface KrxStockCodesRepo extends MongoRepository<**KrxStockCodes**, ObjectId> {
    List<KrxStockCodes> findByCreationDate(String creationDate);
//db.krxStockCodes.find({
//        'creationDate': '20210221'
//    })

        @Query(value = "{ 'contents.stockCode' : ?0 }", fields = "{ 'contents.standardCode' : 1 }")
    List<KrxStockCodes> findByNested(String stockCode);  
/* 
db.krxStockCodes.find({ 
    'contents.stockCode': '044990'
})
*/
}

Using

//Embedded or Nested Document 접근
List<KrxStockCode>  krxStockCodes= krxStockCodesRepo.findAll().get(0).contents;

List<KrxStockPrice> krxStockPrices = krxStockPriceRepo.findByStockCodeAndCreationDate("900080", "20210201");
//    db.krxStockPrices.find({
//    'stockCode' : '900080',
//    'creationDate' : '20210201'
//    })
728x90
반응형

'IT > Spring' 카테고리의 다른 글

Spring, nginx proxy HATEOAS  (0) 2021.04.07
QueryDsl Version Error  (0) 2021.03.17
Spring Mongo Database (Multi Database) #2  (0) 2021.02.24
Spring Mongo Database (Multi Database)  (0) 2021.02.07
Spring boot Oracle DB Connection issues  (0) 2020.12.15