라이브러리

[PHP] MongoDBBSONIterator::next - 반복자를 다음 요소로 이동합니다.


MongoDBBSONIterator는 MongoDB의 BSON 문서를 순회하는 데 사용되는 인터페이스입니다. 이 인터페이스는 MongoDB의 컬렉션에서 문서를 하나씩 순회할 수 있도록 해줍니다.

MongoDBBSONIterator::next


`next` 메소드는 현재 문서를 순회하고 다음 문서로 이동하는 메소드입니다. 이 메소드는 다음 문서가 없으면 `false`를 반환하고, 다음 문서가 있으면 `true`를 반환합니다.

예제


다음 예제는 MongoDBBSONIterator를 사용하여 MongoDB의 컬렉션에서 문서를 순회하는 방법을 보여줍니다.

#hostingforum.kr
php

// MongoDB 연결 설정

$mongo = new MongoDBDriverManager("mongodb://localhost:27017");

$db = $mongo->selectDatabase("mydb");

$collection = $db->mycollection;



// MongoDBBSONIterator 생성

$iterator = new MongoDBBSONIteratorIterator($collection->find());



// 순회

while ($iterator->valid()) {

    $document = $iterator->current();

    echo "ID: " . $document->_id . "
";

    echo "Name: " . $document->name . "
";

    $iterator->next();

}



// MongoDB 연결 종료

$mongo->close();



MongoDBBSONIterator::current


`current` 메소드는 현재 문서를 반환하는 메소드입니다. 이 메소드는 현재 문서가 없으면 `null`을 반환합니다.

예제


다음 예제는 MongoDBBSONIterator의 `current` 메소드를 사용하여 현재 문서를 반환하는 방법을 보여줍니다.

#hostingforum.kr
php

// MongoDB 연결 설정

$mongo = new MongoDBDriverManager("mongodb://localhost:27017");

$db = $mongo->selectDatabase("mydb");

$collection = $db->mycollection;



// MongoDBBSONIterator 생성

$iterator = new MongoDBBSONIteratorIterator($collection->find());



// 순회

while ($iterator->valid()) {

    $document = $iterator->current();

    echo "ID: " . $document->_id . "
";

    echo "Name: " . $document->name . "
";

    $iterator->next();

}



// MongoDB 연결 종료

$mongo->close();



MongoDBBSONIterator::valid


`valid` 메소드는 현재 문서가 존재하는지 여부를 반환하는 메소드입니다. 이 메소드는 현재 문서가 존재하면 `true`를 반환하고, 현재 문서가 존재하지 않으면 `false`를 반환합니다.

예제


다음 예제는 MongoDBBSONIterator의 `valid` 메소드를 사용하여 현재 문서가 존재하는지 여부를 반환하는 방법을 보여줍니다.

#hostingforum.kr
php

// MongoDB 연결 설정

$mongo = new MongoDBDriverManager("mongodb://localhost:27017");

$db = $mongo->selectDatabase("mydb");

$collection = $db->mycollection;



// MongoDBBSONIterator 생성

$iterator = new MongoDBBSONIteratorIterator($collection->find());



// 순회

while ($iterator->valid()) {

    $document = $iterator->current();

    echo "ID: " . $document->_id . "
";

    echo "Name: " . $document->name . "
";

    if (!$iterator->valid()) {

        break;

    }

    $iterator->next();

}



// MongoDB 연결 종료

$mongo->close();



MongoDBBSONIterator::rewind


`rewind` 메소드는 현재 문서를 처음 문서로 이동하는 메소드입니다. 이 메소드는 현재 문서가 처음 문서가 아니면 `true`를 반환하고, 현재 문서가 처음 문서이면 `false`를 반환합니다.

예제


다음 예제는 MongoDBBSONIterator의 `rewind` 메소드를 사용하여 현재 문서를 처음 문서로 이동하는 방법을 보여줍니다.

#hostingforum.kr
php

// MongoDB 연결 설정

$mongo = new MongoDBDriverManager("mongodb://localhost:27017");

$db = $mongo->selectDatabase("mydb");

$collection = $db->mycollection;



// MongoDBBSONIterator 생성

$iterator = new MongoDBBSONIteratorIterator($collection->find());



// 순회

while ($iterator->valid()) {

    $document = $iterator->current();

    echo "ID: " . $document->_id . "
";

    echo "Name: " . $document->name . "
";

    $iterator->next();

}



// 처음 문서로 이동

if ($iterator->rewind()) {

    $document = $iterator->current();

    echo "ID: " . $document->_id . "
";

    echo "Name: " . $document->name . "
";

}



// MongoDB 연결 종료

$mongo->close();



MongoDBBSONIterator::key


`key` 메소드는 현재 문서의 키를 반환하는 메소드입니다. 이 메소드는 현재 문서가 없으면 `null`을 반환합니다.

예제


다음 예제는 MongoDBBSONIterator의 `key` 메소드를 사용하여 현재 문서의 키를 반환하는 방법을 보여줍니다.

#hostingforum.kr
php

// MongoDB 연결 설정

$mongo = new MongoDBDriverManager("mongodb://localhost:27017");

$db = $mongo->selectDatabase("mydb");

$collection = $db->mycollection;



// MongoDBBSONIterator 생성

$iterator = new MongoDBBSONIteratorIterator($collection->find());



// 순회

while ($iterator->valid()) {

    $document = $iterator->current();

    echo "ID: " . $document->_id . "
";

    echo "Name: " . $document->name . "
";

    $iterator->next();

}



// 현재 문서의 키

$document = $iterator->current();

echo "ID: " . $document->_id . "
";

echo "Name: " . $document->name . "
";

echo "Key: " . $iterator->key() . "
";



// MongoDB 연결 종료

$mongo->close();



MongoDBBSONIterator::currentKey


`currentKey` 메소드는 현재 문서의 키를 반환하는 메소드입니다. 이 메소드는 현재 문서가 없으면 `null`을 반환합니다.

예제


다음 예제는 MongoDBBSONIterator의 `currentKey` 메소드를 사용하여 현재 문서의 키를 반환하는 방법을 보여줍니다.

#hostingforum.kr
php

// MongoDB 연결 설정

$mongo = new MongoDBDriverManager("mongodb://localhost:27017");

$db = $mongo->selectDatabase("mydb");

$collection = $db->mycollection;



// MongoDBBSONIterator 생성

$iterator = new MongoDBBSONIteratorIterator($collection->find());



// 순회

while ($iterator->valid()) {

    $document = $iterator->current();

    echo "ID: " . $document->_id . "
";

    echo "Name: " . $document->name . "
";

    $iterator->next();

}



// 현재 문서의 키

$document = $iterator->current();

echo "ID: " . $document->_id . "
";

echo "Name: " . $document->name . "
";

echo "Current Key: " . $iterator->currentKey() . "
";



// MongoDB 연결 종료

$mongo->close();



MongoDBBSONIterator::getInfo


`getInfo` 메소드는 현재 문서의 정보를 반환하는 메소드입니다. 이 메소드는 현재 문서가 없으면 `null`을 반환합니다.

예제


다음 예제는 MongoDBBSONIterator의 `getInfo` 메소드를 사용하여 현재 문서의 정보를 반환하는 방법을 보여줍니다.

#hostingforum.kr
php

// MongoDB 연결 설정

$mongo = new MongoDBDriverManager("mongodb://localhost:27017");

$db = $mongo->selectDatabase("mydb");

$collection = $db->mycollection;



// MongoDBBSONIterator 생성

$iterator = new MongoDBBSONIteratorIterator($collection->find());



// 순회

while ($iterator->valid()) {

    $document = $iterator->current();

    echo "ID: " . $document->_id . "
";

    echo "Name: " . $document->name . "
";

    $iterator->next();

}



// 현재 문서의 정보

$document = $iterator->current();

echo "ID: " . $document->_id . "
";

echo "Name: " . $document->name . "
";

echo "Info: " . $iterator->getInfo() . "
";



// MongoDB 연결 종료

$mongo->close();



MongoDBBSONIterator::getIterator


`getIterator` 메소드는 현재 문서의 이터레이터를 반환하는 메소드입니다. 이 메소드는 현재 문서가 없으면 `null`을 반환합니다.

예제


다음 예제는 MongoDBBSONIterator의 `getIterator` 메소드를 사용하여 현재 문서의 이터레이터를 반환하는 방법을 보여줍니다.

#hostingforum.kr
php

// MongoDB 연결 설정

$mongo = new MongoDBDriverManager("mongodb://localhost:27017");

$db = $mongo->selectDatabase("mydb");

$collection = $db->mycollection;



// MongoDBBSONIterator 생성

$iterator = new MongoDBBSONIteratorIterator($collection->find());



// 순회

while ($iterator->valid()) {

    $document = $iterator->current();

    echo "ID: " . $document->_id . "
";

    echo "Name: " . $document->name . "
";

    $iterator->next();

}



// 현재 문서의 이터레이터

$document = $iterator->current();

echo "ID: " . $document->_id . "
";

echo "Name: " . $document->name . "
";

echo "Iterator: " . $iterator->getIterator() . "
";



// MongoDB 연결 종료

$mongo->close();



MongoDBBSONIterator::getIteratorAsArray


`getIteratorAsArray` 메소드는 현재 문서의 이터레이터를 배열로 반환하는 메소드입니다. 이 메소드는 현재 문서가 없으면 `null`을 반환합니다.

예제


다음 예제는 MongoDBBSONIterator의 `getIteratorAsArray` 메소드를 사용하여 현재 문서의 이터레이터를 배열로 반환하는 방법을 보여줍니다.

#hostingforum.kr
php

// MongoDB 연결 설정

$mongo = new MongoDBDriverManager("mongodb://localhost:27017");

$db = $mongo->selectDatabase("mydb");

$collection = $db->mycollection;



// MongoDBBSONIterator 생성

$iterator = new MongoDBBSONIteratorIterator($collection->find());



// 순회

while ($iterator->valid()) {

    $document = $iterator->current();

    echo "ID: " . $document->_id . "
";

    echo "Name: " . $document->name . "
";

    $iterator->next();

}



// 현재 문서의 이터레이터

$document = $iterator->current();

echo "ID: " . $document->_id . "
";

echo "Name: " . $document->name . "
";

echo "Iterator As Array: " . $iterator->getIteratorAsArray() . "
";



// MongoDB 연결 종료

$mongo->close();


  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 1 페이지

검색

게시물 검색