i am trying to build a chat application.. i am not sure how to structure my mongodb schema..
currently i am using this schema.. collection name: conversations
{
conversation: {
id: 1,
name: test
},
messages: [
{
_id: mongoid("123"),
userID: 1,
message: "test message"
date: date..,
status: 1,
pins: [2, 3] //message pin by user ids
mentions: [4, 5],
replyMsgID: mongoid(456)
},
...
],
users: [
{
userID: 1,
blocked: 1,
lastActiveTime: dateTime,
},
...
]
}
...
i am thinking another schema like this...
{
conversation: {
id: 1,
name: test
},
messages: [
{
_id: mongoid("999")
msgID: mongoID("123")
},
{
_id: mongoid("888")
msgID: mongoID("456")
},
...
],
users: [
{
userID: 1,
blocked: 1,
lastActiveTime: dateTime,
},
...
]
}
messages collection..
messages: [
{
_id: mongoid("123"),
userID: 1,
message: "test message"
date: date..,
status: 1,
pins: [2, 3] //message pin by user ids
mentions: [4, 5],
replyMsgID: mongoid(456),
conversationID: 1
},
...
]
i am new to mongodb and i am confused which schema should be use for a scalable for chat application..
i thinks the problems i will face with schema 1 are.. at some point the document size might get pass 16mb limit of mongo... when fetch or update anything like if i need only 1 message or just a single user the mongo will return the whole document with thousands of messages
the problem with 2nd schema ... i will need to join two collections with aggregate framework and might need to use $unwind, $group a lot... which i think will be very performance heavy... to create a new message i will have to insert the message in the messages collection first then insert the the newly added message to the conversations collections property messages field, same goes for delete.. so lots of query.. :|
it would be really helpful if someone could help me out to select a schema.. or create a better schema..
thanks in advance :)