How to remove documents from multiple collections in one query in MongoDB?

Viewed 88

It‘s an old question from How to remove documents from multiple collections based on ID in MongoDB? 2 years old.

If there is a solution?

My scenario is :

Collection A:
_id,
name
Collection B:
_id,
name,
aId,
Collection C:
_id,
name,
bId,
A vs mutiple-B
B vs mutiple-C

I want if there is a single query to delete docs by aId: XXX. such as:

db.B.find({"aID":XXX}).foreach(
   function(b) {
     db.B.find({"_id":b._id}).foreach(
        function(c) {
           db.C.remove(c._id)
         }
     db.B.remove(b._id)
   }
);

db.A.remove("_id":XXX)

But it is two querys, Is there a better way?

0 Answers
Related