using database relationship in mongodb

MongoDB Relationships are the representation of how multiple documents are logically connected to each other in MongoDB. The Embedded and Referenced methods are two ways to create such relationships. Embedded Relationships and Document Referenced Relationships are two types of relationships that have different ways of working with documents. While Embedded is ideal for one-to-one and one-to-many relationships, Referenced is ideal for many-to-many.

Embedded Document Model:

The documents are embedded within one another in this model. For instance, we have two documents: one is a student document (which contains the student’s basic information such as id and branch) and the other is an address document (which contains the address of the student). As a result, rather than creating two separate documents, the address documents are embedded within the student document. It will assist a user in retrieving data with a single query rather than a series of queries.

Reference Model:

We keep the documents separate in this model, but one document contains the references to the others. For instance, we have two documents: one is a student document (which contains the student’s basic information such as id and branch) and the other is an address document (which contains the address of the student). As a result, the id field of the address document is referenced in the student document. We can now query the address and get the student’s address using this reference id. The normalized relationships are usually designed using this model.

A One-to-One relationship

The most basic of all relationships is the one-to-one relationship. If we have a one-parent and one-child document, it’s an example of a one-to-one relationship.

Example:

Consider the following scenario: we have two documents. The first document contains the student’s id name and branch, while the second document contains the student’s permanent address information. If the address data is frequently used, the user creates a query using StudentName to retrieve the data from the address document.

However, because two documents contain the same field (i.e., StudentName), the user must write a few more queries to retrieve the required information. This data retrieval procedure is time-consuming. As a result, the address document was embedded in the student document.

To retrieve the required data, we now only need to write a single query. The benefit of using embedded documents is that we can group the necessary information into a single document. As a result, getting the details in one call is easier. However, as the document expands in size, for example, by adding academic and athletic information to the above document, it will become longer and take longer to retrieve the information. We may need academic or athletic information only when it is required, and in those cases, we may need to break up the document and use a subset pattern.

A One-to-Many relationship

A single-parent document that has multiple child documents is an example of a one-to-many relationship. It is similar to one-to-one but with a large number of ‘children’ documents. Using the embedded method to establish a relationship can reduce the number of reading operations required to retrieve data.

One-to-Many Relationships with Embedded Documents

Example:

We can create one-to-many relationships between data using embedded documents, allowing us to retrieve data quickly with few read operations. With the help of an example, we will now discuss the one-to-many relationship with embedded documents.

A person may have multiple addresses, such as a current address (the location where he or she currently resides) and a residential address (the location where he or she owns a home or has a permanent address). One-to-many relationship possibilities occur during that time. As a result, we can store both permanent and current addresses in a single document using an embedded document model.

Instead of writing three documents, we can now combine them into one.

Instead of writing three documents, we can now combine them into one.

One of the primary benefits of creating Embedded Relationships in MongoDB is that the queries are executed faster than the referenced relationship. This relationship also improves performance, and results are obtained quickly. This is also true for large datasets.

One-to-many relationships with the document reference

The document reference model can also be used to create a one-to-many relationship. We keep the documents separate in this model, but one document contains the references to the others.

Example:

With the help of an example, we will now discuss the one-to-many relationship with document reference. Assume that we have a teacher who teaches in two different classes. And she is in possession of three documents:

Data retrieval from these various documents is now time-consuming. As a result, we’ll use the reference model to assist the teacher in retrieving data with a single query.

The data for classes 1 and 2 can now be easily retrieved using these classIds fields.

Here, we are working with the database knowledgehut and now, we will use a new collection teacher.

To insert more than one document, we will use insertMany() method.

A Many-to-Many relationship

As there is no single command to implement a many-to-many relationship in a relational database, it is more difficult than a one-to-many relationship. The same is true when using mongoDB to implement them. In fact, you can’t use a command to create any type of relationship in MongoDB.

Having the ability to store arrays in a document, on the other hand, allows you to store data in a way that is easy to retrieve and maintain, as well as providing you with the information you need to link two documents in your code.

Many to Many relationships are a type of mongodb relationship in which any two entities within a document can have multiple relationships.

In this relationship, we can consider a case of Online courses website where there are many courses and also many users. Each course is purchased by many users and each user may purchase many courses.

Example:

For this we will create a database knowledgehut and two different collections of users and courses.