El fullDocument
parámetro a las opciones (segundo) argumento al watch
El método se puede usar para obtener un delta que describa los cambios en el documento para update
operaciones:
const thoughtChangeStream = connection.collection("phonenumbers").watch([], {
fullDocument: 'updateLookup'
});
thoughtChangeStream.on("change", (change) => {
io.of("/api/socket").emit("newThought", change);
});
Esto devolverá un documento de respuesta como este donde updateDescription
contiene los campos que fueron modificados por la actualización:
{
_id: {
_data: '8260931772000000012B022C0100296E5A1004ABFC09CB5798444C8126B1DBABB9859946645F696400646082EA7F05B619F0D586DA440004'
},
operationType: 'update',
clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1620252530 },
ns: { db: 'yourDatabase', coll: 'yourCollection' },
documentKey: { _id: 6082ea7f05b619f0d586da44 },
updateDescription: {
updatedFields: { updatedField: 'newValue' },
removedFields: []
}
}
Nota:Esto solo funcionará para update
operaciones y no funcionará para replace
, delete
, insert
, etc.
Ver también:
- http://mongodb.github.io/ node-mongodb-native/3.0/api/Collection.html .
- https://docs.mongodb.com/manual/reference/change -eventos/