TIL

NoSQL 읽고 쓰기

Document DB

MonogoDB 개요

MongoCursorItemReader

MongoCursorItemReader 해부

MongoCursorItemReader 
  │ 
  ├────── MongoTemplate 
  │         └─ (MongoDB 작업의 핵심 엔진)
  │ 
  ├────── Query (org.springframework.data.mongodb.core.query.Query) 
  │         └─ (MongoDB 쿼리 조건 정의) 
  │
  └────── Cursor 
             └─ (MongoDB 데이터를 순차적으로 읽어오는 스트리밍 객체)
@Bean
@StepScope
public MongoCursorItemReader<SecurityLog> securityLogReader(
        @Value("#{jobParameters['searchDate']}") LocalDate searchDate
) {
    Date startOfDay = Date.from(searchDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    Date endOfDay = Date.from(searchDate.plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());

    return new MongoCursorItemReaderBuilder<SecurityLog>()
            .name("securityLogReader")
            .template(mongoTemplate)
            .collection("security_logs")
            .jsonQuery("""
            {
                "label": "PENDING_ANALYSIS",
                "timestamp": {
                    "$gte": ?0,
                    "$lt": ?1
                }
            }
            """)
            .parameterValues(List.of(startOfDay, endOfDay))
            .sorts(Map.of("timestamp", Sort.Direction.ASC))
            .targetType(SecurityLog.class)
            .batchSize(10)
            .build();
}