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

117
Views
mongoose - How can I set the role field with only predefined options

Here's my user Schema :

const userSchema = new mongoose.Schema({
  username: {
      type: String,
      validate: function (v) {
          return /([0-9a-zA-Z])\w{6,}/.test(v);
      },
      required: [true, "Username required"],
  },
  passwordHash: {
      type: String,
      required: [true, "PasswordHash required"],
  },
  role: {
      type: String,
      required: [true, "User role required"],
  },});

How can I make the role field as an array of two items -admin and manager, where the user can only choose between those two options?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try using enum like so

  role: {
   type: String,
   enum: ["manager", "admin"],
   default: "manager",
  }
about 4 years ago · Juan Pablo Isaza 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!