라이브러리
[PHP] MongoDBBSONIterator::rewind - 반복자를 첫 번째 요소로 되감습니다.
MongoDBBSONIterator::rewind
`MongoDBBSONIterator`는 MongoDB의 BSON 문서를 읽어들이는 Iterator입니다. `rewind` 메서드는 이 Iterator를 처음부터 다시 읽을 수 있도록 합니다.
사용법
`rewind` 메서드는 다음과 같이 사용할 수 있습니다.
#hostingforum.kr
php
$iterator = $collection->find();
$iterator->rewind();
예제
다음 예제에서는 `rewind` 메서드를 사용하여 MongoDB의 컬렉션에 저장된 데이터를 모두 읽어들이는 방법을 보여줍니다.
#hostingforum.kr
php
// MongoDB 연결
$m = new MongoDBDriverManager("mongodb://localhost:27017");
// 데이터베이스와 컬렉션 선택
$db = $m->mongoDB->selectDB("mydatabase");
$collection = $db->selectCollection("mycollection");
// 컬렉션에 데이터 저장
$data = [
["name" => "John", "age" => 25],
["name" => "Jane", "age" => 30],
["name" => "Bob", "age" => 35]
];
$collection->insertMany($data);
// 컬렉션에서 데이터 읽기
$iterator = $collection->find();
$iterator->rewind();
// 데이터 출력
while ($document = $iterator->current()) {
echo "이름: " . $document["name"] . ", 나이: " . $document["age"] . "
";
$iterator->next();
}
결과
이 예제를 실행하면 컬렉션에 저장된 데이터가 모두 출력됩니다.
#hostingforum.kr
이름: John, 나이: 25
이름: Jane, 나이: 30
이름: Bob, 나이: 35
참고
* `rewind` 메서드는 Iterator의 현재 위치를 처음부터 다시 설정합니다.
* `rewind` 메서드를 호출한 후 `current` 메서드를 호출하면 Iterator의 현재 위치가 처음부터 시작됩니다.
* `rewind` 메서드는 Iterator의 데이터를 다시 읽어들이기 때문에 데이터가 변경된 경우에만 사용해야 합니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.