1

I am able to add first degree relation to one collection to another but not able to add DBRefs if the collection has any internal structure (document).

For an example, I want to link tour_id in suggested_place internal structure to o_id of collection tour.

Please see the two image for my mongodb schema design.

First Collection Second Collection

To be honest, I am not very sure, it is possible or not and I have searched all over the Internet for any possibles answers. In case if there is no feature to add these kind of structure definition in mongodb, I am afraid that the junk data will remain there if the parent row is deleted.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Rohit Kumar
  • 877
  • 6
  • 20
  • Collection `tour` doesn't have `o_id` field on your schema. – Sergio Tulentsev Apr 17 '17 at 14:14
  • It is not showing in schema but mongodb generates it for every collection. I am able to see it when I am adding the DBRefs, the only problem is, I am not able to add it for internal document. – Rohit Kumar Apr 17 '17 at 14:15
  • Well, this might be a problem with your modelling tool. And btw, even if you could add a DBRef field that way, it wouldn't help in any way with clearing dependent records. – Sergio Tulentsev Apr 17 '17 at 14:20
  • The modeling tool I am using has a feature for setting cascading operation but for collection to collections only. – Rohit Kumar Apr 17 '17 at 14:21
  • "has a feature for setting cascading operation" - yeah, good luck making it work in mongodb :) – Sergio Tulentsev Apr 17 '17 at 14:25

1 Answers1

0

I am afraid that the junk data will remain there if the parent row is deleted

It will, for sure. Unless you take care of it yourself (remove all children when you delete a parent). Because mongodb is not a relational database and, as such, doesn't have any kind of relations or referential integrity or cascade deletes.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • We can surely delete the child documents when deleting a parent row by programming. So there isn't any feature in mongodb to link 1st degree collections to 2nd degree documents? – Rohit Kumar Apr 17 '17 at 14:19
  • @RohitKumar: link directly into an embedded document? Of course not. But I thought you want to do the opposite: refer to top-level `tour` from an embedded `suggested_place`? – Sergio Tulentsev Apr 17 '17 at 14:20
  • Either way, it's not letting me because it is only showing collection to collection. – Rohit Kumar Apr 17 '17 at 14:23
  • @RohitKumar: yeah well, get another tool – Sergio Tulentsev Apr 17 '17 at 14:26
  • @RohitKumar: why are you even using a formal tool? Pretty charts? You can draw them in visio or omnigraffle or whatever, which won't restrict you in what entities you can link. – Sergio Tulentsev Apr 17 '17 at 14:27
  • Can you suggest one in that case? I'm using http://www.dbschema.com/mongodb-tool.html – Rohit Kumar Apr 17 '17 at 14:27
  • Seems like you are suggesting just to draw the lines. I am actually looking for real DBRefs. The images are just for references. – Rohit Kumar Apr 17 '17 at 14:31
  • @RohitKumar What do you mean, "real DBRefs"? – Sergio Tulentsev Apr 17 '17 at 14:34
  • Meaning, just not drawing the lines in model but the DBRefs should be added in mongodb too. – Rohit Kumar Apr 17 '17 at 14:37
  • @RohitKumar: what do you think a DBRef in mongodb is? It's not a relation object (like in RBMS). It's just a field. No guarantees, no functionality. And it can't even be added to a schema, because mongodb doesn't have one. So I'm quite confused as to what you're trying to achieve there in that tool – Sergio Tulentsev Apr 17 '17 at 14:40