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

246
Vistas
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 Respuestas
Responde la pregunta

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 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