mongoengine - Ignore extra fields for schema validation

有些话、适合烂在心里 提交于 2019-12-03 10:40:27

问题


I am trying to query my database. Some records currently have extra fields that are not included in my model schema (by error, but I want to handle these cases). When I try to query the DB and transform the records into the schema, I get the following error:

FieldDoesNotExist
The field 'X' does not exist on the document 'Y'

Because of the extra fields in the database that differ from the schema.

Is there a way to ignore this schema validation for extra fields in mongoengine?


回答1:


For ignoring this error when having extra fields while data loading, set strict to False in your meta dictionary.

class User(Document):
    email = StringField(required=True, unique=True)
    password = StringField()
    meta = {'strict': False}



回答2:


I believe you want to use a DynamicDocument instead of a Document when defining your model and that will allow extra fields in the db schema to be ignored.




回答3:


I think you want skip schema validation, so when you save your document

document_name.save(validate=False)



回答4:


You can extend from mon.DynamicDocument.

class AF(mon.DynamicDocument):
  meta = {
    'collection': 'af'
  }
user_id = mon.StringField(db_field='customer_user_id')

You can see from the document. A Dynamic Document class is allowing flexible, expandable and uncontrolled schemas.



来源:https://stackoverflow.com/questions/29495037/mongoengine-ignore-extra-fields-for-schema-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!