You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Summary: When using a DynamicEmbeddedDocument, an InvalidQueryError occurs if you try to set a field that is not defined in the MongoEngine schema. This invalidates the purpose of a dynamic document.
Example:
class B(DynamicEmbeddedDocument):
foo = StringField()
class A(Document):
b = EmbeddedDocumentField(B)
# This works
A.objects(...).update(set__b__foo="123")
# This does not work
A.objects(...).update(set__b__bar="123")
# mongoengine.errors.InvalidQueryError: Cannot resolve field "bar"
Solution:
In my custom fork of MongoEngine, I've updated the EmbeddedDocumentField to account for dynamic documents as such.
class EmbeddedDocumentField(BaseField):
def lookup_member(self, member_name):
doc_and_subclasses = [self.document_type] + self.document_type.__subclasses__()
for doc_type in doc_and_subclasses:
field = doc_type._fields.get(member_name)
if field:
return field
# NEW CODE
if member_name not in ("S", "$") and any(issubclass(doc_type, DynamicEmbeddedDocument) for doc_type in doc_and_subclasses):
return DynamicField(db_field=member_name)
Custom Situation: I encountered this error because I had a EmbeddedDocumentListField of DynamicEmbeddedDocuments and I was trying to perform array operators like so:
If the maintainers find my solution acceptable, I would be happy to write tests and make a PR with it. Otherwise, I will defer to coders with greater expertise in this codebase for the proper strategy here.
The text was updated successfully, but these errors were encountered:
mcsimps2
changed the title
DynamicEmbeddedDocument errors during update/modify
DynamicEmbeddedDocument errors during update when adding new field
Mar 4, 2021
mcsimps2
changed the title
DynamicEmbeddedDocument errors during update when adding new field
EmbeddedDocumentField errors during update when adding new field to DynamicEmbeddedDocument
Mar 4, 2021
mcsimps2
added a commit
to mcsimps2/mongoengine
that referenced
this issue
Mar 4, 2021
In order to allow setting new fields on dynamic embedded documents that previously did not exist, the `lookup_member` function returns dynamic fields as appropriate. This enables operations like `A.objects(...).update(foo__newfield="bar")`.
ResolvesMongoEngine#2486
mcsimps2
added a commit
to mcsimps2/mongoengine
that referenced
this issue
Apr 10, 2025
In order to allow setting new fields that did not previouly exist on dynamic embedded documents, the `lookup_member` functions on `EmbeddedDocumentField` and `GenericEmbeddedDocumentField` return dynamic fields as appropriate. This enables operations like `A.objects(...).update(foo__newfield="bar")`.
ResolvesMongoEngine#2486
mcsimps2
added a commit
to mcsimps2/mongoengine
that referenced
this issue
Apr 10, 2025
In order to allow setting new fields that did not previouly exist on dynamic embedded documents, the `lookup_member` functions on `EmbeddedDocumentField` and `GenericEmbeddedDocumentField` return dynamic fields as appropriate. This enables operations like `A.objects(...).update(foo__newfield="bar")`.
ResolvesMongoEngine#2486
Summary: When using a
DynamicEmbeddedDocument
, anInvalidQueryError
occurs if you try to set a field that is not defined in the MongoEngine schema. This invalidates the purpose of a dynamic document.Example:
Solution:
In my custom fork of MongoEngine, I've updated the
EmbeddedDocumentField
to account for dynamic documents as such.Custom Situation: I encountered this error because I had a
EmbeddedDocumentListField
ofDynamicEmbeddedDocument
s and I was trying to perform array operators like so:If the maintainers find my solution acceptable, I would be happy to write tests and make a PR with it. Otherwise, I will defer to coders with greater expertise in this codebase for the proper strategy here.
The text was updated successfully, but these errors were encountered: