라이브러리
[PHP] CollectionFind::fields - 문서 필드 필터 설정
CollectionFind::fields
CollectionFind::fields는 MongoDB의 CollectionFind 메소드에서 사용할 수 있는 옵션 중 하나입니다. 이 옵션은 특정 필드를 조회할 때 사용되며, 조회할 필드를 지정할 수 있습니다.
사용 방법
CollectionFind::fields는 CollectionFind 메소드의 옵션 중 하나로, 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
php
use MongoDBCollection;
$collection = new Collection($client, 'mydatabase', 'mycollection');
$result = $collection->find(['field1' => 'value1'], ['projection' => ['field1' => 1, 'field2' => 0]]);
위 예제에서, `find` 메소드의 두 번째 인자 `['projection' => ['field1' => 1, 'field2' => 0]]`는 CollectionFind::fields 옵션을 사용하여 특정 필드를 조회합니다. `'field1' => 1`은 `field1` 필드를 조회하고, `'field2' => 0`은 `field2` 필드를 조회하지 않습니다.
예제
다음 예제에서는 CollectionFind::fields 옵션을 사용하여 특정 필드를 조회하는 방법을 보여줍니다.
#hostingforum.kr
php
use MongoDBCollection;
// MongoDB 클라이언트 생성
$client = new MongoDBClient('mongodb://localhost:27017');
// 데이터베이스와 컬렉션 생성
$database = $client->mydatabase;
$collection = $database->mycollection;
// 데이터 삽입
$document1 = ['name' => 'John', 'age' => 25, 'city' => 'New York'];
$document2 = ['name' => 'Jane', 'age' => 30, 'city' => 'Los Angeles'];
$collection->insertMany([$document1, $document2]);
// CollectionFind::fields 옵션 사용하여 특정 필드를 조회
$result = $collection->find(['name' => 'John'], ['projection' => ['name' => 1, 'age' => 0]]);
// 결과 출력
foreach ($result as $document) {
print_r($document);
}
위 예제에서, `find` 메소드의 두 번째 인자 `['projection' => ['name' => 1, 'age' => 0]]`는 CollectionFind::fields 옵션을 사용하여 `name` 필드를 조회하고, `age` 필드를 조회하지 않습니다. 결과는 다음과 같습니다.
#hostingforum.kr
php
Array
(
[name] => John
)
참고
CollectionFind::fields 옵션은 MongoDB 3.6 버전부터 사용할 수 있습니다. 이전 버전에서는 `projection` 옵션을 사용하여 특정 필드를 조회할 수 있습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.