Saturday 21 February 2015

Many-to-many mapping


In many-to-many relationship one more entities are mapped to one or more entities.

Example
books-authors
students-teachers
customer-products

One book can have one or more authors.
One author can write one or more books.

Books-authors modelling
We can create two collections, one for books and other for authors.

author collection
{ "_id" : 1, "name" : "Herbert", "books_ids" : [ 1 ] }
{ "_id" : 2, "name" : "Kanetkar", "books_ids" : [ 1, 2 ] }

books collection
{ "_id" : 1, "title" : "learn java", "author_ids" : [ 1, 2 ] }
{ "_id" : 2, "title" : "Explore java", "author_ids" : [ 2 ] }

Document in books collection contains author_ids array, which refers to a document in author collection.

Document in author collection contain books_ids, which refers to a document in books collection.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment