Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
Building an inbox system using Express/MongoDB

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.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!