MongoDB Structure for message app

孤人 提交于 2019-11-29 20:59:48

I see that this question is old, but for anyone interested, a similar question was asked and one answer looks viable https://stackoverflow.com/a/30830429/132610

Conversation : {
 id: 123,
 members: [ user_id1, user_id2 ]
}
Message { conversationId: 123, author: user_2, body: 'Hi what's up' }
Message { conversationId: 123, author: user_1, body: 'Whanna ask some question on stackoverflow' }

Update #1

1) Scalability: MongoDB scales well with very large collection. Billions of messages per collection. There is a technique called sharding that can allow you to split larger collection to multiple nodes.

2) Reading. Since MongoDB has indexing mechanisms, reads are comparable to any fine-tuned database engine. So reading will not be an issue. Especially, when a conversation(group|room) has fewer participants, for example two people messaging each other.

Your question is really one of schema design. I suggest taking a look at this page on MongoDB schema design to get a sense of the choices and trade-offs: http://www.mongodb.org/display/DOCS/Schema+Design

In addition, you should probably review the links in the 'See Also' section of that document. I especially recommend the video presentations.

Finally, you should probably take a look at this document for a discussion of the three possible schemas for a messaging/commenting database, including the trade-offs for each design: http://docs.mongodb.org/manual/use-cases/storing-comments/

Please find my suggestion:

    Person : {
        person_id: '123',
        last_login: 12.06.2008,
        online: true
    }

Conversation : {
 conversation_id: append the greater person_id to the lower person_id, // person_1_id =123 and person_2_id =124 then its 123124

messages: [ 
        { message_id: 1, 
          message_text : 'Hi what's up',
          sender_id : 123,
          receiver_id: 124,
          timestamp : 12344567891
        },
        { message_id: 2, 
          message_text : 'fine',
          sender_id : 124,
          receiver_id: 123,
          timestamp : 12344567891
        }
       ]
}

this is my suggestion

{
"_id" : ObjectId("5a9e9581a2147c0c0f00002e"),
"id_members1" : "5a9e9581a2147c0c0f02345t",
"id_members2" : "5a9e9581a2147c0c0f02134g",
"name" : [ 
    "Omar", 
    "Mohamed"
],
"messages" : [ 
    {
        "author" : "Omar",
        "body" : "salam 3likom",
        "create_at" : ISODate("2018-03-07T09:04:04.000Z")
    }, 
    {
        "author" : "Mohamed",
        "body" : "Wa3likom salam",
        "create_at" : ISODate("2018-03-07T09:04:04.000Z")
    }, 
    {
        "author" : "Mohamed",
        "body" : "wach teshak",
        "create_at" : ISODate("2018-03-07T09:04:04.000Z")
    }, 
    {
        "author" : [ 
            "Omar", 
            "Mohamed"
        ],
        "body" : "test msg",
        "create_at" : ISODate("2018-03-25T15:30:05.000Z")
    }
],
"comments" : [ 
    null, 
    {
        "author" : [ 
            "Omar", 
            "Mohamed"
        ],
        "body" : "test msg",
        "create_at" : ISODate("2018-03-25T15:28:11.000Z")
    }, 
    {
        "author" : [ 
            "Omar", 
            "Mohamed"
        ],
        "body" : "test msg",
        "create_at" : ISODate("2018-03-25T15:28:31.000Z")
    }
]

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!