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

๋ณธ๋ฌธ ์ œ๋ชฉ

[MongoDB] Model Relationships Between Documents (3)

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

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

๋ณธ๋ฌธ

Model One-to-Many Relationships with Document References

 

publisher ์ •๋ณด๊ฐ€ ๋ฐ˜๋ณต๋˜๊ณ  ์žˆ๋‹ค.

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

 

 

์ด๋ฅผ ํ•ด๊ฒฐ ํ•˜๊ธฐ ์œ„ํ•ด์„œ, referencing์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ,

๊ฐ publisher๊ฐ€ ๋‚ธ ์ฑ…์˜ ์ˆ˜๊ฐ€ ์ ๊ณ , ํ•œ ์‚ฌ๋žŒ์ด ๋‚ผ ์ˆ˜ ์žˆ๋Š” ์ฑ…์˜ ์ˆ˜์— ํ•œ๊ณ„๊ฐ€ ์žˆ๋‹ค๋ฉด,

publisher document์•ˆ์— book reference๋ฅผ ๋„ฃ๋Š” ๊ฒƒ์ด ํšจ๊ณผ์ ์ด๋‹ค.

๋”๋ณด๊ธฐ
// publisher document
{
   name: "O'Reilly Media",
   founded: 1980,
   location: "CA",
   books: [123456789, 234567890, ...]
}
// book document
{
    _id: 123456789,
    title: "MongoDB: The Definitive Guide",
    author: [ "Kristina Chodorow", "Mike Dirolf" ],
    published_date: ISODate("2010-09-24"),
    pages: 216,
    language: "English"
}

{
   _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๋กœ referenceํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด ํšจ๊ณผ์ ์ด๋‹ค.

๋”๋ณด๊ธฐ
{
   _id: "oreilly",
   name: "O'Reilly Media",
   founded: 1980,
   location: "CA"
}

{
   _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"
}

{
   _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"
}

 

728x90

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