-
Notifications
You must be signed in to change notification settings - Fork 5
MongoDB
gyim1345 edited this page Dec 21, 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
- joinless λμ κ³ μ crud query
- νλ ₯μ μΈ νμ₯μ±: λ°μ΄ν°λ² μ΄μ€λ μ λ ΄ν μΌλ° νλμ¨μ΄μ μ¬μ©νλλ‘ μ€κ³λμλ€.
- λΉ λ°μ΄ν° μ ν리μΌμ΄μ : NoSQL λ°μ΄ν°λ² μ΄μ€μμ λλμ λ°μ΄ν°λ₯Ό μ½κ² μ²λ¦¬ν μ μλ€.
- λΉμ©μ κ°: κ΄κ³ν λ°μ΄ν°λ² μ΄μ€λ κ³ κ°μ μ€ν λ¦¬μ§ μμ€ν κ³Ό λ μ μλ²λ₯Ό μ€μΉν΄μΌ νλ λ°λ©΄, NoSQL λ°μ΄ν°λ² μ΄μ€λ νΈλμμ κ³Ό λ°μ΄ν° λ³Όλ₯¨μ΄ μ¦κ°ν¨μ λ°λΌ μ λ ΄ν λ²μ© νλμ¨μ΄ ν΄λ¬μ€ν°μ μ½κ² μ€μΉν μ μλ€. λ°λΌμ λ μ μ λΉμ©μΌλ‘ λ λ§μ λ°μ΄ν°λ₯Ό μ²λ¦¬νκ³ μ μ₯ν μ μλ€.
- λμ μ€ν€λ§: λ°μ΄ν° μμ μ μμνλ λ° μ€ν€λ§κ° νμνμ§ μμ SQL λ°μ΄ν°λ² μ΄μ€λ€. κ΄κ³ν λ°μ΄ν°λ² μ΄μ€μμ λ¨Όμ μ€ν€λ§λ₯Ό μ μν΄μΌ νλ―λ‘ μꡬμ¬νμ΄ λ³κ²½λ λλ§λ€ μ€ν€λ§λ₯Ό λ³κ²½ν΄μΌ νλ―λ‘ λ μ΄λ ΅λ€.
- sharding: κ΄κ³ν λ°μ΄ν°λ² μ΄μ€λ μΈλ‘λ‘ νμ₯λλ―λ‘, μ¬λ¬ μλ²μ λΆμ°λμ΄ μλ λ°μ΄ν°λ² μ΄μ€κ° λ§μ κ²½μ°κ° λ§λ€. μΌλ°μ μΌλ‘ NoSQL λ°μ΄ν°λ² μ΄μ€λ μλ μ€λ©μ μ§μνλ€. μ¦, μ ν리μΌμ΄μ μ΄ μλ² νμ ꡬμ±μ‘°μ°¨ μΈμν νμ μμ΄ κΈ°λ³Έμ μΌλ‘ μμμ μμ μλ²μ λ°μ΄ν°λ₯Ό μλμΌλ‘ λΆμ°μν¨λ€.
- Replication: λλΆλΆμ NoSQL λ°μ΄ν°λ² μ΄μ€λ κ°λ μ€λ¨ λλ κ³νλ μ μ§λ³΄μ μ΄λ²€νΈ μ κ°μ©μ±μ μ μ§νκΈ° μν΄ μλ λ°μ΄ν°λ² μ΄μ€ 볡μ λ₯Ό μ§μνλ€.
- μΊμ±: λλΆλΆμ NoSQL κΈ°μ μ λ°μ΄λ ν΅ν© μΊμ± κΈ°λ₯μ κ°μΆκ³ μμ΄ μμ£Ό μ¬μ©λλ λ°μ΄ν°λ₯Ό μμ€ν λ©λͺ¨λ¦¬μ μ΅λν λ§μ΄ 보κ΄νλ©° λ³λμ μΊμ± κ³μΈ΅μ΄ νμνμ§ μλλ€.
- λ°μ΄ν°λ² μ΄μ€μ μμ μ± κΈ°λ₯μ΄ μμ(acid νμ§ μλ€)
- ACID(μμμ±, μΌκ΄μ±, 격리, λ΄κ΅¬μ±)λ μ€λ₯, μ μ μ₯μ λ° κΈ°ν μ₯μ μλ λΆκ΅¬νκ³ λ°μ΄ν° μ ν¨μ±μ 보μ₯νκΈ° μν λ°μ΄ν°λ² μ΄μ€ νΈλμμ μ μμ± μ§ν©μ΄λ€.
- JSONμ΄λ λΉμ·ν ννλΌ μ무λ κ²λ λ£μ μ μμ§λ§ λ³΄ν΅ 3κ°μ§ λ°©μμΌλ‘ μ μ₯/μ¬μ© νλ€.
- Embedded Document Pattern
- λͺ¨λ λ°μ΄ν°λ₯Ό ν collectionμ μ μ₯νλ λ°©μ
- μ₯μ : ν μΏΌλ¦¬λ‘ μ 보λ₯Ό λ€ κ°μ Έμ¬ μ μλ€
- λ¨μ :
- Subset Pattern
- μ¬λ¬κ°μ collectionμ λ§λ€μ§λ§ collectionμμ λ€λ₯Έ collectionμ μ 보λ₯Ό μ€λ³΅μ μΌλ‘ κ°μ§κ³ μλ λ°©μ
- μ₯μ : collectionμ΄ λΉλν΄μ§λκ²μ λ°©μ§νκ³ μμ£Ό μ νλ dataλ€μ subsetμ ν¬ν¨ν¨μΌλ‘μ¨ caching λΉμ·ν μν μ ν μ μλ€.
- λ¨μ : μμ μ΄ μλ€λ©΄ κ·Έ dataκ° μλ λͺ¨λ collectionμμ μ°Ύμμ μμ ν΄μΌ λμ λ§μ΄ λλ €μ§ μ μλ€.
- Reference Pattern
- Mysql κΈ°λ³Έμ μΈ κ΄κ³νμ²λΌ ꡬν ν λ μ°λ λ°©μ
- κ΄κ³ν μ°λ μκ° λΆν° NoSQL μ°λ μ΄μ λ₯Ό μκ° ν΄λ΄μΌ ν κ² κ°λ€.
- μλ μμμ§λ§ μ΅κ·Όμ μκ²Όλ€ 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λ³΄λ€ μΏΌλ¦¬λ¬Έμ λ μ§μΌ λμ λΆνΈνλ μ μ΄ μμλ€.