I am working with mongoengine and trying to do a simple migration. I have a field which I would like to migrate from being a StringField to a ReferenceField to another Object.
The 2 options I would suggest for your migration script:
I've always migrated things by hand using an intermediate collection or an intermediate field in the same collection, but this example makes it look like you don't have to. Stack overflow hates external links so I'm including the example verbatim below. BTW be careful with that "drop_collection" part!
import unittest
from mongoengine import *
class Test(unittest.TestCase):
def setUp(self):
conn = connect(db='mongoenginetest')
def create_old_data(self):
class Person(Document):
name = StringField()
age = FloatField() # Save as string
meta = {'allow_inheritance': True}
Person.drop_collection()
Person(name="Rozza", age=35.00).save()
rozza = Person._get_collection().find_one()
self.assertEqual(35.00, rozza['age'])
def test_migration(self):
self.create_old_data()
class Person(Document):
name = StringField()
age = StringField()
meta = {'allow_inheritance': True}
for p in Person.objects:
p.age = "%s" % p.age
p.save()
rozza = Person.objects.first()
self.assertEqual("35.0", rozza.age)
if __name__ == '__main__':
unittest.main()