라이브러리

[PHP] MongoDBDriverCursorInterface::toArray - 이 커서에 대한 모든 결과를 포함하는 배열을 반환합니다.




MongoDBDriverCursorInterface::toArray

`MongoDBDriverCursorInterface::toArray` 메소드는 MongoDB 커서에서 데이터를 배열로 변환하는 메소드입니다. 커서에서 데이터를 가져오기 위해 이 메소드를 사용할 수 있습니다.

사용법


`toArray` 메소드는 다음과 같은 형식으로 사용할 수 있습니다.

#hostingforum.kr
php

$cursor->toArray();



예제


다음 예제는 `toArray` 메소드를 사용하여 MongoDB 커서에서 데이터를 가져오는 방법을 보여줍니다.

#hostingforum.kr
php

// MongoDB 연결

$m = new MongoDBClient("mongodb://localhost:27017");



// 데이터베이스 선택

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



// 컬렉션 선택

$collection = $db->selectCollection("mycollection");



// 데이터 삽입

$data = [

    ["name" => "John", "age" => 25],

    ["name" => "Jane", "age" => 30],

    ["name" => "Bob", "age" => 35]

];

$collection->insertMany($data);



// 데이터 가져오기

$cursor = $collection->find();



// toArray 메소드 사용

$dataArray = $cursor->toArray();



// 결과 출력

print_r($dataArray);



이 예제에서는 MongoDB 커서에서 데이터를 가져와 배열로 변환한 후, 결과를 출력합니다.

결과


#hostingforum.kr
php

Array

(

    [0] => MongoDBModelBSONDocument Object

        (

            [storageKey] => Array

                (

                    [name] => John

                    [age] => 25

                )



            [document] => MongoDBBSONUTCDateTime Object

                (

                    [milliseconds] => 1643723400

                )



        )



    [1] => MongoDBModelBSONDocument Object

        (

            [storageKey] => Array

                (

                    [name] => Jane

                    [age] => 30

                )



            [document] => MongoDBBSONUTCDateTime Object

                (

                    [milliseconds] => 1643723400

                )



        )



    [2] => MongoDBModelBSONDocument Object

        (

            [storageKey] => Array

                (

                    [name] => Bob

                    [age] => 35

                )



            [document] => MongoDBBSONUTCDateTime Object

                (

                    [milliseconds] => 1643723400

                )



        )



)



참고


`toArray` 메소드는 데이터를 가져올 때 모든 데이터를 메모리에 로드하므로, 큰 데이터 세트를 처리할 때 성능이 저하될 수 있습니다. 대신 `iterate` 메소드를 사용하여 데이터를 가져올 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색