I have been developing an app for renting flats for a personal project of mine. I have been wondering about how to build/implement an inbox system to the current app which uses mongodb as database.
What does matter in such a feature? Would it be possible to define a schema called messages ?
var MessageSchema = new mongoose.Schema({
message: String,
sender : {
id : {
type : mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
}
recipient : {
id : {
type : mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
}
)};
If I were to send a message to user y then it would be stored as recipient and the x value would be the sender. When I want to render a page called inbox could i do Messages.find({req.user._id) to sort all messages ? I am a bit lost in regards to the schema design.
For schema, you probably want something like this:
const messageSchema = new mongoose.Schema({
subject: String,
body: String,
seen: Boolean,
sender: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
recipient: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
})
const userSchema = new mongoose.Schema({
username: String,
email: Boolean,
type: String
})
var Message = mongoose.model('Message', messageSchema);
var User = mongoose.model('User', userSchema);
For querying, you can find everything you need here