Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

314
Visualizações
Mongo / Mongoose: Does Mongoose automatically create indexes on ObjectId types?

I may have missed this in the mongo index documentation or in the mongoose documentation.

Suppose I have a mongoose schema as such:

const SomeEntity = new Schema({
  foo:  { type: String, required: true },
  bar   { type: Schema.ObjectId, ref: 'Bar' }
});

Should I be creating an index on field bar or would mongo automatically address this?

In other words, does mongo automatically create indexes for ObjectId types ?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

In other words, does mongo automatically create indexes for ObjectId types ?

No, the only automatic index that MongoDB creates is for the _id field.

However, it depends on the type of queries you're going to run against your model if you need an index for bar.

Since bar refers to the _id field of documents in the bars collection, those documents themselves will be covered by the automatic _id index that will get created on that collection.

But if you need to be able to find documents in the "SomeEntity" collection that refer to a specific bar:

SomeEntity.find({ bar : someBarId })

...then you probably want to create an index for it:

bar: { type: Schema.ObjectId, ref: 'Bar', index : true }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda