5

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.

Aniruddha
  • 3,513
  • 6
  • 27
  • 38

4 Answers4

8

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?

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Will Chesterfield
  • 1,780
  • 12
  • 15
1

in general, you want to denormalize your data in your collections (=tables). Your collections should be optimized so that you don't need to do joins (joins are not possible in NoSQL).

In MongoDB you can either reference other collections (=tables), or you can embed them into each other -- whatever makes more sense in your domain. There are size limits to entries in a collection, so you can't just embed the encyclopedia britannica ;-)

It's probably best if you look for API documentation and examples for the programming language of your choice. For Ruby, I'd recommend the Mondoid library: http://mongoid.org/docs/relations.html

Tilo
  • 33,354
  • 5
  • 79
  • 106
1

There are some instances in which you want/need to keep the documents separate in which case you would take the _id from the one object and add it as a value in your other object.

For Example:

db.authors
{
  _id:ObjectId(21EC2020-3AEA-1069-A2DD-08002B30309D)
  name:'George R.R. Martin'
}

db.books
{
  name:'A Dance with Dragons'
  authorId:ObjectId(21EC2020-3AEA-1069-A2DD-08002B30309D)
}

There is no official relationship between books and authors its just a copy of the _id from authors into the authorId value in books.

Hope that helps.

SomethingOn
  • 9,813
  • 17
  • 68
  • 107
  • But how do you decide when you choose an id or when you build a nested document? – tester Sep 13 '14 at 14:49
  • It all depends on your data. Some best practices include: don't have subdocuments that grow endlessly as they will slow down your system and could cause issues in the long term. If the data in the subdocuments is common to other documents and may change frequently, having a single source for that data is good.I try to examine HOW I need the data to. Sometimes storing an ID and common data, like a name, is handy when listing documents. Again, it all depends how YOU need to do things. – SomethingOn Sep 14 '14 at 19:37
1

Generally, if you decided to learn about NoSql databases you should follow the "NoSql way", i.e. learn the principles beyond the movement and the approach to design and not simply try to map RDBMS to your first NoSql project.

Simply put - you should learn how to embed and denormalize data (like Will above suggested), and not simply copy the id to simulate foreign keys.

If you do this the "foreign _id way", next step is to search for transactions to ensure that two "rows" are consistently inserted/updated. Few steps after Oracle/MySql is waiting. :)

emirc
  • 1,948
  • 1
  • 23
  • 38