Best choice for designing documents

Hello,

I am just starting with CouchBase and i would like your input on whats the best choice between embedding and referring document fields.

My use case is as follows: i am building a web app where notifications are triggered (a script produces these notifications and stores them to CB once every 24 hours) and presented to a publisher. A publisher may have many apps, and the apps themselves contain a lot of notifications.

A sample of the queries i would like to do internally are:

  • “Bring me all notifications from this publisher”
  • “List all notifications by notification_type”
  • “Bring me all notifications for this app”
  • “Bring me notifications for this specific timedate”

The structure of the doc the way i am viewing it is

Key:

 n::<'notification_id'>

Doc Value:

{  
 publisher : 'stelios',
 appid : 4,
 time_created: '2015-10-9',
 notification_type: 5,
 ...//more data
}  

Should i embed all the properties i want in a single document, or separate them into a document sets for notifications, a document set for the publishers and a document set for the apps. I have a read-heavy workload and inconsistencies are not an issue since a user cant update a field of a notification.

i.e refering the notification in a separate publisher doc:

key:

 p::<'publisher_id'>

Doc Value:

{  
 notification_ids: []
}  

First question: Can i use the lookup pattern to perform the first 3 queries:

client.add('n::1', notification_doc1);
client.add('n::2', notification_doc2);
client.set('p::stelios', {
    notification_ids: ['n::1', 'n::2']
});
client.set('a::4', {
    notification_ids: ['n::1', 'n::2']
});

This way I can first query for a publisher and get the notifications id and then with a multi get I can fetch all of their notifications.

Also, is there a way to have flexibility on my queries based on timeframe (e.g get notifications per day, per hour, etc) or should i use views to expose the timestamp?

I used these resources for my research:

http://docs.couchbase.com/developer/dev-guide-3.0/doc-design-considerations.html
http://blog.couchbase.com/data-modelling-when-embed-or-refer

If you have any other material in mind i could consult please refer me to it.

Hi,

about the queries and doc lookup, the listing query cannot be done as you don’t really know what you are looking for. It’s not deterministic so you would have to scan all docs. So for “List all notifications by notification_type”, you need a view, or use N1QL.
The other queries can be achieved with appropriate data modeling and lookup docs.

To have this level of flexibility(per day, hour etc…), you indeed have to use views or N1QL.

Hi Stelios_Savva,
I can see that you have already found some very good resources! Antoher resource that i have found very good is this video:

It describes many of the same things as you have already learned in the resources you mentioned but uses different sample data and explains a few other modeling ideas by sample.

Is it possible for you to try a few data models in a small PoC and use that PoC to decide what model is working best in you application?