MongoDB\ChangeStream::key()
定义
返回值
更改流中当前事件的索引,或者如果当前没有事件(即MongoDB\ChangeStream::valid()
返回 false
)则为 null
。
示例
此示例在迭代更改流时报告事件索引。
$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(); printf("%d: %s\n", $changeStream->key(), $event['operationType']); }
假设在上述脚本迭代更改流期间,文档被插入、更新和删除,则输出将类似于
0: insert 1: update 2: delete