Belvi Nosakhare
Belvi Nosakhare

Reputation: 3255

Retrieving entity with OR keyword in Google Datastore

class Object(db.Model):
    title = db.StringProperty()
    user = db.StringProperty()
    tags = db.StringListProperty()

want to implement query on the object above that will product same result as:

Select * from Object WHERE title = 'title name' OR user = 'User name'

Google datastore query i have been able to implement seems to handle

Select * from Object WHERE title = 'title name' AND user = 'User name'

Is there any way i can implement this ?

PS: I am using python on google app engine

Upvotes: 1

Views: 79

Answers (2)

cjlallana
cjlallana

Reputation: 636

According to the GQL Reference,

GQL does not have an OR operator. However, it does have an IN operator, which provides a limited form of OR.

Also,

The IN operator compares value of a property to each item in a list. The IN operator is equivalent to many = queries, one for each value, that are ORed together. An entity whose value for the given property equals any of the values in the list can be returned for the query.

So your options are either to use the IN operator, or to execute two different queries and combine the results.

Apart from that, my suggestion would be to use NDB queries instead of GQL, as they are more efficient, flexible and easier to use overall.

Upvotes: 2

Jesse
Jesse

Reputation: 8393

qry = Object.query(ndb.OR(Object.title == 'title',
                          Object.user == 'user'))

Note, this would imply that you have a corresponding index for this query within index.yaml:

- kind: Object
  properties:
  - name: title
    direction: asc
  - name: user
    direction: desc

And the last thing to note, you should not use the kind "Object". I'm assuming this was purely for your question.

Upvotes: 2

Related Questions