본문 바로가기
IT/Spring

Spring Mongo Database (Multi Database) #2

by 민쌍 2021. 2. 24.

MongoRepository

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

Document

@Document(collection = "krxStockPrices")
@Data
public class **KrxStockPrice** {
    @SerializedName("_id")
    private ObjectId id;
    @SerializedName("closePrice")
    public String closePrice;
    @SerializedName("createdAt")
    public LocalDateTime createdAt;
    @SerializedName("creationDate")
    public String creationDate;
    @SerializedName("highPrice")
    public String highPrice;
    @SerializedName("lowPrice")
    public String lowPrice;
    @SerializedName("market")
    public String market;
    @SerializedName("marketCap")
    public String marketCap;
    @SerializedName("marketCode")
    public String marketCode;
    @SerializedName("numberOfShares")
    public String numberOfShares;
    @SerializedName("openPrice")
    public String openPrice;
    @SerializedName("stockCode")
    public String stockCode;
    @SerializedName("stockName")
    public String stockName;
    @SerializedName("tradingValue")
    public String tradingValue;
    @SerializedName("tradingVolume")
    public String tradingVolume;

}

@Document
@Data
@NoArgsConstructor
public class **KrxStockCodes** {
    @SerializedName("_id")
    @Expose
    public ObjectId id;
    @SerializedName("contents")
    @Expose
    public List<KrxStockCode> contents = null;

    @SerializedName("createdAt")
    @Expose
    public LocalDateTime createdAt;
    @SerializedName("creationDate")
    @Expose
    public String creationDate;
    @SerializedName("market")
    @Expose
    public String market;
    @SerializedName("marketCode")
    @Expose
    public String marketCode;

    public class KrxStockCode {

        @SerializedName("standardCode")
        @Expose
        public String standardCode;
        @SerializedName("stockCode")
        @Expose
        public String stockCode;
        @SerializedName("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' 카테고리의 다른 글

QueryDsl Version Error  (0) 2021.03.17
Spring Mongo Database (Multi Database) #2  (0) 2021.02.28
Spring Mongo Database (Multi Database)  (0) 2021.02.07
Spring boot Oracle DB Connection issues  (0) 2020.12.15
배포 순서 (Spring, Java Project)  (0) 2020.10.24