์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[MongoDB] Model Relationships Between Documents (2)

๐Ÿ˜Ž ์ง€์‹ in Action/React, Typescript, Javascript ๐ŸŒ

by :ํ•ดํ”ผ๋ž˜๋น—๐Ÿพ 2021. 6. 10. 13:38

๋ณธ๋ฌธ

728x90

Model One-to-Many Relationships with Embedded Documents

Embedded Document Pattern

๋”๋ณด๊ธฐ
// 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"
                }
              ]
 }

Subset Pattern

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๊ฐœ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹ ๋•Œ๋งŒ ์ €์žฅ๋˜๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•ด์•ผ ํ•œ๋‹ค.

 

๋‹ค๋ฅธ ์˜ˆ์‹œ:

  • Comments on a blog post, when you only want to show the most recent or highest-rated comments by default.
  • Cast members in a movie, when you only want to show cast members with the largest roles by default.

 

 

728x90

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ