🛠️ 기능
MongoDB에 원하는 컬렉션을 가져오는 기능
🔍 코드 해석
public static MongoCollection<Document> getCollection(String colName) {
MongoCollection<Document> collection = database.getCollection(colName);
return collection;
}
- MongoCollection<Document>
- MongoDB의 특정 컬렉션 객체
- 테이블과 같은 개념이며, 문서(Document) 단위로 데이터를 저장
- database.getCollection(colName)
- database는 이미 연결된 MongoDatabase
- database에서 colName에 해당하는 컬렉션을 가지고 옴
- ex) colName이 "todos"면 todo_db 안의 todos 컬렉션을 가지고 옴
client.getDatabase("todo_db"); // 연결된 DB 객체, todo_db라는 이름의 데이터베이스를 사용한다고 가정
📌 사용 예시
MongoCollection<Document> todos = Database.getCollection("todos");
todo_db안의 "todos" 컬렉션을 사용할 수 있게 됨.
todos.insertOne(new Document("title", "공부하기").append("done", false));
데이터 조작 가능
'Java > MongoDB' 카테고리의 다른 글
[MongoDB] Database - ConnectionString (1) | 2025.05.27 |
---|