使用MeteorJS从MongoDB中的多个集合中删除文档 [英] Removing document from multiple collections in MongoDB with MeteorJS

查看:71
本文介绍了使用MeteorJS从MongoDB中的多个集合中删除文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5个馆藏-A,B,C,D,E。我有一个文档存储在A中,并且与其他4个馆藏中的文档有关。我正在使用MeteorJS。现在,我必须安全删除文档中的数据。换句话说,必须在A中删除该文档,并在其他文档中删除其引用。只有将它们全部删除后,我才必须向用户显示成功消息。现在,我只有:

I have 5 collections - A, B, C, D, E. I have a document which is stored in A, and related to documents in the other 4 collections. I am using MeteorJS. Now, I have to delete the data in the document safely. In other words, the document has to be deleted in A, as well as its references in the others. Only after they are all removed, I have to show the user a success message. Right now, I have only:

A.remove(documentID);

我必须在此处使用回调向用户显示结果。我对MongoDB完全陌生,并且了解无法像SQL那样在这里级联删除行。我不知道如何编写代码以从所有集合中删除文档及其引用(全部由 documentID 引用)并将结果显示给用户。有人可以帮忙吗?非常感谢!

I have to use a callback here to show the user the result. I am completely new to MongoDB and understand that cascade deletion of a row is not possible here as in SQL. I don't know how to write the code which will remove the document and its references (referenced by documentID in all) from all the collections and show the result to the user. Can somebody help please? Thanks a lot!

推荐答案

您正在寻找集合挂钩: https://github.com/matb33/meteor-collection-hooks

You're looking for collection hooks: https://github.com/matb33/meteor-collection-hooks

集合挂钩允许您在对集合进行插入/更新/删除之前/之后执行代码。使用您的示例,您可以创建一个类似于以下内容的收集钩子:

Collection hooks allow you to execute code before/after insert/updates/deletes on collections. Using your example, you could create a collection hook that looks something like:

A.after.remove(function(userId, doc) {
    B.remove({AReference: doc._id});
    C.remove({AReference: doc._id});
    D.remove({AReference: doc._id});
    E.remove({AReference: doc._id});
});

AReference必须是您相关字段的任何内容。

AReference will need to be whatever your related field is.

这篇关于使用MeteorJS从MongoDB中的多个集合中删除文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆