How to properly store mentions in document

Hello,
I am trying to implement the mention feature (@mention) for my app. However, I am not entirely sure how I should design the documents for this feature.

The first way I could think of is to have a placeholder code (something like [&] for example) to specify the place where mentions happens, and have a ‘mentions’ field containing the userID of the mentioned users (this field could either be an array of string or a map[string]int mapping the id with the corresponding position of the mentions). Front end could then replace the placeholder code with the mention text (@FirstName LastName for example) and display when it receives the document from back end.
A message document containing ‘Hey @A, @B’ would look like this in this case:

  messageID: {
     docType: 'message',
     text: 'Hey [&], [&]',
     mentions: ['uid::a', 'uid::b'],
  }

or

  messageID: {
     docType: 'message',
     text: 'Hey [&], [&]',
     mentions:{
         'uid:a': 1,
         'uid:b': 2
     },
  }

The second way could be having some kind of format like [@username]{user::id} or @{user::id} to specify that that the person with userID of user::id is being tagged at that position of the text (kinda similar to how markdown does hyperlink I think). Then front end could format it correspondingly to display the tagging.
In this case , the same message above would be stored as below:

  messageID: {
     docType: 'message',
     text: 'Hey [@A]{uid::a}, [@B]{uid::b}',
  }

or

  messageID: {
     docType: 'message',
     text: 'Hey @{uid::a}, @{uid::b}',
  }

This is probably less about Couchbase db specifically and more about designing the db in general, but I am pretty inexperienced and do not know what is the better way to implement this (or if there is another way that is better than the ones I have and solves their issues). Thank you a lot in advance and I would really appreciate any advice or suggestion.

Hi @bte234,

A long time ago, I worked on a product that also had mention functionality. I can’t remember exactly how it worked, but I think they were encoded along the lines of a combination of your first example and your third example. If you’ll be using N1QL queries, I think the first approach (using an array) might offer some advantages. If you plan to use Full Text Search, you might benefit from storing the usernames as well (e.g. someone is searching for a twitter name). So you could combine along the lines of:

  messageID: {
     'docType': 'message',
     'textParsed': 'Hey [@mgroves]{uid::a}, [@probablyrealrob]{uid::b} how are you?',
     'rawText': 'Hey @mgroves, @probablyrealrob how are you?'
     'mentions': ['uid::a', 'uid::b'],
  }

(Assuming you can make the matchup between mentions and the tokens in textParsed work).

Generally speaking, I would think about what kind of queries you plan to run against these messages, and if they would involve mentions or not.

I agree that this isn’t a Couchbase-specific problem, so you might want to have a look at the Twitter-text library on Github, to see how they are parsing mentions (and you might get some insight on how they are storing them as well).

1 Like