I have the following model
class Skill(EmbeddedDocument):
name = StringField(required = True)
level = IntField(required = True)
class Agent(Document):
name = StringField(required = True)
email = EmailField(required = True, unique = True)
skills = ListField(EmbeddedDocumentField(Skill))
I want to search for the Agents that have skills with (name = "computer skills and level >5)
I have wrote the following query:
Agent.objects.filter(name='ashraf', skills__level__gt=5,skills__name="Computer Skills")
If an Agent have skill named "Computer skills" with level = 3 and also have a skill named "English skills" with level = 10 this Agent will be in the query result
You would need to do an $elemMatch
[1] query and there is no inbuilt support for it in mongoengine at this time. You'd have to do a raw query like so:
Agent.objects.filter(
name='ashraf',
__raw__={"skills": {
"$elemMatch": {
"level": {"$gt": 5},
"name": "Computer Skills"
}
}}
)
[1] http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24elemMatch
I don't use python driver, but the general Mongo syntax for what you're trying to accomplish is this....
db.agent.find({name:'ashraf', 'skills.name' : "computer skills", level:{ $gt: 5}})
来源:https://stackoverflow.com/questions/9241408/cant-query-over-listfieldembeddeddocumentfield