자바스크립트를 허용해주세요.
[ 자바스크립트 활성화 방법 ]
from Mohon Aktifkan Javascript!
 

[MongoDB] 3장 Collection Document 집합

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