Mongoengine query based on reference field attribute

Viewed 2954

I have the following Documents

Food(Document)
  title = fields.StringField()
  type = fields.StringField()

Recipe(Document)
  title = fields.StringField()
  food = fields.ReferenceField(Food)

I would like to get the list of recipes whose food.type == "nonveg"

Can I do something like the following?

Recipe.objects.where(food__type="nonveg")

Thanks!

3 Answers

"type" is a reserved word in mongodb for an operator. So you should use this query : Recipe.objects(food__type__="nonveg")

Related