Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!