Help designing document oriented with CouchBase


I need your help to design a document oriented forum database in Couchbase. This is a mobile application that requires the Document unique Key ID so I am thinking to use a composite Key document ( Text ID, Topic ID, The Category ID) where
1- Topic ID is an UUID id automatically assigned to the topic
2- Category ID: an Id automatically assigned to a category (like Couchbase server in the case of this forum).

Then, I would create;

  • A view on Mobile Device ID (which identifies the user id of the forum)
  • A view on the username (the username of the forum).

finally my document will look like this JSON;

“type”: “message”,
“DateTime”:"“Mar 13, 2015 9:00:20 PM”"
“about”: {
“TextId”: “g25636-g63773-hjjgede-gh8829”,
“type”: “message”,
“text”:“First Message”,
“DateTime”:"“Mar 12, 2015 3:00:01 AM”"

it is a simple forum on mobile, where I don’t allow people to update their messages after sending it, so my idea is to perform only inserts document (No deletes,No updates) Please let me know your thoughts ?



also should I consider including channel id in the JSON document for Sync Gateway ?


You might consider saving yourself that first view and having a look-up document, instead.

For example, if the mobile device ID is “123456” you might have a document with a key of “Mob:123456”, the content of which would be the user id. Obviously you’d need to pre-populate that and perhaps your current flow doesn’t allow for that.

If you’re planning to do insert-only, you’ll might need to consider purging documents from the local database on the device if the number of forum posts grew quite large.

You might later decide to change your JSON document schema. You’ll probably find it useful to record a “SchemaVersion” in the document to help you process different versions of documents appropriately.

As for whether you should include the channel ID in the document, my question is this: how are you generating the channel ID and what are you using it for? Is it each forum topic going to be a channel? If so, then you already have the TopicID in the document, which can serve as the channel name.


@matthew Thanks very much for your help,

My document will be something like this I think (please need your feedback on it);

“type”: “post”,
“Topic”:“please help me with your knowledge”,
“text”:“second reply 2”,
“DateTime”:“Mar 13, 2015 9:00:20 PM”,
“postAbout” =[
“PostId”: “post-4fgtyeye561h29033665e853a3dc8bc4c5e78f7a6cf8d55910df230567037tgbr-10”,
“city”:“San Francisco”,
“text”:“First Message”,
“DateTime”:"“Mar 12, 2015 3:00:01 AM”"
“PostId”: “post-8edstyefdq61h29033665e853a3dc8bc4c5e78f7a6cf8d55910df230567037hfee-1”,
“text”:“First reply 1”,
“DateTime”:"“Mar 12, 2015 5:23:43 PM”"

and Yes I will do only inserts and purge maybe post older than 3 months.

PostId ====> Unique ID to every post: Composite of -- ; we want to show the last n posts for user timeline; counter-number-of-posts is stored locally in the
couchbase Lite database , incremented every time user submit a new post.

type ======> useful for Map reduce View build , we create a type “post” ; Since the Map function is executed for each and every document we can filter by type.

userID ======> Device Unique ID

channel_id => this is topic id (Topic_ID) and ID for the topic It’s added to post-message documents to associate them with that topic and this is also used on the Sync Gateway side to assign messages to channels and grant user access.

DateTime ========> will help to know how many users are “active” for a certain period and keep track of user activity.



@jens could you please suggest, this is a couch Lite modeling :smile:Do you think appending the posts of the same topic in the one single JSON document is better than making every post a separate JSON document.


@Molenx can you please help :slight_smile: I read your book it is the best :slight_smile:


Putting multiple posts in one document is probably a bad idea. It’s going to take more traffic to replicate the document as it gets bigger and bigger, and if two people add a post and then push to the server, it creates a conflict.