Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoDB alternatives for foreign key constraints

Tags:

mongodb

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?
like image 437
viv1d Avatar asked Jan 11 '13 14:01

viv1d


People also ask

Does MongoDB support foreign key constraints?

No. MongoDB doesn't support primary key-foreign key relationship. MongoDB has _id key field for every document that uniquely identifies the particular document.

What is the good alternative to MongoDB?

Some of the best MongoDB alternatives for 2022 include Redis, Apache Cassandra, RethinkDB, DynamoDB, OrientDB, CouchDB, and ArangoDB.

Is there foreign key in NoSQL?

NoSQL databases do not support foreign keys or joins and do not have the concept of referential integrity. Let's explore different kinds of NoSQL databases and when it's appropriate to use each.

How can avoid foreign key constraint in SQL?

Use SQL Server Management StudioIn Object Explorer, expand the table with the constraint and then expand the Keys folder. Right-click the constraint and select Modify. In the grid under Table Designer, select Enforce Foreign Key Constraint and select No from the drop-down menu. Select Close.


1 Answers

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).

like image 106
Philipp Avatar answered Nov 09 '22 02:11

Philipp