为了在删除需要启用CollMod的文档后,将完整的文档在收藏中启用。
1.下载蒙古山https://www.mongodb.com/try/download/shell
2.运行蒙古什尔(Mongoshell)并使用Connection String与Admin用户连接,如果您需要
,请创建临时管理员 3.在Mongoshell中运行以下命令 - 更改您的收藏的“ CollMod”
db.runCommand({collMod: "ProductReview", changeStreamPreAndPostImages: {enabled: true}})
4.在您的变更流中,您可以像这样访问已删除的文档(以下代码为nodejs)
const client = new MongoClient(env.DATABASE_URL);
const database = client.db("my_database");
const reviews = database.collection("ProductReview");
// 'fullDocumentBeforeChange' is the magic here
const changeStream = reviews.watch([], {
fullDocument: "updateLookup",
fullDocumentBeforeChange: "required"
});
changeStream.on("change", async (event) => {
let document;
if (event.operationType === 'delete') {
document = event.fullDocumentBeforeChange;
} else {
document = event.fullDocument;
}
// do some stuff with the document
});
文档:
https://www.mongodb.com/docs/v6.0/reference/operator/aggregation/changeStream/
相关问题:
- https://jira.mongodb.org/browse/SERVER-36941
- https://jira.mongodb.org/browse/SERVER-56074
- https://stackoverflow.com/questions/56939610/how-to-get-fulldocument-from-mongodb-changestream-when-a-document-is-deleted
- https://www.mongodb.com/community/forums/t/change-stream-fulldocument-on-delete/15963
- https://www.mongodb.com/try/download/shell