Search code examples
ibm-cloudcloudant

Decide create multiple database and relational in cloudant


I'm currently learning NOSQL cloudant and trying to design the database, as I'm learning I'm cloudant treat all records as a documents and using denormalise for table. so I'm currently a bit confuse to how to decide which one need to be in one document and which one is need to be separated.

Below are my test cases : let's say i'm designing store book tables structure, for simplicity I'll be having this tables BOOK, STORE, STORE_BRANCH

BOOK field : _id, book_name, author STORE field : _id, store_name STORE_BRANCH field : _id, store_branch_name, address, store_id_fk

with above case, I not able to decide where should i put the "price" field to ? as for normal RDBMS i will just create another table and having fields : ( store book_id, store_branch_id and prices), this with the assumption the price of the book is different for each branch. so i wondering how I put this in cloudant ?

any suggestion is appreciated


Solution

  • Your doubts are pretty common for RDMBS user. In NoSQL generally you use the everything-in-one-document approach. In fact, in some cases, approximating JOINs in a document-oriented database like Cloudant is outright trivial. For example if you want to model a one-to-n relationship, you can put all n-related documents into the document they belong to. In your case you should put all the store_branch in the related store. This strategy is OK if:

    • The document does not get so big that it impairs performance. This can be mitigated somewhat by using database views or show functions.
    • The information in the inner document only appears there and does not need to be duplicated into other documents, or such duplication is acceptable for your application.
    • The document does not get updated concurrently. If it does, there will likely be unnecessary conflicts that will need to be resolved by the application.

    If the above strategy is not applicable you can use an approach that more closely mimics how you solve this problem in a relational database: you can create a document for each "relational table". In your case you should create a document having fields : ( store book_id, store_branch_id and prices), too.

    This Cloudant article explains very deeply these possibilities: Cloudant - Join the fun, have fun with JOINs.