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

287
Views
Can a subdocument be required in mongoose?

Is it possible to have nested schemas in mongoose and have a required validator on the children? Something like this:

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  }
});

const eventSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  host: {
    type: userSchema,
    required: true
  }
});

I can't find anything in the documentation. Thanks.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Yes, your schema is correct.

The docs for mongoose nested schema (SubDocuments) can be found here

over 4 years ago · Santiago Trujillo Report

0

i suppose you'll update eventSchema with subdocuments of type user model. you can use { runValidators: true} for update.

eventModel.update({ name: 'YOUR NAME' }, { $push: { host: user } }, { runValidators: true}, function(err) {

})
over 4 years ago · Santiago Trujillo Report

0

You can use the nested schema in mongoose.

It will also give you he Object Id on each sub schema values as well.

  • Doc: Here
  • Example: Here
over 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!