-
Notifications
You must be signed in to change notification settings - Fork 5
MongoDB
gyim1345 edited this page Dec 19, 2020
·
5 revisions
-
nosql(Not Only SQL)
-
Document Database. CollectionsμΌλ‘ κ΅¬μ± λμ΄ μλλ° collectionμ tableμ΄λΌκ³ μκ° νλ©΄ νΈνκ³ tableμμ recordλ documentλ‘ κ΅¬μ± λμ΄ μλ€.
-
documentλ field/value κ°μ κ°μ§λλ° μ΄κ²μ jsonμ΄λ κ±°μ κ°μ ꡬ쑰λΌκ³ μκ° νλ©΄λλ€.
-
λ€λ₯Έ μ μ documentκ° μμ± λλ©΄ κ³ μ field μΈ _idμ μμ μ«μκ° valueκ°μ λ€μ΄κ°λ€.
-
νΉμ§
- λΆμ° μ»΄ν¨ν μμ€ν .(sharding, replica)
- νμ₯μ±μ΄ λ°μ΄λ¨.(μννμ₯ κ°λ₯)
- λΉμ© μ κ° κ°λ₯ (μ λ ΄ν λ²μ© νλμ¨μ΄ ν΄λ¬μ€ν°μ μ½κ² μ€μΉν μ μμ)
- μ μ°ν μ€ν€λ§ μ€κ³.
- λΉμ ν λ° λ°μ ν λ°μ΄ν°λ₯Ό μ²λ¦¬ν¨.(json, word, pdf...)
- 볡μ‘ν κ΄κ³λ μλ€.
- μ€ν μμ€λ€.
-
μ₯μ
- documentλ json ννλΌκ³ λ³Όμλ μμΌλ λ§μ μΈμ΄μ νΈνμ΄ λλ€.
- μ νν λ§ν΄μ bjsonμ΄λ€. bjsonμ binary JSONμ μ½μμ΄λ€. json κ°μ λ¬Έμλ€μ binaryλ‘ encode νκ²μ΄λ€.
- λμ μ€ν€λ§/Schema-less
- collectionλ΄μ κ° Documentλ μ€ν€λ§κ° μκ±°λ λ€λ₯Ό μ μλ€. (rdbms λ schema λ₯Ό λ³κ²½μ λͺ¨λ λ°μ΄ν°λ₯Ό λ³κ²½ ν΄μΌ νμ§λ§ nosqlμ κ·Έλ μ§ μλ€.)
- joinless λμ κ³ μ crud query
- Scalability(νμ₯μ±)μ΄ μ’κ³ Sharding(db λΆν κΈ°μ )ν΄λ¬μ€ν° κ΅¬μΆ μ΄ κ°λ₯νλ€
- 곡μλ¬Έμ(https://www.mongodb.com/nosql-explained/advantages)
- μ€μΌμΌ μμ μν€ν
μ²λ‘ λλμ λ°μ΄ν°λ₯Ό κ³ μμΌλ‘ μ²λ¦¬ (scalability/sharding)
- μ μ»΄ν¨ν°μ μ€ν 리μ§λ₯Ό ν΄λ¬μ€ν°μ μ½κ² μΆκ° ν μμλ ν΄λΌμ°λ μ»΄ν¨ν νκ²½μμ ꡬννλ λ° νΉν μ΄λ €μμ΄ μλ€.
- NoSQL μμ€ν
μ μν νμ₯ μν€ν
μ²λ λ°μ΄ν° λ³Όλ₯¨μ΄λ νΈλν½μ΄ μ¦κ° ν λ νμ₯μ±μ λν λͺ
νν κ²½λ‘λ₯Ό μ 곡νλ€. SQL λ°μ΄ν°λ² μ΄μ€λ‘ λμΌν μ νμ νμ₯ μ±μ λ¬μ±νλ €λ©΄ λΉμ©μ΄ λ§μ΄ λ€κ±°λ λ§μ μμ§λμ΄λ§μ΄ νμνκ±°λ μ€ν λΆκ°λ₯ν μ μλ€
- μν νμ₯μ λ λ§μ μμ€ν μ μΆκ°νμ¬ νμ₯
- μμ§ νμ₯μ κΈ°μ‘΄ μμ€ν μ λ λ§μ μ λ ₯ (CPU, RAM)μ μΆκ°νμ¬ νμ₯.
- λΉ κ΅¬μ‘°μ , λ° κ΅¬μ‘°μ λλ ꡬ쑰μ λ°μ΄ν° μ μ₯ (μ μ°μ±)
- NoSQL λ°μ΄ν°λ² μ΄μ€λ λ°μ΄ν°λ₯Ό μ΄ν΄νκΈ° μ½κ³ μ ν리μΌμ΄μ μμ λ°μ΄ν°λ₯Ό μ¬μ©νλ λ°©μμ λ κ°κΉκ² μ μ₯ν μ μλ€. λ°μ΄ν°λ₯Ό μ¬μ©νκΈ° μν΄ μ μ₯νκ±°λ κ²μ ν λ νμν λ³νμ΄ λ μ λ€. ꡬ쑰ν, λΉ κ΅¬μ‘°ν λλ λ° κ΅¬μ‘°ν μ¬λΆμ κ΄κ³μμ΄ λ€μν μ νμ λ°μ΄ν°λ₯Ό λ³΄λ€ μ½κ² ββμ μ₯νκ³ κ²μ ν μ μμ΅λλ€.
- μ€ν€λ§ λ° νλμ λν κ°νΈν μ
λ°μ΄νΈ νμ±ν (λ€μμ±/μ μ°μ±)
- κ΅¬μ‘°κ° μμ΄μ μλ‘μ΄ κ΅¬μ‘°λ‘ μ μ₯ κ°λ₯
- ꡬ쑰 λ°©ν΄ μμ΄ λ§ λ£μ μ μλ€.
- κ°λ°μ μΉνμ
- NoSQL λ°μ΄ν°λ² μ΄μ€λ μ ν리μΌμ΄μ μ μ¬μ©λλ λ°μ΄ν° κ°μ²΄μ μ’ λ₯μ κ°κΉμ΄ μμμ λ°μ΄ν°λ₯Ό μ μ₯νλ―λ‘ λ°μ΄ν°λ² μ΄μ€ μνμΌλ‘ λ°μ΄ν°λ₯Ό μ΄λν λ νμν λ³νμ΄ μ λ€.
- ν΄λΌμ°λλ₯Ό μ΅λν νμ©νμ¬ λ€μ΄ νμ μ λ‘ μ 곡
- μ¬μ©νλ νμ₯ μν€ν μ²λ λ°©λν λ°μ΄ν° μΈνΈμ λλμ νΈλν½μ μμ© ν μμλ λͺ νν νμ₯ κ²½λ‘λ₯Ό μ 곡 ν λΏλ§ μλλΌ μ»΄ν¨ν° ν΄λ¬μ€ν°λ₯Ό μ¬μ©νμ¬ λ°μ΄ν°λ² μ΄μ€λ₯Ό μ 곡νλ©΄ λ°μ΄ν°λ² μ΄μ€κ° μλμΌλ‘ μ©λμ νμ₯ λ° μΆμ ν μ μλ€
- μ€μΌμΌ μμ μν€ν
μ²λ‘ λλμ λ°μ΄ν°λ₯Ό κ³ μμΌλ‘ μ²λ¦¬ (scalability/sharding)
- documentλ json ννλΌκ³ λ³Όμλ μμΌλ λ§μ μΈμ΄μ νΈνμ΄ λλ€.
-
λ¨μ
- λ°μ΄ν°λ² μ΄μ€μ μμ μ± κΈ°λ₯μ΄ μμ(acid νμ§ μλ€)
- ACID(μμμ±, μΌκ΄μ±, 격리, λ΄κ΅¬μ±)λ μ€λ₯, μ μ μ₯μ λ° κΈ°ν μ₯μ μλ λΆκ΅¬νκ³ λ°μ΄ν° μ ν¨μ±μ 보μ₯νκΈ° μν λ°μ΄ν°λ² μ΄μ€ νΈλμμ μ μμ± μ§ν©μ΄λ€.
- λ°μ΄ν°λ² μ΄μ€μ μμ μ± κΈ°λ₯μ΄ μμ(acid νμ§ μλ€)
-
λ°μ΄ν° νν λ°©μ
- JSONμ΄λ λΉμ·ν ννλΌ μ무λ κ²λ λ£μ μ μμ§λ§ λ³΄ν΅ 3κ°μ§ λ°©μμΌλ‘ μ μ₯/μ¬μ© νλ€.
- Embedded Document Pattern
- λͺ¨λ λ°μ΄ν°λ₯Ό ν collectionμ μ μ₯νλ λ°©μ
- μ₯μ : ν μΏΌλ¦¬λ‘ μ 보λ₯Ό λ€ κ°μ Έμ¬ μ μλ€
- λ¨μ :
- Subset Pattern
- μ¬λ¬κ°μ collectionμ λ§λ€μ§λ§ collectionμμ λ€λ₯Έ collectionμ μ 보λ₯Ό μ€λ³΅μ μΌλ‘ κ°μ§κ³ μλ λ°©μ
- μ₯μ : collectionμ΄ λΉλν΄μ§λκ²μ λ°©μ§νκ³ μμ£Ό μ νλ dataλ€μ subsetμ ν¬ν¨ν¨μΌλ‘μ¨ caching λΉμ·ν μν μ ν μ μλ€.
- λ¨μ : μμ μ΄ μλ€λ©΄ κ·Έ dataκ° μλ λͺ¨λ collectionμμ μ°Ύμμ μμ ν΄μΌ λμ λ§μ΄ λλ €μ§ μ μλ€.
- Reference Pattern
- Mysql κΈ°λ³Έμ μΈ κ΄κ³νμ²λΌ ꡬν ν λ μ°λ λ°©μ
- κ΄κ³ν μ°λ μκ° λΆν° NoSQL μ°λ μ΄μ λ₯Ό μκ° ν΄λ΄μΌ ν κ² κ°λ€.
-
join
- μλ μμμ§λ§ μ΅κ·Όμ μκ²Όλ€ aggregate!!
//μΈλΆ collection db.inventory.insert([ { "_id" : 1, "sku" : "almonds", "description": "product 1", "instock" : 120 }, { "_id" : 2, "sku" : "bread", "description": "product 2", "instock" : 80 }, { "_id" : 3, "sku" : "cashews", "description": "product 3", "instock" : 60 }, { "_id" : 4, "sku" : "pecans", "description": "product 4", "instock" : 70 }, { "_id" : 5, "sku": null, "description": "Incomplete" }, { "_id" : 6 } ]) //λ΄λΆ collection db.orders.insert([ { "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 }, { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 }, { "_id" : 3 } ]) //쿼리문 db.orders.aggregate([ { $lookup: { from: "inventory", localField: "item", foreignField: "sku", as: "inventory_docs" } } ]) //κ²°κ³Όλ¬Ό { "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2, "inventory_docs" : [ { "_id" : 1, "sku" : "almonds", "description" : "product 1", "instock" : 120 } ] } { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1, "inventory_docs" : [ { "_id" : 4, "sku" : "pecans", "description" : "product 4", "instock" : 70 } ] } { "_id" : 3, "inventory_docs" : [ { "_id" : 5, "sku" : null, "description" : "Incomplete" }, { "_id" : 6 } ] }
reference:
- νλ‘μ νΈλ₯Ό νλ©΄μ schemaλ₯Ό μμ£Ό λ°κΎΈκ² λμλλ° μ΄λ mongodbμ schema μ μ°ν¨μ λͺΈμ λλΌκ² λμλ€.
- embedded + subset λ°©μμΌλ‘ μ¬μ© ν λ €λ€ λ³΄λκΉ μ€λ³΅ λ°μ΄ν°λ₯Ό λ§λ€μ΄μΌ νκ±°λ μμ ν΄μΌ ν λ sqlλ³΄λ€ μΏΌλ¦¬λ¬Έμ λ μ§μΌ λμ λΆνΈνλ μ μ΄ μμλ€.
