MongoDB\ChangeStream::key()
定义
Return Values
当前事件在变更流中的索引,如果没有当前事件null
即 MongoDB\ChangeStream::valid()
返回false
)。
示例
此示例在迭代变更流时报告事件的索引。
$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']); }
假设在上述脚本迭代change stream时插入、更新和删除了文档,则输出将类似于以下内容:
0: insert 1: update 2: delete