Migrations in mongoengine: InvalidId

前端 未结 2 603
南旧
南旧 2021-02-06 09:43

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.

相关标签:
2条回答
  • 2021-02-06 10:19

    The 2 options I would suggest for your migration script:

    • using DynamicField on the field you must migrate should allow you to read ObjectIds and store back DBRefs
    • doing the migration in pymongo directly (pymongo's collection is accessible through Person._get_collection()) and looping over the items, updating & saving
    0 讨论(0)
  • 2021-02-06 10:29

    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()
    
    0 讨论(0)
提交回复
热议问题