Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Datastore filter with OR condition

I am working with NodeJS on Google App Engine with the Datastore database.

I am using composite query filter and just need a basic "OR" condition.

Example: Query Tasks that have Done = false OR priority = 4

const query = datastore.createQuery('Task')
  .filter('done', '=', false) //How to make this an OR condition?
  .filter('priority', '=', 4);

However, according to the documentation:

Cloud Datastore currently only natively supports combining filters with the AND operator.

What is a good way to achieve a basic OR condition without running two entirely separate queries and then combining the results?

UPDATE

I have my solution described in detail here in my other post. Any feedback for improvements to the solution would be appreciated since I'm still learning NodeJS.

like image 913
pengz Avatar asked Nov 21 '17 19:11

pengz


People also ask

Is Google Datastore deprecated?

Because Cloud Datastore API v1 is released, Cloud Datastore API v1beta3 is now deprecated.

What is ancestor in Datastore?

For highly related or hierarchical data, Datastore allows entities to be stored in a parent/child relationship. This is known as an entity group or ancestor/descendent relationship. This is an example of an entity group with kinds of types person, pet, and toy. The 'Grandparent' in this relationship is the 'Person'.

How do I update entity in Datastore?

Updating entities To update an existing entity, modify the attributes of the Entity object, then pass it to the DatastoreService. put() method. The object data overwrites the existing entity. The entire object is sent to Datastore with every call to put() .


1 Answers

Not currently possible to achieve a query with an OR condition - this is what the note you quoted means.

Some client libraries provide some (limited) support for OR-like operations. From Restrictions on queries:

The nature of the index query mechanism imposes certain restrictions on what a query can do. Cloud Datastore queries do not support substring matches, case-insensitive matches, or so-called full-text search. The NOT, OR, and != operators are not natively supported, but some client libraries may add support on top of Cloud Datastore.

But AFAIK no such library is available for NodeJS.

If you only have a need for a few specific such queries one possible approach would be to compute (at the time of writing the entities) an additional property with the desired result for such query and use equality queries on that property instead.

For example, assuming you'd like a query with OR-ing the equivalents of these filters:

  • .filter('status', '=', 'queued')
  • .filter('status', '=', 'running')

You could compute a property like not_done every time status changes and set it to true if status is either queued or running and false otherwise. Then you can use .filter('not_done', '=', true) which would have the same semantics. Granted, it's not convenient, but it may get you past the hurdle.

like image 139
Dan Cornilescu Avatar answered Oct 13 '22 19:10

Dan Cornilescu