Firstly, I have looked at these "solutions":
Cascade style delete in Mongoose
How to cascade delete in mongodb?
How to cascade delete document in mongodb?
What is the recommended equivalent of cascaded delete in MongoDB for N:M relationships?
But I still can't figure out how to do a cascade-style deletion. All the previous questions are using 2 separate collections, but in my case, everything is in a single collection.
So I have a schema that looks like this:
{
"_id": "fafa76b0-40ef-4f38-af64-35992ceed119",
"user": {
"timestamp": "2022-10-22T13:53:43.042187",
"name": "john2",
"surname": "doe2",
"email": "abcde1234@gmail.com",
"phone": "+012345678987",
"age": 18,
"gender": "male",
"nationality": "smth",
"universityMajor": null,
"preferences": null,
"password": "e9cee71ab932fde863338d08be4de9dfe39ea049bdafb342ce659ec5450b69ae"
},
"postings": [
{
"id": "55412a9a-8efb-4213-90a4-efd693937f4a",
"timestamp": "2022-10-22T13:53:43.043187",
"title": "new posting 3",
"totalNumOfRoommates": 2
},
{
"id": "c3840cb9-4066-47ee-84db-404970c2e067",
"timestamp": "2022-10-22T13:53:43.043187",
"title": "new posting 4",
"totalNumOfRoommates": 20
},
{
"id": "e76d4dbd-02cb-41ca-a3f4-5204bf94611d",
"timestamp": "2022-10-24",
"title": "test Update4",
"totalNumOfRoommates": 15,
"price": 2500
}
],
"starredPostings": [
"7d31d7d3-28ac-47b7-91bb-bd9a1d5954fe"
]
}
The postings array holds posting objects, each object has its own unique ID.
Then there's starredPostings which simply stores the ID of a posting object in another document.
What I want is that when I delete a document, let's call it document X, I want the IDs of all of the postings in document X to be removed from starredPostings in other documents
P.S: I am using FastAPI and Pymongo