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

740
Vistas
How to remove schema validation in mongoose (mongodb)?

I have MongoDB server version: 5.0.6 installed and used "mongoose": "^6.0.14" to create a model validation. Example of model:

const User = new Schema({
  name: {
    type: String
  },
  relationId: {
    type: Number,
    unique: true,
    required: 'Id in SQL DB'
  },
});

Now I don't need the relationId to be required. Actually I want to delete it from the DB but I can't. And I can't to change the behavior of the validator. I tried:

Modified the Mongoose schema to:

const User = new Schema({
  name: {
    type: String
  }
});

As admin in the DB threw the command:

db.runCommand({ collMod: "users", validator: {}, validationLevel: "off" })

Returns an {ok:1} but when I try to insert a document via Mongoose still has the error: 11000 duplicate key error collection: collection.users index: relationId already exists

I used as well

db.users.updateMany({}, {$unset: {relationId: ''}})

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Since you had the unique: true part in the schema definition mongoose has already created a unique index in the database. Removing it from the schema definition doesn't automatically remove the index from the database. You should drop the index manually. You can use syncIndexes to remove the index from the database.

over 4 years ago · Santiago Trujillo 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