0

In the attempt of migrating to the new Datastore mode in Firestore from the former Datastore, and as well migrating from Python 2.7 to 3.7, we're trying to figure out how to query using OR operator!

Formally using the NDB library, there was ndb.AND & ndb.OR yet there is no mentioning of the OR in the new query structure in Python 3

According to the docs, query happens on a few steps, and it is possible to add multiple filters on multiples add_filter steps:

query = client.query(kind='Task')
query.add_filter('done', '=', False)
query.add_filter('priority', '=', 4)

The above basically means:

Task entities that are marked not done AND have a priority of 4

How about the OR operator?

What if one wants to query for a property that has this value OR this value?

Khaled
  • 907
  • 1
  • 8
  • 18
  • Logical OR queries aren't supported by Firestore. – Doug Stevenson Mar 25 '19 at 18:42
  • Interesting, what would be a clever way around this? All I can think of right now is 1) Re-model the property as an Array and add_filter for the values required? 2) Another way might be to create query steps for first value and add fetch results to a list, then start another query steps and append results to the formally created list. Sounds like a bit of a slower process though :/ – Khaled Mar 25 '19 at 23:39
  • Notably, Firebase in Datastore mode migration should be non breaking (right?!) so old code using NDB for "OR", and also "IN" operator, should still work! any notes on that? – Khaled Mar 26 '19 at 00:29
  • 1
    Perform each query individually, then merge the results on the client. That's the standard advice. – Doug Stevenson Mar 26 '19 at 05:58
  • 1
    Potentially of interest: https://stackoverflow.com/questions/47437166/google-datastore-combine-union-multiple-sets-of-entity-results-to-achieve-or-c/47438039#47438039 – Dan Cornilescu Mar 28 '19 at 04:02

0 Answers0