9

I have created a SQL DB and examined the integrity. Now I wanted to put these tables in mongoDB, and I've kept it in the mapping rules. Table = collection, row = doc, and so on.

But how does one set about following in mongoDB:

create table pruefen 
( MatrNr integer references Studenten on delete cascade,
  VorlNr integer references Vorlesungen,  
  PersNr integer references Professoren on delete set null,  
  Note numeric(2,1) check (Note between 0.7 and 5.0),   
  primary key (MatrNr, VorlNr));

DBRef, I've tried but is not a foreign key replacement.

And if the application is to take over as it would look then?
viv1d
  • 339
  • 4
  • 5
  • 12

2 Answers2

29

MongoDB has no cascading deletes. When your application deletes data, it is also responsible for removing any referenced objects itself and any references to the deleted document. But usually when you use on delete in a relational database, you have a case of composition where one parent object owns one or more child objects, and the child objects are meaningless without the parent. In that situation, MongoDB encourages embedding instead of referencing. That means that you create an array in the parent object, and put the complete child documents into that array instead of keeping them in an own collection. That way they will be deleted together with the parent, because they are a part of it.

While keeping more than one value in a field is an absolute no-go in SQL, there is nothing wrong with that in MongoDB. That's because the MongoDB query language can easily work with arrays and embedded objects. You can even create indices on fields of sub-documents in arrays, so you can easily search for objects which are embedded in other objects.

When you still want to reference objects from another collection, you can either use a DBRef, or you can also use any other unique identifier (uniqueness is one of the few things which can be enforced by MongoDB. To do so, create an unique index with the createIndex command). But MongoDB does not enforce consistency in this case. You can create DBRefs which point to non-existing ObjectIds and when the document the DBRef points to is deleted, nothing will happen. The application is responsible for making sure that when it deletes a document, all documents which reference it are updated.

Constraints can not be enforced by MongoDB either. It can't even enforce a specific type for a field, due to the schemaless nature of MongoDB. Again, your application is responsible for making sure that the data it puts into mongodb is following specific specifications. When you want to automatize this, there are object-relational mapping frameworks for MongoDB for many programming languages available.

To wrap it all up: MongoDB is not as "smart" as SQL databases. It doesn't do much on its own. It does what it is told to do by the application, not more and not less. But that's the reason why it's so fast (no expensive consistency checks) and flexible (no database modifications necessary to implement new features).

Philipp
  • 67,764
  • 9
  • 118
  • 153
  • 1
    "MongoDB encourages embedding instead of referencing" can you provide link? – Eddie Jamsession Oct 15 '13 at 12:34
  • 1
    Shouldn't it be faster to enforce constraints in db level than in application level? Doesn't it mean that MongoDB is ultimately slower because of this? Not to speak about how well will those application level constraints get implemented... – Tamas Hegedus May 07 '19 at 13:59
  • @TamasHegedus Use the right tool for the job. If you are using MongoDB but try to emulate every feature you have in a SQL database, then it will of course be slower, because you are not using it for the use-cases it was designed for. – Philipp May 07 '19 at 14:52
8

One of the great things about relational database is that it is really good at keeping the data consistent within the database. One of the ways it does that is by using foreign keys. A foreign key constraint is that let's say there's a table with some column which will have a foreign key column with values from another table's column. In MongoDB, there's no guarantee that foreign keys will be preserved. It's upto the programmer to make sure that the data is consistent in that manner. This maybe possible in future versions of MongoDB but today, there's no such option. The alternative for foreign key constraints is embedding data.

Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
  • 4
    Embeddig data is only an alternative if the parent document owns the embedded one. There are cases when documents have their own independent lifecycle but still need to be referenced from other documents. If I understand correctly there is just no way of preserving consistency in that case, leading to inevitable data degradation over time. For the worst, people will try to find workarounds, wasting time all over the world in parallel to solve a fundamentally unsolvable problem. – Tamas Hegedus May 07 '19 at 14:04