文档菜单
文档首页
/ / /
PHP 库手册
/ / /

MongoDB\ChangeStream::key()

本页内容

  • 定义
  • 返回值
  • 示例
  • 另请参阅
MongoDB\ChangeStream::key()

返回当前事件在更改流中的索引。

function key(): integer|null

更改流中第一个事件的索引从零开始,对于每个后续事件递增一。

更改流中当前事件的索引,或者如果当前没有事件(即MongoDB\ChangeStream::valid() 返回 false)则为 null

此示例在迭代更改流时报告事件索引。

<?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();
printf("%d: %s\n", $changeStream->key(), $event['operationType']);
}

假设在上述脚本迭代更改流期间,文档被插入、更新和删除,则输出将类似于

0: insert
1: update
2: delete
  • MongoDB\Client::watch()

  • MongoDB\Collection::watch()

  • MongoDB\Database::watch()

  • Iterator::key()

  • 可滚动游标迭代

  • 更改流的MongoDB手册文档

返回

getResumeToken()