Search code examples
google-app-engineapp-engine-ndbgoogle-cloud-datastore

multiple filters vs OR , ndb query


What is the difference between these queries:

  1. With consequent filters:

    qry1 = Account.query() # Retrieve all Account entitites
    qry2 = qry1.filter(Account.userid >= 40) # Filter on userid >= 40
    qry3 = qry2.filter(Account.userid < 50) # Filter on userid < 50 as well
    
  2. Using ndb.OR:

    qry = Article.query(ndb.OR(Account.userid >= 40,
                           Account.userid < 50))
    
  3. Using ndb.AND:

    qry = Article.query(ndb.AND(Account.userid >= 40,
                            Account.userid < 50))
    

Solution

  • The first query does an AND. Thus, only the entities that match both inequalities will be returned by the query. The second query does an OR. Thus, entities that match either of the filters will be returned. For more information about ndb queries, take a look at NDB Queries.