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

80
Visualizações
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 Respostas
Responde à pergunta

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