Skip to content

MongoDB Data Modeling Pattern

juhyojeong edited this page Dec 4, 2022 · 1 revision

I. ์„œ๋ก 

1. ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•˜๊ฒŒ ๋œ ๊ณ„๊ธฐ

๋‚˜๋Š” 8์›” 31์ผ๋ถ€ํ„ฐ ํ˜„์žฌ๊นŒ์ง€ ๋„ค์ด๋ฒ„ ๋ถ€์ŠคํŠธ์บ ํ”„ ์›น/๋ชจ๋ฐ”์ผ 7๊ธฐ ์›นํ’€์Šคํƒ ๋ฉค๋ฒ„์‹ญ ๊ณผ์ •์—์„œ ํ•™์Šต์„ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ๋‹ค. ํ˜„์žฌ๋Š” ๊ทธ๋ฃนํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ๊ณ , ๋‚˜๋Š” ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์„ ๋‹ด๋‹นํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ์—์„œ โ€˜MongoDB๋ฅผ ๋„์ž…ํ•˜์žโ€™๋Š” ์˜๊ฒฌ์ด ๋‚˜์™”๋‹ค. MongoDB๋ฅผ ๋ณด๋‹ค ์ž˜ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” โ€˜๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง ํŒจํ„ดโ€™์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ , ์ด ์ค‘ โ€˜์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ์—์„œ ์ž˜ ๋งž๋Š” ํŒจํ„ด์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒโ€™๋ผ๋Š” ์ƒ๊ฐ์„ ํ•ด์„œ ํŒจํ„ด์„ ์กฐ์‚ฌํ•ด๋ณด๊ธฐ๋กœ ํ•˜์˜€๋‹ค.

2. ํ”„๋กœ์ ํŠธ ์†Œ๊ฐœ

์šฐ๋ฆฌ ๊ทธ๋ฃน์€ โ€˜๋ถ€๋„๋Ÿผโ€™์ด๋ผ๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ, โ€˜๋ถ€๋„๋Ÿผโ€™์€ ์• ์ž์ผ ๊ธฐ๋ฒ• ์ค‘ ํ•œ ์ข…๋ฅ˜์ธ ์Šคํฌ๋Ÿผ์„ ๋„์™€์ฃผ๋Š” ๋„๊ตฌ์ด๋‹ค. ์Šคํฌ๋Ÿผ์„ ์˜จ๋ผ์ธ์—์„œ ์ฒ˜์Œ ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด ๊ฐ ํ™œ๋™์—์„œ ๋ฌด์—‡์„ ํ•ด์•ผํ• ์ง€ ๊ณ ๋ฏผํ•˜๊ฒŒ ๋˜๊ณ , ๊ทธ ๊ณผ์ •์—์„œ ๋‹ต๋‹ตํ•จ๊ณผ ์–ด๋ ค์›€์„ ๋Š๋‚„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ ๊ทธ๋ฃน์€ ์Šคํฌ๋Ÿผ์„ ์ง„ํ–‰ํ•˜๋Š”๋ฐ์— ๋„์›€์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ํ˜‘์—…ํˆด์„ ๋งŒ๋“ค๊ธฐ๋กœ ํ•˜์˜€๋‹ค.

3. ๋ฌธ์„œ์˜ ๋ชฉ์ 

MongoDB๋ฅผ ๋ณด๋‹ค ์ž˜ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ โ€˜๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง ํŒจํ„ดโ€™์„ ์กฐ์‚ฌํ•œ ํ›„ ๊ฐ ํŒจํ„ด์— ๋Œ€ํ•œ ์„ค๋ช…๊ณผ ์žฅ๋‹จ์ ์„ ๊ธฐ์ˆ ํ•˜๊ณ , ์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ์— ๋งž๋Š” โ€˜๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง ํŒจํ„ดโ€™์„ ์ ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค.


II. ๋ณธ๋ก 

0. ์‚ฌ์ „ ์ง€์‹

1. MongoDB ๊ณ„์ธต๋„

image

2. Shading

3. BASE

์ฐธ๊ณ ํ•  ๊ฒƒ https://kciter.so/posts/about-mongodb

4. ์„ฑ๋Šฅ ํ–ฅ์ƒ

์ฐธ๊ณ ํ•  ๊ฒƒ https://velog.io/@moongq/MongoDBMongoose-ํผํฌ๋จผ์Šค-ํ–ฅ์ƒ์‹œํ‚ค๊ธฐ

๋ณด์™„

https://marsettler.com/mongodb/mongodb-study-week-5/

Virtual Data

1. Approximation Pattern

Approximation Pattern์€ **๋ฆฌ์†Œ์Šค ๋น„์šฉ(์‹œ๊ฐ„, ๋ฉ”๋ชจ๋ฆฌ, CPU ์ฃผ๊ธฐ)**์ด ๋งŽ์ด ๋“œ๋Š” ๊ณ„์‚ฐ์ด ์ž์ฃผ ์ˆ˜ํ–‰๋˜๊ณ , ์ด๋Ÿฌํ•œ ๊ณ„์‚ฐ์˜ ์ •๋ฐ€๋„๊ฐ€ ์ตœ์šฐ์„  ์ˆœ์œ„๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์— ์œ ์šฉํ•˜๋‹ค.

์žฅ์ 

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€ํ•œ ์“ฐ๊ธฐ๊ฐ€ ์ ์Œ
  • ํ†ต๊ณ„์ ์œผ๋กœ ์œ ํšจํ•œ ์ˆซ์ž๋ฅผ ์œ ์ง€ํ•จ

๋‹จ์ 

  • ์ •ํ™•ํ•œ ์ˆซ์ž๊ฐ€ ํ‘œ์‹œ๋˜์ง€ ์•Š์Œ
  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ

์˜ˆ์ œ

  • ์ผ๋ฐ˜์ ์œผ๋กœ 700,000๋ช…์ด ์‚ฌ์ดํŠธ๋ฅผ ๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€ ๋˜๋Š” 699,983๋ช…์ด ๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€ ์•„๋Š” ๊ฒƒ์€ ์ค‘์š”ํ•˜์ง€ ์•Š์Œ โ‡’ ๋”ฐ๋ผ์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์นด์šดํ„ฐ๋ฅผ ๊ตฌ์ถ•ํ•˜๊ณ  ์ž„๊ณ„๊ฐ’์ด ์ถฉ์กฑ๋˜๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์—…๋ฐ์ดํŠธ
  • ์•„๋ž˜ ์ด๋ฏธ์ง€์™€ ๊ฐ™์ด Approximation Pattern์„ ์‚ฌ์šฉํ•˜์—ฌ cout ์ž‘์—…์— ๋Œ€ํ•œ ์“ฐ๊ธฐ๋ฅผ ์ค„์ž„์œผ๋กœ์จ, ์•„ํ‚คํ…์ฒ˜ ๋ณต์žก์„ฑ๊ณผ ๋น„์šฉ์„ ์ค„์ผ ์ˆ˜ ์žˆ์Œ

image

2. Attribute Pattern

Attribute Pattern์€ ์œ ์‚ฌํ•œ ํ•„๋“œ๊ฐ€ ๋งŽ์€ ํฐ ๋ฌธ์„œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜์ง€๋งŒ ๊ณตํ†ต ํŠน์„ฑ์„ ๊ณต์œ ํ•˜๋Š” ํ•„๋“œ ํ•˜์œ„ ์ง‘ํ•ฉ์ด ์žˆ๊ณ  ํ•ด๋‹น ํ•„๋“œ ํ•˜์œ„ ์ง‘ํ•ฉ์„ ์ •๋ ฌํ•˜๊ฑฐ๋‚˜ ์ฟผ๋ฆฌํ•˜๋ ค๋Š” ๋ฌธ์ œ์— ์œ ์šฉํ•˜๋‹ค. ์ •๋ ฌํ•ด์•ผ ํ•˜๋Š” ํ•„๋“œ์˜ ๋ฌธ์„œ๊ฐ€ ์ž‘์€ ํ•˜์œ„ ์ง‘ํ•ฉ์—์„œ๋งŒ ๋ฐœ๊ฒฌ๋˜๋Š” ๊ฒฝ์šฐ, ๋˜๋Š” ๋ฌธ์„œ ๋‚ด์—์„œ ๋‘ ์กฐ๊ฑด์ด ๋ชจ๋‘ ์ถฉ์กฑ๋˜๋Š” ๊ฒฝ์šฐ.

์žฅ์ 

  • ๋” ์ ์€ ์ธ๋ฑ์Šค๊ฐ€ ํ•„์š”ํ•จ
  • ์ฟผ๋ฆฌ ์ž‘์„ฑ์ด ๋” ๊ฐ„๋‹จํ•ด์ง€๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ ๋” ๋นจ๋ผ์ง

์˜ˆ์ œ

์ ์šฉ ์ „

{
    title: "Star Wars",
    director: "George Lucas",
    ...
    release_US: ISODate("1977-05-20T01:00:00+01:00"),
    release_France: ISODate("1977-10-19T01:00:00+01:00"),
    release_Italy: ISODate("1977-10-20T01:00:00+01:00"),
    release_UK: ISODate("1977-12-27T01:00:00+01:00"),
    ...
}

์ ์šฉ ํ›„

{
    title: "Star Wars",
    director: "George Lucas",
    โ€ฆ
    releases: [
        {
            location: "USA",
            date: ISODate("1977-05-20T01:00:00+01:00")
        },
        {
            location: "France",
            date: ISODate("1977-10-19T01:00:00+01:00")
        },
        {
            location: "Italy",
            date: ISODate("1977-10-20T01:00:00+01:00")
        },
        {
            location: "UK",
            date: ISODate("1977-12-27T01:00:00+01:00")
        },
        โ€ฆ 
    ],
    โ€ฆ 
}

3. Bucket Pattern

์‹œ๊ณ„์—ด, ์‹ค์‹œ๊ฐ„ ๋ถ„์„ ๋„๋Š” ์‚ฌ๋ฌผ ์ธํ„ฐ๋„ท(IoT) ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ ๊ฐ™์€ ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•ด์•ผ ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.

์žฅ์ 

  • ์ปฌ๋ ‰์…˜์˜ ์ „์ฒด ๋ฌธ์„œ ์ˆ˜ ๊ฐ์†Œ
  • ์ธ๋ฑ์Šค ์„ฑ๋Šฅ ํ–ฅ์ƒ
  • ์‚ฌ์ „ ์ง‘๊ณ„๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์•ก์„ธ์Šค๋ฅผ ๋‹จ์ˆœํ™” ํ•  ์ˆ˜ ์žˆ์Œ

์˜ˆ์ œ

์ ์šฉ ์ „

{
  sensor_id: 12345,
  timestamp: ISODate("2019-01-31T10:00:00.000Z"),
  temperature: 40
}
{
  sensor_id: 12345,
  timestamp: ISODate("2019-01-31T10:01:00.000Z"),
  temperature: 40
}
{
  sensor_id: 12345,
  timestamp: ISODate("2019-01-31T10:02:00.000Z"),
  temperature: 41
}

์ ์šฉ ํ›„

{
    sensor_id: 12345,
    start_date: ISODate("2019-01-31T10:00:00.000Z"),
    end_date: ISODate("2019-01-31T10:59:59.000Z"),
    measurements: [
        {
            timestamp: ISODate("2019-01-31T10:00:00.000Z"),
            temperature: 40
        },
        {
            timestamp: ISODate("2019-01-31T10:01:00.000Z"),
            temperature: 40
        },
        ...
        {
            timestamp: ISODate("2019-01-31T10:42:00.000Z"),
            temperature: 42
        }   
    ],
    transaction_count: 42,
    sum_temperature: 2413
}
  • Document์˜ ํฌ๊ธฐ๊ฐ€ 16MB๋กœ ์ œํ•œ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ํ•ด๋‹น ์ œํ•œ์„ ์ดˆ๊ณผํ•˜์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•ด์•ผ ํ•จ
  • ํ•ด๋‹น Bucket์— ํฌํ•จ ๋œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ Aggregation ์—ฐ์‚ฐ์€ ๋‹จ์ผ Document ๋‚ด๋ถ€์—์„œ ์ง„ํ–‰ ๊ฐ€๋Šฅํ•จ โ‡’ Aggregation ์—ฐ์‚ฐ์„ ๋‚ฎ์€ ๋น„์šฉ์— ํ•  ์ˆ˜ ์žˆ์Œ

4. Computed Pattern

๋งค์šฐ ์ฝ๊ธฐ ์ง‘์•ฝ์ ์ธ ๋ฐ์ดํ„ฐ ์•ก์„ธ์Šค ํŒจํ„ด์ด ์žˆ๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์œ ์šฉํ•˜๋‹ค.

์žฅ์ 

  • ๋นˆ๋ฒˆํ•œ ๊ณ„์‚ฐ์„ ์œ„ํ•œ CPU ์ž‘์—…๋Ÿ‰ ๊ฐ์†Œ
  • ์ฟผ๋ฆฌ ์ž‘์„ฑ์ด ๋” ๊ฐ„๋‹จํ•ด์ง€๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ ๋” ๋นจ๋ผ์ง

๋‹จ์ 

  • ๋ณธ ํŒจํ„ด์˜ ํ•„์š”์„ฑ์„ ์‹๋ณ„ํ•˜๊ธฐ ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์Œ
  • ํŒจํ„ด์„ ์ ์šฉํ•˜๊ฑฐ๋‚˜ ๋‚จ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ•„์š”ํ•˜์ง€ ์•Š๋Š” ํ•œ ํ”ผํ•ด์•ผ ํ•จ

์˜ˆ์ œ

image

5. Document Versioning Pattern

MongoDB์—์„œ Document์˜ ์ด์ „ ๋ฒ„์ „์„ ์œ ์ง€ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋œ๋‹ค.

์žฅ์ 

  • ๊ธฐ์กด ์‹œ์Šคํ…œ์—์„œ๋„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Œ
  • ์ตœ์‹  ๋ฒ„์ „์˜ ์ฟผ๋ฆฌ ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์Œ

๋‹จ์ 

  • ์“ฐ๊ธฐ ํšŸ์ˆ˜๊ฐ€ 2๋ฐฐ๋กœ ๋Š˜์–ด๋‚จ
  • ์ฟผ๋ฆฌ๋Š” ์˜ฌ๋ฐ”๋ฅธ ์ปฌ๋ ‰์…˜์„ ๋Œ€์ƒ์œผ๋กœ ํ•ด์•ผ ํ•จ

์˜ˆ์ œ

image

image

6. Extended Reference Pattern

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ž์ฃผ ์•ก์„ธ์Šคํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ์œผ๊ธฐ ์œ„ํ•ด ๋งŽ์€ JOIN ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ๊ฐ€์žฅ ์œ ์šฉํ•˜๋‹ค.

์žฅ์ 

  • JOIN ์ž‘์—…์ด ๋งŽ์„ ๋•Œ ์„ฑ๋Šฅ์ด ํ–ฅ์ƒ๋จ
  • ์ฝ๊ธฐ ์†๋„๊ฐ€ ๋นจ๋ผ์ง€๊ณ  ์ „์ฒด JOIN ์ˆ˜ ๊ฐ์†Œ

๋‹จ์ 

  • ๋ฐ์ดํ„ฐ ๋ณต์ œ

์˜ˆ์ œ

image

7. Outlier Pattern

์ผ๋ฐ˜์ ์ธ ๋ฐ์ดํ„ฐ ํŒจํ„ด์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์— ๋งž์ง€ ์•Š๋Š” ๋ช‡ ๊ฐ€์ง€ ์ฟผ๋ฆฌ๋‚˜ ๋ฌธ์„œ๊ฐ€ ์žˆ๊ณ , ์ด๋Ÿฌํ•œ ์˜ˆ์™ธ๊ฐ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์†”๋ฃจ์…˜์„ ์ฃผ๋„ํ•  ๊ฒฝ์šฐ ์œ ์šฉํ•˜๋‹ค.

์žฅ์ 

  • ๋ช‡ ๊ฐ€์ง€ Document ํ˜น์€ ์ฟผ๋ฆฌ๊ฐ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์†”๋ฃจ์…˜์„ ๊ฒฐ์ •ํ•˜์ง€ ๋ชปํ•˜๋„๋ก ํ•จ
  • ์ฟผ๋ฆฌ๋Š” โ€œ์ผ๋ฐ˜์ ์ธโ€ ์‚ฌ์šฉ ์‚ฌ๋ก€์— ๋งž๊ฒŒ ์กฐ์ •๋˜์ง€๋งŒ ์ด์ƒ๊ฐ’์€ ์—ฌ์ „ํžˆ ํ•ด๊ฒฐ๋จ

๋‹จ์ 

  • ์ข…์ข… ํŠน์ • ์ฟผ๋ฆฌ์— ๋งž๊ฒŒ ์กฐ์ •๋˜๋ฏ€๋กœ ์ž„์‹œ ์ฟผ๋ฆฌ๊ฐ€ ์ž˜ ์ˆ˜ํ–‰๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Œ
  • ์ด ํŒจํ„ด์˜ ๋Œ€๋ถ€๋ถ„์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ๋กœ ์ˆ˜ํ–‰๋จ

์˜ˆ์ œ

์ผ๋ฐ˜์ ์ธ ๋„์„œ ๊ตฌ๋งค ๋‚ด์—ญ

{
    "_id": ObjectID("507f1f77bcf86cd799439011")
    "title": "A Genealogical Record of a Line of Alger",
    "author": "Ken W. Alger",
    โ€ฆ,
    "customers_purchased": ["user00", "user01", "user02"]

}

๋ฒ ์ŠคํŠธ ์…€๋Ÿฌ : ****1000๋ช… ์ด์ƒ์˜ ๊ตฌ๋งค์ž๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€

{
    "_id": ObjectID("507f191e810c19729de860ea"),
    "title": "Harry Potter, the Next Chapter",
    "author": "J.K. Rowling",
    โ€ฆ,
   "customers_purchased": ["user00", "user01", "user02", โ€ฆ, "user999"],
   "has_extras": "true"
}

8. Pre-allocation Pattern

๋ฌธ์„œ ๊ตฌ์กฐ๋ฅผ ์•Œ๊ณ  ์žˆ๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋‹จ์ˆœํžˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ฑ„์›Œ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์œ ์šฉํ•˜๋‹ค.

์žฅ์ 

  • ๋ฌธ์„œ ๊ตฌ์กฐ๊ฐ€ ๋ฏธ๋ฆฌ ์•Œ๋ ค์ง„ ๊ฒฝ์šฐ ๋””์ž์ธ ๋‹จ์ˆœํ™”

๋‹จ์ 

  • ๋‹จ์ˆœ์„ฑ๊ณผ ์„ฑ๋Šฅ

์˜ˆ์ œ

๊ทผ๋ฌด์ผ์„ ๋ฐฐ์—ด๋กœ ์ €์žฅ

image

๊ทผ๋ฌด์ผ์„ ๋ฒ”์œ„ ๋ชฉ๋ก์œผ๋กœ ๋ฐฐ์—ด์— ์ €์žฅ

image

9. Polymorphic Pattern

์ฐจ์ด์ ๋ณด๋‹ค ์œ ์‚ฌ์„ฑ์ด ๋” ๋งŽ์€ ๋‹ค์–‘ํ•œ ๋ฌธ์„œ๊ฐ€ ์žˆ๊ณ  ๋ฌธ์„œ๋ฅผ ๋‹จ์ผ ์ปฌ๋ ‰์…˜์— ๋ณด๊ด€ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ์œ ์šฉํ•˜๋‹ค.

์žฅ์ 

  • ๊ตฌํ˜„์ด ์‰ฌ์›€
  • ์ฟผ๋ฆฌ๋ฅผ ๋‹จ์ผ ์ปฌ๋ ‰์…˜์—์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Œ

์˜ˆ์ œ

์ ์šฉ์ „

image

์ ์šฉ ํ›„

image

10. Schema Versioning Pattern

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ย ๋ฐ์ดํ„ฐ ์Šคํ‚ค๋งˆ์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ์ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ˆ˜๋ช… ๋™์•ˆ ์ž์ฃผ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ(๊ฑฐ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ๊ฐ€ ํ•ด๋‹น๋จ), ์ด ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฌธ์„œ์˜ ์ด์ „ ๋ฒ„์ „๊ณผ ํ˜„์žฌ ๋ฒ„์ „์ด ์ปฌ๋ ‰์…˜์— ๋‚˜๋ž€ํžˆ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์žฅ์ 

  • โ€˜๋‹ค์šด ํƒ€์ž„โ€™์ด ํ•„์š”ํ•˜์ง€ ์•Š์Œ
  • ์Šคํ‚ค๋งˆ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ œ์–ด
  • ํ–ฅํ›„ ๊ธฐ์ˆ  ๋ถ€์ฑ„ ๊ฐ์†Œ

๋‹จ์ 

  • ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ์ค‘์— ๋™์ผํ•œ ํ•„๋“œ์— ๋Œ€ํ•ด ๋‘ ๊ฐœ์˜ ์ธ๋ฑ์Šค๊ฐ€ ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Œ

์˜ˆ์ œ

Version 1

{
  "_id": "<ObjectId>",
  "name": "Darth Vader",
  "home": "503-555-0100",
  "work": "503-555-0110",
  "mobile": "503-555-0120"
}

Version 2

{
  "_id": "<ObjectId>",
  "schema_version": "2",
  "name": "Anakin Skywalker (Retired)",
  "contact_method": [
    { "work": "503-555-0210" },
    { "mobile": "503-555-0220" },
    { "twitter": "@anakinskywalker" },
    { "skype": "AlwaysWithYou" }
  ]
}

11. Subset Pattern

์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋ฌธ์„œ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์€ ๋Œ€์šฉ๋Ÿ‰ ๋ฌธ์„œ๋กœ ์ธํ•ด ์ž‘์—… ์ง‘ํ•ฉ์ด RAM ์šฉ๋Ÿ‰์„ ์ดˆ๊ณผํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.

์žฅ์ 

  • ์ž‘์—… ์„ธํŠธ์˜ ์ „์ฒด ํฌ๊ธฐ ๊ฐ์†Œ
  • ๊ฐ€์žฅ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋””์Šคํฌ ์•ก์„ธ์Šค ์‹œ๊ฐ„ ๋‹จ์ถ•

๋‹จ์ 

  • ํ•˜์œ„ ์ง‘ํ•ฉ์„ ๊ด€๋ฆฌํ•ด์•ผ ํ•จ
  • ์ถ”๊ฐ€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€ํ•œ ์ถ”๊ฐ€ ์ด๋™์ด ํ•„์š”

์˜ˆ์ œ

image

  • ์˜จ๋ผ์ธ ์‡ผํ•‘๋ชฐ์—์„œ ์ตœ๊ทผ ๋ฆฌ๋ทฐ 5๊ฐœ๋งŒ ํ‘œ์‹œ๋˜๊ณ , ๋”๋ณด๊ธฐ๋ฅผ ํด๋ฆญํ•ด์•ผ ์ „์ฒด ๋ฆฌ๋ทฐ ๋ชฉ๋ก์„ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ
  • ์ธ๊ธฐ ์ƒํ’ˆ ๋ชฉ๋ก์„ ์šฐ์„  ํ‘œ์‹œํ•˜๊ณ  ๋”๋ณด๊ธฐ๋ฅผ ํด๋ฆญํ•ด์•ผ ์ „์ฒด ์ƒํ’ˆ ๋ชฉ๋ก์ด ํ‘œ์‹œ ๋˜๋Š” ๊ฒƒ
  • ๋ฏธ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค์–ด ๋†“๊ณ  ์š”์ฒญ์ด ์žˆ์„ ๊ฒฝ์šฐ๋งŒ ์ถ”๊ฐ€ ์ฟผ๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•˜๋ฏ€๋กœ, MongoDB์˜ ์กฐํšŒ ์š”์ฒญ์„ ์ค„์ผ ์ˆ˜ ์žˆ์Œ

12. Tree Pattern

๋ฐ์ดํ„ฐ๊ฐ€ ๊ณ„์ธต์  ๊ตฌ์กฐ์ด๊ณ  ์ž์ฃผ ์ฟผ๋ฆฌ๋  ๋•Œ ์œ ์šฉํ•˜๋‹ค.

์žฅ์ 

  • ์—ฌ๋Ÿฌ JOIN ์ž‘์—…์„ ํ”ผํ•จ์œผ๋กœ์จ ์„ฑ๋Šฅ ํ–ฅ์ƒ

๋‹จ์ 

  • ๊ทธ๋ž˜ํ”„ ์—…๋ฐ์ดํŠธ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๊ด€๋ฆฌํ•ด์•ผ ํ•จ

์˜ˆ์ œ

ํ…Œ์ด๋ธ”

image

ํŠธ๋ฆฌ ๊ตฌ์กฐ

image

ํ‘œํ˜„ : 5์˜ ์ƒ์œ„ ๋…ธ๋“œ๋กœ 3, 2, 1์ด ์กด์žฌํ•จ

image

13. ์‚ฌ์šฉ ์‚ฌ๋ก€ ๋ถ„์„

image

Real-Time Analytics?

  • Approximation
  • Bucket
  • Computed
  • Extended Reference
  • Preallocated
  • Schema Versioning

III. ๊ฒฐ๋ก 

๋ถ€๋„๋Ÿผ์— ์ ์šฉ

Attribute ํŒจํ„ด ํ˜น์€ Bucket ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด์•ผํ•  ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค. ์‚ฌ์‹ค Attribute ํŒจํ„ด๊ณผ Bucket ํŒจํ„ด์˜ ์ •ํ™•ํ•œ ์ฐจ์ด๋ฅผ ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค. ์ด ๋‘ ํŒจํ„ด์— ๋Œ€ํ•œ ๋น„๊ต๋Š” ์ถ”ํ›„์— ์ž์„ธํžˆ ๋‹ค๋ฃจ์–ด ๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด Bucket ํŒจํ„ด์„ ์ ์šฉํ•˜์—ฌ, ํ˜„์žฌ ์šฐ๋ฆฌ๊ฐ€ MongoDB๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ €์žฅํ•ด์•ผํ•  ๋ฐ์ดํ„ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

Workspace Data

{
	workspaceId,
  createAt,
	updateAt,
}

Object Data

{
	objectId,
	type,
	left,
	top,
	width,
	height,
	color,
	text,
	creator,
	workspaceId,
	fontSize,
	scaleX,
	scaleY,
}

์ข…ํ•ฉํ•˜๋ฉด ํ•˜๋‚˜์˜ Document์— ์ €์žฅํ•ด์•ผํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

{
	workspaceId: 'workspaceId',
  createAt: ISODate(""),
	updateAt: ISODate(""),
  objects: [
		{
			objectId, type, left, top, width, height, color, text, creator, workspaceId, fontSize, scaleX, scaleY,
		},
		{
			objectId, type, left, top, width, height, color, text, creator, workspaceId, fontSize, scaleX, scaleY,
		},
		...
		{
			objectId, type, left, top, width, height, color, text, creator, workspaceId, fontSize, scaleX, scaleY,
		}
	],
	...
}

ํ•˜๋‚˜์˜ Document์—๋Š” workspaceId๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ํ•ด๋‹น Workspace์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” Object๋“ค์„ ๋ฐฐ์—ด์— ์ €์žฅํ•˜๋„๋ก ํ•œ๋‹ค.

๋Š๋‚€ ์ 

MongoDB๋Š” ๊ต‰์žฅํžˆ ์ž์œจ์„ฑ์ด ๋†’์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ธ ๊ฒƒ ๊ฐ™๋‹ค. ๋‹จ์ˆœํžˆ โ€˜RDBMS๋ณด๋‹ค ์†๋„๊ฐ€ ๋น ๋ฅด๋‹คโ€™๋ผ๋Š” ์ƒ๊ฐ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ํฐ ์ฝ” ๋‹ค์น  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ์ˆœํžˆ MongoDB๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์ง€๋งŒ, ๊ทธ ์ „์— ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์žก๊ณ  ๊ฐ€๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค. ๋ฌดํ„ฑ๋Œ€๊ณ  ์‚ฌ์šฉํ•˜๋‹ค๊ฐ€๋Š” MongoDB์˜ ์žฅ์ ์ธ โ€˜์„ฑ๋Šฅโ€™๊ณผ โ€˜๊ฐ€์šฉ์„ฑ' ๋‘ ๋งˆ๋ฆฌ ํ† ๋ผ ๋ชจ๋‘ ๋†“์น  ์ˆ˜ ์žˆ๋‹ค.


IV. Reference

Building with Patterns: A Summary | MongoDB Blog

๐Ÿ“š ๊ทธ๋ผ์šด๋“œ ๋ฃฐ

โœ๏ธ ์ปจ๋ฒค์…˜

๐Ÿง‘โ€๐Ÿซ ๋ฉ˜ํ† ๋ง

๐Ÿ“ ์• ์ž์ผ ํ”„๋กœ์„ธ์Šค

๊ธฐํš
๋ฐ์ผ๋ฆฌ ์Šคํฌ๋Ÿผ
์Šคํ”„๋ฆฐํŠธ ๋ฆฌ๋ทฐ
์Šคํ”„๋ฆฐํŠธ ํšŒ๊ณ 
ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…
๊ธฐํƒ€ ์‚ฐ์ถœ๋ฌผ

๐Ÿ“– ๊ธฐ์ˆ ๋ฌธ์„œ

Week2
Week3
Week4
Week5

๐Ÿ—‚ ์ฐธ๊ณ ๋ฌธ์„œ

Clone this wiki locally