라이브러리

[PHP] MongoDBDriverWriteResult::getUpsertedIds - 업서트된 문서에 대한 식별자 배열을 반환합니다.




MongoDBDriverWriteResult::getUpsertedIds


MongoDBDriverWriteResult::getUpsertedIds는 MongoDB의 업서트(UPSERT) 연산의 결과에서 업서트된 아이디를 반환하는 메서드입니다. 업서트 연산은 문서가 이미 존재하는 경우 문서를 업데이트하고, 존재하지 않는 경우 문서를 삽입하는 연산입니다.

사용법


MongoDBDriverWriteResult::getUpsertedIds 메서드는 다음과 같이 사용할 수 있습니다.

#hostingforum.kr
php

use MongoDBDriverWriteResult;

use MongoDBDriverBulkWrite;



// MongoDB 클라이언트를 생성합니다.

$client = new MongoDBClient('mongodb://localhost:27017');



// 데이터베이스와 컬렉션을 선택합니다.

$db = $client->test;

$collection = $db->mycollection;



// 업서트 연산을 수행합니다.

$bulk = new BulkWrite;

$bulk->insertOne(['name' => 'John', 'age' => 30]);

$bulk->updateOne(['name' => 'John'], ['$set' => ['age' => 31]]);



$writeResult = $collection->executeBulkWrite($bulk);



// 업서트된 아이디를 가져옵니다.

$upsertedIds = $writeResult->getUpsertedIds();



// 업서트된 아이디를 출력합니다.

foreach ($upsertedIds as $id) {

    echo "업서트된 아이디: " . $id . "
";

}



예제


위의 예제에서, `executeBulkWrite` 메서드를 호출하여 업서트 연산을 수행한 후, `getUpsertedIds` 메서드를 호출하여 업서트된 아이디를 가져옵니다. 업서트된 아이디는 업서트 연산이 수행된 문서의 `_id` 필드의 값입니다.

참고


* MongoDBDriverWriteResult::getUpsertedIds 메서드는 업서트 연산이 수행된 문서의 `_id` 필드의 값만 반환합니다.
* 업서트 연산이 수행된 문서의 다른 필드는 반환되지 않습니다.
* 업서트 연산이 수행되지 않은 경우, `getUpsertedIds` 메서드는 빈 배열을 반환합니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색