[MongoDB] Model Relationships Between Documents (2)
// patron document
{
_id: "joe",
name: "Joe Bookreader"
}
// address documents
{
patron_id: "joe", // reference to patron document
street: "123 Fake Street",
city: "Faketon",
state: "MA",
zip: "12345"
}
{
patron_id: "joe",
street: "1 Some Other Street",
city: "Boston",
state: "MA",
zip: "12345"
}
๋ง์ฝ, address data์ ์ด๋ฆ์ ๊ฐ์ด ์์ฃผ ์ฌ์ฉ๋ฐ๋๋ค๋ฉด, ์์ฃผ ์ฟผ๋ฆฌ๋ฅผ ์ฌ์ฉํด์ผ ํ๊ธฐ ๋๋ฌธ์,
์์ ํฌํจํ๋ ๊ฒ ๋ซ๋ค.
{
"_id": "joe",
"name": "Joe Bookreader",
"addresses": [
{
"street": "123 Fake Street",
"city": "Faketon",
"state": "MA",
"zip": "12345"
},
{
"street": "1 Some Other Street",
"city": "Boston",
"state": "MA",
"zip": "12345"
}
]
}
Embedded document pattern์์ ํฌํจ๋ ํ๋๊ฐ unboundedํ๋ฉด document๊ฐ ๋๋ฌด ์ปค์ง ์ํ์ด ์๋ค.
์ด๋ด ๊ฒฝ์ฐ์๋ subset pattern์ ์ฌ์ฉํ ์ ์๋ค.
// product collection ์๋ 10๊ฐ์ ์ต๊ทผ ๋ฆฌ๋ทฐ๋ง ๊ฐ์ง๊ณ ์๊ณ
{
"_id": 1,
"name": "Super Widget",
"description": "This is the most useful item in your toolbox.",
"price": { "value": NumberDecimal("119.99"), "currency": "USD" },
"reviews": [
{
"review_id": 786,
"review_author": "Kristina",
"review_text": "This is indeed an amazing widget.",
"published_date": ISODate("2019-02-18")
}
...
{
"review_id": 776,
"review_author": "Pablo",
"review_text": "Amazing!",
"published_date": ISODate("2019-02-16")
}
]
}
// review collection๋ ๋ชจ๋ ๋ฆฌ๋ทฐ๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
// ๋ง์ฝ, ์ฌ์ฉ์๊ฐ ๋ ๋ง์ ๋ฆฌ๋ทฐ๋ฅผ ๋ณด๊ณ ์ถ๋ค๋ฉด review collection์ ์์ฒญํ์ฌ ๋ณผ ์ ์๋ค.
{
"review_id": 786,
"product_id": 1,
"review_author": "Kristina",
"review_text": "This is indeed an amazing widget.",
"published_date": ISODate("2019-02-18")
}
{
"review_id": 785,
"product_id": 1,
"review_author": "Trina",
"review_text": "Nice product. Slow shipping.",
"published_date": ISODate("2019-02-17")
}
...
{
"review_id": 1,
"product_id": 1,
"review_author": "Hans",
"review_text": "Meh, it's okay.",
"published_date": ISODate("2017-12-06")
}
๊ฐ์ฅ ์์ฃผ ์ก์ธ์คํ๋ ๋ฐ์ดํฐ ๋ถ๋ถ(์ต๊ทผ10๊ฐ)์ ์์ฉ ํ๋ก๊ทธ๋จ์ด ๋จผ์ ๋ก๋ํ๋ ์ปฌ๋ ์ (product)์ ํฌํจ๋์ด์ผํฉ๋๋ค.
์ฅ์ : reduces the overall size of the working set. / read performance๋ฅผ improveํ ์ ์๋ค.
๋จ์ : data ์ค๋ณต์ ์ผ๊ธฐํ ์ ์๋ค.
์) product collection๊ณผ reviews collection์ ๋์ผ ๋ฐ์ดํฐ๊ฐ ๋ค์ด์๋ ๊ฒฝ์ฐ
the reviews are consistent between each collection
๋ง์ฝ, customer๊ฐ review๋ฅผ ์์ ํ๋ฉด, product collection๊ณผ reviews collection์ ์๋ ๋์ผํ ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ ์์ ํด ์ผ๊ด์ฑ์ ์ ์งํด์ผ ํจ.
๊ทธ๋์, ์ด๋ฌํ ์ผ์ด ์ผ์ด๋์ง ์๋๋ก, ํญ์ product collection์๋ ์ต๊ทผ 10๊ฐ์ ๋ฐ์ดํฐ๊ฐ ์๊ณ , reviews collection์๋ ์ต๊ทผ 10๊ฐ์ ๋ฐ์ดํฐ๊ฐ ์๋ ๋๋ง ์ ์ฅ๋๋ ๊ฒ์ ๋ณด์ฅํด์ผ ํ๋ค.
๋ค๋ฅธ ์์:
| [React] Virtual-DOM, Reconciliation, fiber (0) | 2021.06.21 |
|---|---|
| [MongoDB] Model Relationships Between Documents (3) (0) | 2021.06.10 |
| [MongoDB] Model Relationships Between Documents (1) (0) | 2021.06.10 |
| [React] Fetch data in React (2)๐ฆ (0) | 2021.05.27 |
| [React] Fetch data in React (1) (0) | 2021.05.27 |