fullDocument tham số cho đối số tùy chọn (thứ hai) cho watch phương pháp có thể được sử dụng để lấy một delta mô tả các thay đổi đối với tài liệu cho update hoạt động:
const thoughtChangeStream = connection.collection("phonenumbers").watch([], {
fullDocument: 'updateLookup'
});
thoughtChangeStream.on("change", (change) => {
io.of("/api/socket").emit("newThought", change);
});
Sau đó, điều này sẽ trả về một tài liệu phản hồi như thế này trong đó updateDescription chứa các trường đã được sửa đổi bởi bản cập nhật:
{
_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: []
}
}
Lưu ý:Điều này sẽ chỉ hoạt động đối với update và sẽ không hoạt động đối với replace , delete , insert , v.v.
Xem thêm:
- https://mongodb.github.io/ node-mongodb-native / 3.0 / api / Collection.html .
- https://docs.mongodb.com/manual/reference/change -sự kiện /