简体   繁体   中英

UpdateQueryError MongoDB Array field

So I have a mongo object:

@connection.register
class UserInformation(Document):
    structure = {
        'extra_infos':[{
            'nickname':basestring,
            'name_on_account':basestring,
        }],
        'age': int,
        'mean_distance':float,
    }

And I updated the doc to this

@connection.register
class UserInformation(Document):
    structure = {
        'extra_infos':[{
            'nickname':basestring,
            'name_on_account':basestring,
            'email':basestring,
        }],
        'age': int,
        'mean_distance':float,
    }

Then I went ahead with the following migration:

class UserInformationMigration(DocumentMigration):
    def allmigration01_email(self):
        self.target = {'extra_infos':{'$exists':True},'extra_infos.email':{'$exists':False}}
        self.update = {'$set':{'extra_infos.email':[]}}

Then I executed the following commands

migration = UserInformationMigration(UserInformation)
migration.migrate_all(collection=connection['user_info'])

But I keep on getting the following error:

UpdateQueryError: 'extra_infos.email' not found in UserInformation's structure

What am I doing wrong? is it because extra_infos is an array? I suspect that my migration is defined incorrectly, but I am not sure how to define it differently for an array extra_infos

Yes, you are correct. You will need to use this instead:

self.update = {'$set':{'extra_infos.0.email':[]}}

It will reference the first element in 'extra_infos'.

The datatype of extra_infos is list and not dict .

One way is as suggested by @atx, but the approach seems to be a hack and not as a solution, according to me, since if it is not a list of dicts, but only one dict in the list, you may directly have a dict .

You may actually modify the executor field as following and need not alter your code:

@connection.register class UserInformation(Document): structure = { 'extra_infos':{ 'nickname':basestring, 'name_on_account':basestring, 'email':basestring, }, 'age': int, 'mean_distance':float, }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM