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

241
Visualizações
Storing enum to MongoDb (for managing tag names)

If we have a collection of books, we can assign tags of authors into an array as follows:

Books collection

{
    ...,
    "authors" : ["John Michaels", "Bill Williams"]
}

This can cause problems if an author's name changes.

Instead, I was thinking of assigning an integer value to each author and creating a 'tags' collection:

Tags collection

{
    “tags” : [
        {“John Michaels” : 0},
        {“Jane Collins” : 1},
        {“Bill Williams” : 2}
    ]
}

Here is my books collection, here we specify that ‘John Michaels’ and ‘Bill Williams’ are the authors:

{
    …,
    “authors” : [0, 2]
}

If I ever needed to change the author’s name ‘Bill Williams’ to ‘Bill H. Williams’, there would be no problem because the value stored in the books collection remains unchanged.

My Question is if MongoDB has something like enums that will automatically increment the integral value or if there is something else built into MongoDB to help with this type of situation.

Thank you

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

0

This is typical use case of referencing other collections. So, you should have 2 collections:

Authors collection:

{
  _id: ObjectId,
  name: String,
  ... // Other fields
}

Books collection:

{
  _id: ObjectId,
  authors: [ ObjectId ], // References to documents from Author collection
  ... // Other fields
}

So, in authors property of the Books collection, you store _id values of all the authors. Then when you fetch book document, you can easily fetch up-to-date authors data from Authors collection.

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