Best practice about Table Name & Document ID in NoSQL


#1

Hi,

I need to know the best practice for these things:

First: About Table/Class Name.
In RDBMS we can create table. In NoSQL, we have views and map.
What if I have 2 object like this:
Object Customer = {name: xxx, address: xxxx}
Object Warehouse = {name: xxx, location: xxxx}

Suppose I want to list all of customers, I will create a view like this:

function(doc, meta){
    if(doc.name){
        emit(doc.name);
    }
}

In this condition, Warehouse will be emitted too.
What is the best practice for this situation?
Should I check every single property of the object in map function?
or should I create a unique property for each class, i.e: {table: 'Customer, name: xxx, address: xxx}.
or I add the class name in document ID?

Second: What would be the best value for Document ID?
I read a suggestion to put a meaningful value taken from object’s key value as Doc ID, not only a random GUID.
My question is: What if the key value in the object change?

Any advices for a newbie like me will be appreciated.


#2

Hi,

People usually add a type field to their document and check this property in the map function.

Having meaningful key is always helpful. If you have a user object then your key could be user::uuid::someUUID. Or user::email::useremail@userdom.com if you think the email will not change. There are several blog post on that topic:

http://blog.couchbase.com/three-things-know-about-document-database-modelling-part-1
http://blog.couchbase.com/manual-secondary-indexes


#3

In the first question you should put in an addition field called “_entity”:“person” or “_entity”:“warehouse” that way you can put additional filter in your map/reduce

function(doc,meta){
 if(doc.name && doc._entity && doc._entity == "Warehouse"){
   emit(doc.name,1);
   }
}

or you can possible pre-pend you key to have that data old key : abc123 new key : wh_abc123 or per_abc123

For your second question it just depends.
You want it to be unique to prevent overriding a key & you don’t want it to big either as keys in Couchbase can only be 250 characters & every byte counts b/c memory is expensive.

To make a key super unique I do md5 or sha1 or sha2 (MAC Address of app server + time stamp + email address of user).


#4

How do you create unique keys using Java SDK? Will such key creation be successful even if we create documents in parallel using multiple threads? Can you please explain more about this?


#5

You can do an ADD() operations. This will throw an error if it already exists. so you would probably do a GET() w/ CAS number and do a CAS() operations [Check And Set]