Para agentes de IA: hay un índice de documentación disponible en https://www.mongodb.com/es/docs/llms.txt — versiones en markdown de todas las páginas están disponibles agregando .md a cualquier ruta URL.

MongoDB\ChangeStream::current()

MongoDB\ChangeStream::current()

Devuelve el evento actual en el flujo de cambios.

function current(): array|object|null

La estructura de cada documento de evento variará en función del tipo de operación. Consulta Eventos de cambio en el manual de MongoDB para obtener más información.

Un arreglo u objeto para el evento actual en el flujo de cambios, o null si no hay ningún evento actual (es decir, MongoDB\ChangeStream::valid() devuelve false). El tipo de retorno dependerá de la opción typeMap para MongoDB\Collection::watch().

Este ejemplo informa sobre eventos mientras recorre un flujo de cambios.

<?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; }}

Suponiendo que se insertara, actualizara y eliminara un documento mientras el script anterior estuviese iterando el flujo de cambios, la salida se parecería entonces a:

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"}