MongoDB offers two different ways of storing data in collections: denormalization and normalization. For example, MongoDB uses collections, objects, and fields instead of using tables, columns, and rows. How MongoDB stores data is also different. However, relational databases can use foreign keys to visualize data from multiple collections simultaneously. Typically, MongoDB doesn’t work with the concept of foreign keys. We can also set null, which means that when data in the country database is deleted, the cities linked are set to null. For example, we can choose to cascade, which means that if a country is eliminated, so are all of the cities linked with it.
When creating foreign keys, we can specify what should happen if data is modified or destroyed.
This will allow you to search for all of the cities in Pakistan with a single query.Įvent Management System Project in Node JS with Source Code (Free Download) If we read the data from the city table in the example above, we can see that each city has an associated country_id. We want to link these two tables together such that each city is associated with the appropriate country.Ī foreign key can be used to accomplish this: Take, for example, two tables: country and city. It comprises a primary key column in the parent table linked to other columns in the child tables. Foreign KeyĪ foreign key is a column or a set of columns that link two tables. But, overall, the database is easier to scale because it doesn’t impose many restrictions. MongoDB, unlike traditional RDBMS, necessitates a distinct approach to database design. Instead, it uses JSON-like files to hold data and does not impose any structure. MongoDB is a well-known database that works without a schema.