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

79
Views
Migrating MongoDB fields with MongoEngine

So, I have two Documents in MongoDB modelled in MongoEngine (classic relational database example)

class Person(Document)
    name = StringField()

class Address(Document)
    person = ReferenceField(Person)
    city = StringField()

One person can have multiple addresses. I would like to migrate these models such that this becomes the new schema:

class Address(Document)
    city = StringField()

class Person(Document)
    name = StringField()
    address = ListField(ReferenceField(Address))

This involves both setting and unsetting fields on the two schema's and on top of that making sure that the old Address entries are migrated into the correct Person address list.

In my mind it would go something like this:

  1. Set field to Person
  2. For each Address add it to the correct Person by appending the ListField
  3. Unset field from Address

This seems like a trivial example with an easy enough solution although the implementation for mongoengine has eluded me for some time now. By changing the classes in python the server crashes complaining that fields cannot be resolved. Also reordering the classes is an issue since class dependency on eachother switches. On top of that collections in MongoDB need to be updated.

I remember SQLAlchemy has good migration support but I haven't found anything similar to this for Flask + MongoEngine.

Anyone know a good solution for this?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Mongodb does not have relationship like sqlalchemy so what about something like this:

class Person(Document):
    name = StringField()
    address = ListField(DocumentField(Address))

city1 = Address(city='City1')
session.save(ad)
city2 = Address(city='City2')
session.save(city2)
person = Person(name='Ali', address=[city1, city2])
session.save(person)
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!