Help designing document oriented with CouchBase


#1

Hello,
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;

{
“TextId”:“6a4353-g63773-hjjgede-gh8829”,
“type”: “message”,
“userID”:“gdhetyey637302027302hdihjeihe73832”,
“userName”:“user526262”,
“TopicID”:“90002891”,
“CategID”:“15”,
“text”:“reply1”,
“DateTime”:"“Mar 13, 2015 9:00:20 PM”"
“about”: {
“TextId”: “g25636-g63773-hjjgede-gh8829”,
“type”: “message”,
“userID”:“gt6uI1h7302hdihjeihe73832”,
“userName”:“user101”,
“TopicID”:“90002891”,
“CategID”:“15”,
“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 ?

Thanks
Wissa


#2

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


#3

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.


#4

@matthew Thanks very much for your help,

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

{
“PostId”:“post-2ed202ac08ea9033665e853a3dc8bc4c5e78f7a6cf8d55910df230567037dcc4-15”,
“type”: “post”,
“userID”:“2ed202ac08ea9033665e853a3dc8bc4c5e78f7a6cf8d55910df230567037dcc4”,
“userName”:“user526262”,
“Age”:“51”,
“Sexe”:“F”,
“city”:“Paris”,
“state”:“NA”,
“country”:“France”,
“channel_id”:“90002891”,
“Topic”:“please help me with your knowledge”,
“CategID”:“15”,
“Category”:“Shopping”,
“text”:“second reply 2”,
“DateTime”:“Mar 13, 2015 9:00:20 PM”,
“SchemaVersion”:1,
“postAbout” =[
{
“PostId”: “post-4fgtyeye561h29033665e853a3dc8bc4c5e78f7a6cf8d55910df230567037tgbr-10”,
“userID”:“4fgtyeye561h29033665e853a3dc8bc4c5e78f7a6cf8d55910df230567037tgbr”,
“userName”:“user101”,
“Age”:“42”,
“Sexe”:“M”,
“city”:“San Francisco”,
“state”:“California”,
“country”:“USA”,
“text”:“First Message”,
“DateTime”:"“Mar 12, 2015 3:00:01 AM”"
},
{
“PostId”: “post-8edstyefdq61h29033665e853a3dc8bc4c5e78f7a6cf8d55910df230567037hfee-1”,
“userID”:“8edstyefdq61h29033665e853a3dc8bc4c5e78f7a6cf8d55910df230567037hfee”,
“userName”:“user21130002”,
“Age”:“32”,
“Sexe”:“M”,
“city”:“Madrid”,
“state”:“NA”,
“country”:“Spain”,
“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.

thanks


#5

@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.
Thanks


#6

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


#7

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.