I have no clue as to what your "Q" function is doing, but I'm going to show you how I query embedded documents. (EDITED: I see what you're talking about the "Q" now. Added to my answer down below)
Looking at your models~
class EmbeddedDoc(EmbeddedDocument):
name = StringField()
def to_json(self):
return {
"name": self.name
}
class ParentDoc(Document):
name = StringField()
children = EmbeddedDocumentFieldList(EmbeddedDoc)
def to_json(self):
return {
"_id": str(self.pk),
"name": self.name,
"children": [child.to_json() for child in self.children]
}
I override the to_json() method to make it more human readable. That part is optional, but I recommend it.
I would do somethin like this in my method~
pdoc = ParentDoc.objects(pk=some_ID_variable)
if pdoc:
pdoc = pdoc.get(pk=some_ID_variable)
if pdoc.children.filter(name=some_name_variable):
child = pdoc.children.get(name=some_name_variable)
return child.to_json()
return {"message": "Child with that name not found"}
return {"message": "Parent with ID not found"}
What's going on? First I query to see if the ParentDoc exists. If it exists, get the ParentDoc object from the queryset. Query the object's embedded objects by name using filter, since embedded objects don't have primary keys. If it exists return the embedded object in json format.
According to the advanced queries section Mongoengine User guide, to use an or operator, I'll need to use the or bitwise operator "|" along with a Q class.
So I'd add this ~
from mongoengine.queryset.visitor import Q
and change the above filter to something like~
children = []
cdocs = pdoc.children.filter(Q(someParam=someValue) | Q(anotherParam=anotherValue))
children.append(cdoc.tojson() for cdoc in cdocs)
return {"Children": children}
I've not used this though. Hope it works for ya!