Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB - How to Handle Relationship

Tags:

mongodb

I just start learning about nosql database, specially MongoDB (no specific reason for mongodb). I browse few tutorial sites, but still cant figure out, how it handle relationship between two documents/entity

Lets say for example: 1. One Employee works in one department 2. One Employee works in many department

I dont know the term 'relationship' make sense for mongodb or not.

Can somebody please give something about joins, relationship.

like image 561
Aniruddha Avatar asked Oct 02 '11 19:10

Aniruddha


People also ask

Can MongoDB handle relationships?

MongoDB is a document database and doesn't maintain relationships between documents like relational databases such as PostgreSQL. Still, MongoDB allows you to create relationships between documents. These relationships can either be modeled through embedded or referenced approaches.

How do relationships work in MongoDB?

Relationships in MongoDB represent how various documents are logically related to each other. Relationships can be modeled via Embedded and Referenced approaches. Such relationships can be either 1:1, 1:N, N:1 or N:N. Let us consider the case of storing addresses for users.

How does MongoDB manage relationship between documents?

In MongoDB, a relationship represents how different types of documents are logically related to each other. Relationships like one-to-one, one-to-many, etc., can be represented by using two different models: Embedded document model. Reference model.

How do you create a relationship between two collections in MongoDB?

For performing MongoDB Join two collections, you must use the $lookup operator. It is defined as a stage that executes a left outer join with another collection and aids in filtering data from joined documents. For example, if a user requires all grades from all students, then the below query can be written: Students.


1 Answers

The short answer: with "nosql" you wouldn't do it that way.

What you'd do instead of a join or a relationship is add the departments the user is in to the user object.

You could also add the user to a field in the "department" object, if you needed to see users from that direction.

Denormalized data like this is typical in a "nosql" database.

See this very closely related question: How do I perform the SQL Join equivalent in MongoDB?

like image 89
Will Chesterfield Avatar answered Oct 13 '22 10:10

Will Chesterfield