728x90
반응형
MongoDB
문서 기반(Document-Oriented
) 저장소를 제공하는 NoSQL Database System
특징
Document-oriented storage
: MongoDB는Database > Collections > Documents
구조로 Document는Key-Value
형태의BSON(Binary JSON)
으로 구성Schemaless
: 스키마가 존재하지 않아 필드 추가/삭제가 용이Full Index Support
: 다양한 인덱싱을 제공Single Field Indexes
: 기본적인 인덱스 타입Compound Indexes
: RDBMS의 복합인덱스Multikey Indexes
:Array
에 매칭되는 값이 하나라도 있으면 인덱스에 추가하는 멀티키 인덱스Geospatial Indexes and Queries
: 위치기반 인덱스와 쿼리Text Indexes
:String
에도 인덱싱이 가능Hashed Index
:Btree 인덱스
가 아닌Hash 타입
의 인덱스도 사용 가능
Replication & High Availability
: 간단한 설정만으로도 데이터 복제를 지원하여 가용성 향상Auto-Sharding
: 자동으로 데이터를 분산하여 저장하며, 하나의 컬렉션처럼 사용 가능Querying
: 다양한 종류의 쿼리문 지원 (필터링, 수집, 정렬, 정규표현식 등)Fast In-Pace Updates
: 고성능의Atomic Operation
을 지원MapReduce
:Map
과Reduce
함수의 조합을 통해 분산/병렬 시스템 운용 지원GridFS
: 분산파일 저장을 자동으로 수행. 실제 파일이 어디에 저장되어 있는지 신경 쓸 필요 없고 복구도 자동 수행
예제
1. 의존성 추가
// pom.xml
// Docker 이용할 MongoDB
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
// 테스트용 내장 MongoDB
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
2. Account 객체 및 저장소 생성
// Account.java
@Document(collection = "accounts")
public class Account {
@Id
private String id;
private String username;
private String email;
getter/setter
}
// AccountRepository.java
public interface AccountRepository extends MongoRepository<Account, String> {
Optional<Account> findByEmail(String email);
}
3. Database 설정
(1) Docker 사용
// Terminal
// 저장소 생성
// 기본 포트 : 27017
$ docker run -p 27017:27017 --name mongo_boot -d mongo
// 저장소 실행
// bash 명령어는 해당 DB 접속하면서 bash 실행
$ docker exec -i -t mongo_boot bash
// Application.java
@SpringBootApplication
public class Application {
@Autowired
AccountRepository accountRepository;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public ApplicationRunner applicationRunner() {
return args -> {
Account account = new Account();
account.setEmail("sun7190@naver.com");
account.setUsername("Jinseo");
accountRepository.insert(account);
System.out.println("finish");
};
}
}
(2) 내장 MongoDB 사용
// AccountRepositoryTest.java
@RunWith(SpringRunner.class)
@DataMongoTest
public class AccountRepositoryTest {
@Autowired
AccountRepository accountRepository;
@Test
public void findByEmail() {
Account account = new Account();
account.setUsername("jsk");
account.setEmail("test@test.com");
accountRepository.save(account);
Optional<Account> byId = accountRepository.findById(account.getId());
System.out.println("UserName : " + byId.get().getUsername());
System.out.println("Email : " + byId.get().getEmail());
assertThat(byId).isNotEmpty();
Optional<Account> byEmail = accountRepository.findByEmail(account.getEmail());
assertThat(byEmail).isNotEmpty();
assertThat(byEmail.get().getUsername()).isEqualTo("jsk");
}
}
결과 확인
1. Docker 사용 결과
2. 내장 MongoDB 사용 결과
참고
https://docs.mongodb.com/manual/core/document/
Documents — MongoDB Manual
Documents MongoDB stores data records as BSON documents. BSON is a binary representation of JSON documents, though it contains more data types than JSON. For the BSON spec, see bsonspec.org. See also BSON Types. Document Structure MongoDB documents are com
docs.mongodb.com
728x90
반응형
'Dev > Spring Boot' 카테고리의 다른 글
[스프링 부트 개념과 활용] 스프링 데이터 12부 (0) | 2020.08.31 |
---|---|
[스프링 부트 개념과 활용] 스프링 데이터 11부 (0) | 2020.08.28 |
[스프링 부트 개념과 활용] 스프링 데이터 9부 (0) | 2020.08.28 |
[스프링 부트 개념과 활용] 스프링 데이터 8부 (0) | 2020.08.27 |
[스프링 부트 개념과 활용] 스프링 데이터 7부 (0) | 2020.08.27 |
댓글