I need to know what are the best practices for storing data in Mongodb.
I have a scenario where I need to develop a group chat.
Currently I have implemented the following structure:
Collection #1 - Users:
{ _id: "", name: "", email: ""}
Collection #2 - Groups:
{ _id: "", title: "", photo: "", "members": [{userId: ""}, {userId: ""}, {userId: ""}]}
Collection #3 - Messages:
{ _id: "", userId: "", messageText: ""}
I have created 3 different collections for this. Is this a good practice?
Or should I do something like:
Keep only one collection i.e: Users and put Groups and Messages under each user respectively?
Something like this:
{
_id: "",
name: "",
email: "",
groups: [
{
_id: "",
title: "",
photo: "",
members: [{userId: ""}, {userId: ""}, {userId: ""}],
messages: [
{ _id: "", userId: "", messageText: ""},
{ _id: "", userId: "", messageText: ""},
{ _id: "", userId: "", messageText: ""},
]
},
{
_id: "",
title: "",
photo: "",
members: [{userId: ""}, {userId: ""}, {userId: ""}],
messages: [
{ _id: "", userId: "", messageText: ""},
{ _id: "", userId: "", messageText: ""},
{ _id: "", userId: "", messageText: ""},
]
}
]
}