상세 컨텐츠

본문 제목

[MongoDB] Model Relationships Between Documents (2)

😎 지식/FE-리액트🌐

by :Eundms 2021. 6. 10. 13:38

본문

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.

 

 

관련글 더보기

댓글 영역