
Collection::find 메소드는 데이터베이스에서 데이터를 조회할 때 사용됩니다.
여러 개의 collection이 있을 때, 각 collection의 데이터를 구분하여 반환하려면, 다음과 같은 방법을 사용할 수 있습니다.
- $collectionName : collection 이름을 지정하여 데이터를 조회할 수 있습니다. 예를 들어, `$collectionName = 'users';` 이면, users collection의 데이터를 조회할 수 있습니다.
- $filter : 특정 필드에 해당하는 데이터를 검색할 수 있습니다. 예를 들어, `$filter = ['name' => 'John'];` 이면, name 필드가 'John'인 데이터를 조회할 수 있습니다.
- $sort : 데이터를 정렬할 수 있습니다. 예를 들어, `$sort = ['age' => 1];` 이면, age 필드를 기준으로 데이터를 오름차순으로 정렬할 수 있습니다.
Collection::find 메소드 사용 예제:
#hostingforum.kr
php
$collection = db->selectCollection('users');
$users = $collection->find(['name' => 'John']);
foreach ($users as $user) {
echo $user['name'] . "n";
}
$collection = db->selectCollection('users');
$users = $collection->find()->sort(['age' => 1]);
foreach ($users as $user) {
echo $user['name'] . " (" . $user['age'] . ")n";
}
위 예제에서, `$collection->find(['name' => 'John'])`은 name 필드가 'John'인 데이터를 조회합니다. `$collection->find()->sort(['age' => 1])`은 age 필드를 기준으로 데이터를 오름차순으로 정렬합니다.
2025-03-21 01:08