gcloud datastore: Can I filter with IN or Contains

2019-08-24 20:36发布

问题:

I am a new bee with the Datastore gCloud. And I want to filter in an entity called Score all the scores that have relation with a list of companies. My entity is formed as follows:

{
    "company_id": 1,
    "score": 100, 
}

I have several entities with different company IDs. I tried to filter using the query.add_filter command but got the error ValueError:

('Invalid expression: "IN"', 'Please use one of: =, <, <=,>,> =.')

The reason for the error is very clear to me, but I have not found anything in the documentation on how to run filters with IN or CONTAINS, in addition to types other than those listed in the above error message.

回答1:

The filters operators you seek are not supported by the datastore. The only supported ones are listed in Filters:

The comparison operator can be any of the following:

Operator                  Meaning
EQUAL                     Equal to
LESS_THAN                 Less than
LESS_THAN_OR_EQUAL        Less than or equal to
GREATER_THAN              Greater than
GREATER_THAN_OR_EQUAL     Greater than or equal to

Some of the client libraries may offer some equivalents, but with limitations. For example the standard environment GAE-specific ndb library (not usable in your context) offers such support. The example from The != and IN Operations can be useful as you can implement it in a similar manner in your code:

Similarly, the IN operation

property IN [value1, value2, ...]

which tests for membership in a list of possible values, is implemented as

(property == value1) OR (property == value2) OR ...