google-app-enginegoogle-cloud-datastoregqlgqlquery

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).


Solution

  • 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