라이브러리

[PHP] MongoDBDriverWriteResult::getDeletedCount - 삭제된 문서 수를 반환합니다.




MongoDBDriverWriteResult::getDeletedCount


`MongoDBDriverWriteResult::getDeletedCount` 메소드는 MongoDB의 `updateOne` 또는 `deleteOne` 메소드의 결과에서 삭제된 문서의 수를 반환합니다. 이 메소드는 `MongoDBDriverWriteResult` 객체의 속성 중 하나입니다.

사용법


`getDeletedCount` 메소드는 `MongoDBDriverWriteResult` 객체의 속성 중 하나이므로, 이 객체를 반환받은 후에 사용할 수 있습니다. 예를 들어, `updateOne` 메소드의 결과에서 삭제된 문서의 수를 얻으려면 다음과 같이 사용할 수 있습니다.

#hostingforum.kr
php

use MongoDBClient;

use MongoDBDriverBulkWrite;

use MongoDBDriverBulkWriteException;

use MongoDBDriverWriteResult;



$client = new Client();

$db = $client->selectDatabase('mydb');

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



$bulk = new BulkWrite();

$bulk->deleteOne(['_id' => new MongoDBBSONObjectId('...')]);



try {

    $result = $collection->executeBulkWrite('mycollection', $bulk);

    echo $result->getDeletedCount(); // 삭제된 문서의 수를 출력합니다.

} catch (BulkWriteException $e) {

    echo 'Bulk write error: ' . $e->getMessage();

}



예제


다음 예제는 `updateOne` 메소드를 사용하여 문서를 업데이트하고, 업데이트된 문서의 수를 얻는 방법을 보여줍니다.

#hostingforum.kr
php

use MongoDBClient;

use MongoDBDriverBulkWrite;

use MongoDBDriverBulkWriteException;

use MongoDBDriverWriteResult;



$client = new Client();

$db = $client->selectDatabase('mydb');

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



$bulk = new BulkWrite();

$bulk->updateOne(['_id' => new MongoDBBSONObjectId('...')], ['$set' => ['name' => 'John']]);



try {

    $result = $collection->executeBulkWrite('mycollection', $bulk);

    echo $result->getModifiedCount(); // 업데이트된 문서의 수를 출력합니다.

    echo $result->getMatchedCount(); // 매칭된 문서의 수를 출력합니다.

    echo $result->getUpsertedCount(); // 삽입된 문서의 수를 출력합니다.

    echo $result->getInsertedCount(); // 삽입된 문서의 수를 출력합니다.

    echo $result->getDeletedCount(); // 삭제된 문서의 수를 출력합니다.

} catch (BulkWriteException $e) {

    echo 'Bulk write error: ' . $e->getMessage();

}



참고


`getDeletedCount` 메소드는 `updateOne` 또는 `deleteOne` 메소드의 결과에서 삭제된 문서의 수를 반환합니다. 이 메소드는 `MongoDBDriverWriteResult` 객체의 속성 중 하나이므로, 이 객체를 반환받은 후에 사용할 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

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

검색

게시물 검색