Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
How to create a mongoose schema to accept only some discreet values

Lets say I have this schema.

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const TestSchema = new Schema({
  language: {
  required: true,
  default: 'eng',
  },
});

// Compile model from schema
const TestModel = mongoose.model("Test", TestSchema);

How can I configure this schema so that language field will only accept its value as eng or hbrw. If it recievs some other value it would return an error.

I know it can be done explicitly with if else but I want to know if it can be built into the schema.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This is how its done:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const TestSchema = new Schema({
  language: {
    required: true,
    enum: ['eng', 'hbrw']
    default: 'eng',
  },
});

// Compile model from schema
const TestModel = mongoose.model("Test", TestSchema);

Enum field validates the coming data against the array of data given in enum field.

Thanks @turivishal for this solution

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda