-
Notifications
You must be signed in to change notification settings - Fork 6
몽고디비 데이터 모델링
박재윤 edited this page Nov 20, 2020
·
2 revisions
테이블의 구조를 결정하고 선언해야하는 RDB와 다르게 몽고디비 컬렉션에는 동일한 스키마가 필요없다.
- 단일 집합의 문서는 동일한 집합을 가질 필요가 없다.
- 하지만 실제로 컬렉션에 있는 문서들은 비슷한 문서들을 공유한다.
key-value의 구조로 되어있다.
sub-document가 있을 수 있다.
// Publisher
{
_id: "oreilly",
name: "O'Reilly Media",
founded: 1980,
location: "CA"
}
// Book 1
{
_id: 123456789,
title: "MongoDB: The Definitive Guide",
author: [ "Kristina Chodorow", "Mike Dirolf" ],
published_date: ISODate("2010-09-24"),
pages: 216,
language: "English",
publisher: {
name: "O'Reilly Media",
founded: 1980,
location: "CA"
}
}
// Book 2
{
_id: 234567890,
title: "50 Tips and Tricks for MongoDB Developer",
author: "Kristina Chodorow",
published_date: ISODate("2011-05-06"),
pages: 68,
language: "English",
publisher: {
name: "O'Reilly Media",
founded: 1980,
location: "CA"
}
}
reference를 이용해서 한 문서와 다른 문서의 관계를 연결할 수 있다.
// Publisher
{
_id: "oreilly",
name: "O'Reilly Media",
founded: 1980,
location: "CA"
}
// Book 1
{
_id: 123456789,
title: "MongoDB: The Definitive Guide",
author: [ "Kristina Chodorow", "Mike Dirolf" ],
published_date: ISODate("2010-09-24"),
pages: 216,
language: "English",
publisher_id: "oreilly" // <- Publisher._id
}
// Book 2
{
_id: 234567890,
title: "50 Tips and Tricks for MongoDB Developer",
author: "Kristina Chodorow",
published_date: ISODate("2011-05-06"),
pages: 68,
language: "English",
publisher_id: "oreilly" // <- Publisher._id
}
만약 publisher의 name이나 age를 변경해야 하는 경우,
- -Embeded 방식은 Publisher, Book 문서를 모두 수정해서 데이터의 일관성을 유지해야한다. 만약 데이터가 자주 변경되는 상황이 생기면 일관성을 유지하기 어려월질 수 있다.
- Reference 방식으로 저장된 데이터는 Publisher document만 수정해주면 데이터의 일관성이 유지된다.
만약 많은 양의 book 문서를 publisher 정보를 포함해서 가져오려고 하는 경우,
- Embeded 방식은 그냥 데이터를 가져오면 되고
- Reference 방식은 추가적인 요청을 해서 데이터를 가져와야한다.
결론적으로 데이터 수정이 많은 경우 reference 방식이 유리하고 함께 불러올 데이터가 많을 경우 embeded가 유리하다.