Domanda

I am trying to model a FB-like chat/discussion between two users using MongoDB.

I came up with the following BSon "structure" for a message:

  {
    _id: ObjectId(...),
    discussionId: ObjectId(...),
    from: {
       id: ObjectId(...), 
       nickname: ‘Joe’,
       thumbnail: ‘xoopp7788ee….jpg’
     },
    to: {
       id: ObjectId(...), 
       nickname: ‘Jane’,
       thumbnail: ‘rtolkj96547cc….jpg’
     },
    text: ‘Hello Jane, How are you today?’,
    posted: ISODateTime(...),
    viewed: ISODateTime(...),
    next: ObjectId(...),
    previous: ObjectId(...) 
  }

I did read the following mongodb documentation but I still wanted to submit my question because my model/problem is slightly different.

First I am not sure whether the next and previous fields are necessary. Can I use just the posted field in order to thread the messages?

Second, can having just one document per message pose a performance issue? Would I be better off with several messages per document?

I am looking forward to reading your comments and suggestions.

edit1 : the discussionId would somehow be a unique ID for a combination of two users...

È stato utile?

Soluzione

IMHO:

  • thumbnail if it references an avatar should be in user profile. If that's a smiley then it should be at the same level as text.
  • if from.id is a user ID, then to save space you don't need to repeat nickname. It can be "injected" in UI or at the time it's sent out somewhere if needed. Sometimes duplicating data can be useful, but in this case it's dubious.
  • discussion collection might already have from and to per each discussion. In this case you don't have to repeat user IDs in each message. You could keep only from.
  • If there could be more than 2 people discussing something - group chat, then from and to does not work. You might have to make 2 arrays of from and to. It depends on the type of chat you would have.
  • next and previous: usually I would not have them. You can find a discussion thread by discussionId, from.id, to.id and sort it by posted. Unless you have some very strange way chat works you can delete these 2 fields.
  • posted has a date and it's ok, but if you want to save more space date is stored in _id as well, so you can avoid creating an index and storing an extra field.
  • eventually, if you decide to remove nickname and thumbnail then you can bring from.id to a level up as fromId.

Overall it looks good. I'm just nitpicking for performance purposes and small improvements.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top