728x90
1. Document
Document는 MongoDB의 기본 데이터 단위로 JSON 형식으로 저장되며 키-값 쌍으로 구분됩니다.
{
"name": "Alice",
"age": 30,
"email": "alice@example.com"
}
2.Collection
// Users Collection
[
{ "name": "Alice", "age": 30, "email": "alice@example.com" },
{ "name": "Bob", "age": 25, "email": "bob@example.com" },
{ "name": "Charlie", "age": 20, "email": "charlie@example.com" }
]
Collection의 특징으로는 다음과 같습니다.
- 스키마의 유연함: 같은 Collection 내에서 Document 구조가 다를 수 있어, 데이터의 유연성을 높임
- 인덱스 지원: Collection에 인덱스를 추가해 검색 성능을 향상
- CRUD 연산 지원: Document에 대해 Create, Read, Update, Delete 연산을 지원
3. Collection 사용 예
- Collection 생성
MongoDB 에서는 Collection을 명시적으로 생성하지 않고, 데이터를 삽입하면 자동 생성됩니다.
db.users.insertOne({ name: "Alice", age: 30, email: "alice@example.com" });
- Document 조회
Collection에서 Document를 조회할 수 있습니다.
db.users.find(); // 모든 Document 조회
db.users.find({ age: { $gt: 28 } }); // 나이가 30보다 큰 사용자
- Document 수정
Collection 내의 Document를 수정할 수 있습니다.
db.users.updateOne(
{ name: "Alice" },
{ $set: { age: 31 } }
);
- Document 삭제
Collection 내의 Document를 삭제합니다.
db.users.deleteOne({ name: "Alice" });
GitHub - Koras02/Mongo-Blog: https://thinky.tistory.com/category/Back-End/MongoDB
https://thinky.tistory.com/category/Back-End/MongoDB - Koras02/Mongo-Blog
github.com
728x90
LIST
'Database > MongoDB' 카테고리의 다른 글
[MongoDB] 6장 MongoDB 작업 - Aggregation (2) | 2025.08.05 |
---|---|
[MongoDB] 5장 클라이언트 도구 설치 (0) | 2025.03.20 |
[MongoDB] 4장 컬렉션과 데이터베이스 (0) | 2025.03.09 |
[MongoDB] 2장 MongoDB JSON 형식의 데이터 구조 (0) | 2025.03.02 |
[MongoDB] 1장 MongoDB란? (0) | 2025.02.27 |