Y
Y
YakutD2020-07-27 10:26:23
MongoDB
YakutD, 2020-07-27 10:26:23

How to properly store objects in properties?

This is my first time dealing with MongoDB (and NoSQL in general), and even more so with Monguz. There is a users collection, it has a pets field with the Array type, which in turn stores an array with objects from the pets collection. Is it possible to remove an object from pets and automatically remove the corresponding element in the pets field of the users collection?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-07-27
@YakutD

nope, mongo can't cascade delete entries,
you'll have to find and delete the corresponding element in the pets field of the users collection manually
BUT, mongoose has a remove hook that will be called when a document is removed from the pets collection - you can select users who have a pet ID each time you delete , and change something
https://mongoosejs.com/docs/api/model.html#model_M...
https://stackoverflow.com/questions/14348516/casca...
alternatively I suggest just deleting from pets and leave users as they are , and then, when fetching from the database, filter the array of animals for the presence of a document in pets (again, the history of animals that the user had in the past will come in handy later)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question