Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google App Engine - Querying for arrays containing a value

I have a GAE Datastore table with an array field in it (containing a few strings). I would like to filter this table, based on all array fields that contain a specific string. How can i do that ? I didn't see a 'contains' operator in GQL, and the 'in' operator works the other way around. Do I just need to loop over all entities and do the check myself ?

(P.S. I'm using Python in my work with GAE).

like image 400
Baruch Oxman Avatar asked Sep 10 '13 13:09

Baruch Oxman


1 Answers

just use equals, for example:

class MyModel(db.Model):
  colors = db.StringListProperty()

MyModel(colors=['red', 'blue']).put()
MyModel(colors=['green', 'blue']).put()
MyModel(colors=['red', 'green']).put()

color = 'red'
query = MyModel.gql('WHERE colors = :1', color)
models = query.fetch(10)

assert len(models) == 2
like image 68
Gwyn Howell Avatar answered Oct 23 '22 07:10

Gwyn Howell