使用MeteorJS从MongoDB中的多个集合中删除文档

编程入门 行业动态 更新时间:2024-10-24 04:39:01
本文介绍了使用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!

推荐答案

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

You're looking for collection hooks: github/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中的多个集合中删除文档

本文发布于:2023-11-22 18:39:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618554.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   文档   MeteorJS   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!