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

682
Vistas
Specify an Index Name with Mongoose

When defining a Mongoose schema it is often prudent to specify what indexes should exist. That said, in many cases we would like to control the name of the index created, especially when those indexes are compound so they are understandable.

Indeed, when creating certain indexes, specifying an index name explicitly is required to avoid exceeding the index name length limit.

Since ensureIndex is called (by default) on indexes defined in the schema, what is the appropriate syntax to control the name of an index created by ensureIndex? I assume this is impossible with the field level index syntax, but surely it is available for schema level indexes?

var ExampleSchema = new Schema({
  a: String,
  b: String,
  c: Date,
  d: Number,
  e: { type: [String], index: true } // Field level index
});

// We define compound indexes in the schema
ExampleSchema.index({a: 1, b: 1, c: 1});
ExampleSchema.index({d:1, e:1}, {unique: true});

It is worth noting that db.collection.ensureIndex is deprecated (by mongodb), and is now an alias for db.collection.createIndex.

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

0

You can set the name of the index using the name property of the option parameter of the index call:

ExampleSchema.index({a: 1, b: 1, c: 1}, {name: 'my_index'});
ExampleSchema.index({d:1, e:1}, {name: 'my_other_index', unique: true});

As noted in the docs, the second parameter of index contains the:

Options to pass to MongoDB driver's createIndex() function

The createIndex doc list all the possible option settings, including name.

over 4 years ago · Santiago Trujillo Denunciar

0

It turns out that Mongoose wraps the Mongo driver fairly transparently.

As such, a call to <Mongoose.Schema>.index(<keys>, <options>) can be roughly interpreted to result in a call to db.collection.ensureIndex(keys, options) or db.collection.createIndex(keys, options) in Mongo 3.0+.

Hence, the required syntax (while poorly undocumented) is identical to the MongoDB syntax for schema index declarations.

That is, we declare names as follows:

ExampleSchema.index({a: 1, b: 1, c: 1}, {name: "ExampleIndexName_ABC"});
ExampleSchema.index({d:1, e:1}, {unique: true, name: "ExampleCompoundIndexName"});

Options are also include:

  • background
  • unique
  • name
  • partialFilterExpression
  • sparse
  • expireAfterSeconds
  • storageEngine

See the official MongoDB documents for details.

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