MongoEngine/mongoengine

Question re querying documents containing fields not defined in model

anentropic opened this issue · 1 comments

Docs here https://docs.mongoengine.org/guide/validation.html#document-validation say:

...Mongoengine will not validate a document when an object is loaded from the DB into an instance of your model but this operation may fail under some circumstances (e.g. if there is a field in the document fetched from the database that is not defined in your model).

I wanted to confirm if that last part is true, or if there is a way around it?

I would like to use MongoEngine to query a pre-existing mongodb collection, and would like to define a model schema for only the fields I care about. I actually have no real way of knowing for sure what all possible fields of different documents in the collection are.

It'd be fine to get an error if the retrieved doc contains a field value whose type conflicts with what I've defined in my schema. But I want to ignore rather than error for any fields in the retrieved doc that aren't in the schema. Is that possible?

set strict=False on your Document, it will make it ignore fields that aren't defined on your Document

https://docs.mongoengine.org/apireference.html#documents
By default, any extra attribute existing in stored data but not declared in your model will raise a [FieldDoesNotExist](https://docs.mongoengine.org/apireference.html#mongoengine.FieldDoesNotExist) error. This can be disabled by setting strict to False in the meta dictionary.

As for fields that you define, you won't necessarily get an exception, it's case by case on the type of fields. AFAIR it depends if it does some parsing of the fields. If you want validation of all defined fields, you can call .validate on your Document after loading it