Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't query over ListField(EmbeddedDocumentField)

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

like image 318
Ashraf Fouda Avatar asked Apr 08 '26 15:04

Ashraf Fouda


1 Answers

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

like image 117
Ross Avatar answered Apr 11 '26 04:04

Ross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!