문서 메뉴
문서 홈
/ / /
PHP 라이브러리 매뉴얼
/ / /

MongoDB\ChangeStream::current()

이 페이지의 내용

  • 정의
  • 반환 값
  • 예제
  • 다음도 참조하세요.
MongoDB\ChangeStream::current()

변경 스트림의 현재 이벤트를 반환합니다.

function current(): array|object|null

각 이벤트 문서의 구조는 작업 유형에 따라 달라집니다. 자세한 내용은 MongoDB 매뉴얼의 변경 이벤트 를 참조하세요.

변경 스트림에 있는 현재 이벤트에 대한 배열 또는 객체이거나, 현재 이벤트가 없는 경우 null 입니다(예: MongoDB\ChangeStream::valid()false 를 반환). 반환 유형은MongoDB\Collection::watch()typeMap 옵션에 따라 달라집니다

이 예시에서는 change stream을 반복하는 동안 발생하는 이벤트를 보고합니다.

<?php
$uri = 'mongodb://rs1.example.com,rs2.example.com/?replicaSet=myReplicaSet';
$collection = (new MongoDB\Client($uri))->test->inventory;
$changeStream = $collection->watch();
for ($changeStream->rewind(); true; $changeStream->next()) {
if ( ! $changeStream->valid()) {
continue;
}
$event = $changeStream->current();
$ns = sprintf('%s.%s', $event['ns']['db'], $event['ns']['coll']);
$id = json_encode($event['documentKey']['_id']);
switch ($event['operationType']) {
case 'delete':
printf("Deleted document in %s with _id: %s\n\n", $ns, $id);
break;
case 'insert':
printf("Inserted new document in %s\n", $ns);
echo json_encode($event['fullDocument']), "\n\n";
break;
case 'replace':
printf("Replaced new document in %s with _id: %s\n", $ns, $id);
echo json_encode($event['fullDocument']), "\n\n";
break;
case 'update':
printf("Updated document in %s with _id: %s\n", $ns, $id);
echo json_encode($event['updateDescription']), "\n\n";
break;
}
}

위의 스크립트가 change stream을 반복하는 동안 문서가 삽입, 업데이트 및 삭제되었다고 가정하면 출력은 다음과 유사합니다.

Inserted new document in test.inventory
{"_id":{"$oid":"5a81fc0d6118fd1af1790d32"},"name":"Widget","quantity":5}
Updated document in test.inventory with _id: {"$oid":"5a81fc0d6118fd1af1790d32"}
{"updatedFields":{"quantity":4},"removedFields":[]}
Deleted document in test.inventory with _id: {"$oid":"5a81fc0d6118fd1af1790d32"}

돌아가기

ChangeStream 클래스

다음

getCursorId()