MongoDB에서 컬렉션의 문서를 조회하는 기본 방법
MongoDB의 컬렉션(Collection)에 저장된 문서(Document)를 조회하려면 find() 메서드를 사용합니다. 기본 문법은 다음과 같습니다.
db.yourCollectionName.find();
위 문법을 실행하면 해당 컬렉션에 있는 모든 문서가 반환됩니다. 실제 동작을 이해하기 위해 먼저 샘플 데이터가 담긴 컬렉션을 만들어 보겠습니다.
1. 테스트용 컬렉션 생성 및 데이터 삽입
insertOne() 메서드를 사용해 학생 정보 문서를 하나씩 삽입합니다.
> db.retrieveAllStudents.insertOne({"StudentId":"STUD101","StudentName":"David","StudentAge":24});
{
"acknowledged" : true, "insertedId" : ObjectId("5c6bf5cf68174aae23f5ef4e")
}
> db.retrieveAllStudents.insertOne({"StudentId":"STUD102","StudentName":"Carol","StudentAge":22});
{
"acknowledged" : true, "insertedId" : ObjectId("5c6bf5e968174aae23f5ef4f")
}
> db.retrieveAllStudents.insertOne({"StudentId":"STUD103","StudentName":"Maxwell","StudentAge":25});
{
"acknowledged" : true, "insertedId" : ObjectId("5c6bf5f768174aae23f5ef50")
}
> db.retrieveAllStudents.insertOne({"StudentId":"STUD104","StudentName":"Bob","StudentAge":23});
{
"acknowledged" : true, "insertedId" : ObjectId("5c6bf60868174aae23f5ef51")
}
> db.retrieveAllStudents.insertOne({"StudentId":"STUD105","StudentName":"Sam","StudentAge":27});
{
"acknowledged" : true, "insertedId" : ObjectId("5c6bf61b68174aae23f5ef52")
}총 5개의 학생 문서가 성공적으로 삽입되었습니다. 이제 find() 메서드로 전체 문서를 조회해 보겠습니다.
2. find() 메서드로 전체 문서 조회
> db.retrieveAllStudents.find();
실행 결과는 다음과 같습니다.
{ "_id" : ObjectId("5c6bf5cf68174aae23f5ef4e"), "StudentId" : "STUD-101", "StudentName" :
"David", "StudentAge" : 24 }
{ "_id" : ObjectId("5c6bf5e968174aae23f5ef4f"), "StudentId" : "STUD-102", "StudentName" :
"Carol", "StudentAge" : 22 }
{ "_id" : ObjectId("5c6bf5f768174aae23f5ef50"), "StudentId" : "STUD-103", "StudentName" :
"Maxwell", "StudentAge" : 25 }
{ "_id" : ObjectId("5c6bf60868174aae23f5ef51"), "StudentId" : "STUD-104", "StudentName" :
"Bob", "StudentAge" : 23 }
{ "_id" : ObjectId("5c6bf61b68174aae23f5ef52"), "StudentId" : "STUD-105", "StudentName" :
"Sam", "StudentAge" : 27 }3. pretty() 메서드로 가독성 높은 출력 만들기
기본 출력은 한 줄로 나열되어 있어 문서가 많을 경우 읽기 어려울 수 있습니다. 이때 pretty() 메서드를 함께 사용하면 각 필드가 들여쓰기된 형태로 정렬되어 훨씬 보기 편합니다.
> db.retrieveAllStudents.find().pretty();
포맷팅된 출력 결과는 다음과 같습니다.
{
"_id" : ObjectId("5c6bf5cf68174aae23f5ef4e"),
"StudentId" : "STUD-101",
"StudentName" : "David",
"StudentAge" : 24
}
{
"_id" : ObjectId("5c6bf5e968174aae23f5ef4f"),
"StudentId" : "STUD-102",
"StudentName" : "Carol",
"StudentAge" : 22
}
{
"_id" : ObjectId("5c6bf5f768174aae23f5ef50"),
"StudentId" : "STUD-103",
"StudentName" : "Maxwell",
"StudentAge" : 25
}
{
"_id" : ObjectId("5c6bf60868174aae23f5ef51"),
"StudentId" : "STUD-104",
"StudentName" : "Bob",
"StudentAge" : 23
}
{
"_id" : ObjectId("5c6bf61b68174aae23f5ef52"),
"StudentId" : "STUD-105",
"StudentName" : "Sam",
"StudentAge" : 27
}4. 조건을 지정해 특정 문서만 조회하기
find() 메서드는 조건(query filter)을 인자로 받아 원하는 문서만 골라서 조회할 수도 있습니다. 예를 들어 StudentName이 "Maxwell"인 문서만 가져오려면 다음과 같이 쿼리를 작성합니다.
> db.retrieveAllStudents.find({"StudentName":"Maxwell"}).pretty();조건에 일치하는 문서만 출력됩니다.
{
"_id" : ObjectId("5c6bf5f768174aae23f5ef50"),
"StudentId" : "STUD-103",
"StudentName" : "Maxwell",
"StudentAge" : 25
}정리
MongoDB에서 컬렉션의 문서를 조회할 때는 find() 메서드를 사용하며, 조건 없이 호출하면 모든 문서를 반환하고 조건 객체를 전달하면 일치하는 문서만 필터링해서 가져옵니다. 또한 pretty() 메서드를 함께 사용하면 결과를 사람이 읽기 좋은 형태로 확인할 수 있습니다. findOne(), limit(), sort() 등의 메서드와 조합하면 더욱 유연한 데이터 조회가 가능합니다.