Hướng dẫn mongodb constraints foreign key
How do I design a scheme such this in MongoDB? I think there are no foreign keys! asked Jun 13, 2011 at 17:40 Mark PegasovMark Pegasov 4,9599 gold badges25 silver badges30 bronze badges 4
First, to clarify some naming conventions. MongoDB uses
Take the following model:
Clearly Jane's course list points to some specific courses. The database does not apply any constraints to the system (i.e.: foreign key constraints), so there are no "cascading deletes" or "cascading updates". However, the database does contain the correct information. In addition, MongoDB has a DBRef standard that helps standardize the creation of these references. In fact, if you take a look at that link, it has a similar example.
To be clear, MongoDB is not relational. There is no standard "normal form". You should model your database appropriate to the data you store and the queries you intend to run. Frank Schmitt 29.3k11 gold badges69 silver badges105 bronze badges answered Jun 13, 2011 at 18:03 3 You may be interested in using a ORM like Mongoid or MongoMapper. http://mongoid.org/docs/relations/referenced/1-n.html In a NoSQL database like MongoDB there are not 'tables' but collections. Documents are grouped inside Collections. You can have any kind of document – with any kind of data – in a single collection. Basically, in a NoSQL database it is up to you to decide how to organise the data and its relations, if there are any. What Mongoid and MongoMapper do is to provide you with convenient methods to set up relations quite easily. Check out the link I gave you and ask any thing. Edit: In mongoid you will write your scheme like this:
Edit:
You can use that ObjectId in order to do relations between documents. renno 2,5491 gold badge23 silver badges51 bronze badges answered Jun 13, 2011 at 17:46 NerianNerian 15.6k12 gold badges65 silver badges94 bronze badges 7 We can define the so-called
The If the course More: MongoDB Schema DesignThe document-typed nature of MongoDB supports flexible ways to define relationships. To define a one-to-many relationship: Embedded document
Example:
Child referencingLike the Parent referencingSuitable for one-to-squillions, such as log messages.
Virtually, a References:
answered May 8, 2017 at 11:05 JoyJoy 9,24811 gold badges41 silver badges93 bronze badges 1 From The Little MongoDB Book
So,
If its a RESTful API data, replace the course id with a GET link to the course resource Ben 52.3k48 gold badges171 silver badges217 bronze badges answered Sep 5, 2013 at 13:23 ZAkyZAky 1,0847 silver badges21 bronze badges 2 Short answer: You should to use "weak references" between collections, using ObjectId properties:
https://docs.mongodb.com/manual/core/data-modeling-introduction/#references This will of course not check any referential integrity. You need to handle "dead links" on your side (application level). answered Jan 26, 2021 at 15:44 EfrainEfrain 3,0982 gold badges32 silver badges58 bronze badges The purpose of ForeignKey is to prevent the creation of data if the field value does not match its ForeignKey. To accomplish this in MongoDB, we use Schema middlewares that ensure the data consistency. Please have a look at the documentation. https://mongoosejs.com/docs/middleware.html#pre answered Jul 1, 2020 at 18:35 I see no reason why you cannot use a NOSQL db as a relational DB since you have aggregate pipelines which you can use to create views like SQL inner joins. Each collection should contain one type e.g. order and order-details is a 1:M relation. ORDER
COLLECTION ORDER-DETAILS COLLECTION PRODUCT And so on. The reason why you would not simply use a relational database is because a NOSQL database has other features you could use in tandem with relationally designed data collections. The way you implement the relations is the same as in relational databases. You have to add the foreign keys yourself (primary keys are added automatically for each document object) and create the normalized join collections. A database will not normalize itself usually. The only caveat is that you have to code referential integrity yourself as NOSQL does not do that (yet). So in your admin UI if a user tries to delete a parent that has children you throw a friendly error saying they have to delete the children first. It is not hard but you still need to be careful when using DB utilities not to delete a parent that has children because the DB will let you. Always use the naming conventions for foreign keys id. For example: ORDER-DETAIL COLLECTION answered Aug 29, 2021 at 8:26 2 |