Query
(
client
,
kind
=
None
,
project
=
None
,
namespace
=
None
,
ancestor
=
None
,
filters
=
(),
projection
=
(),
order
=
(),
distinct_on
=
(),
)
A Query against the Cloud Datastore.
This class serves as an abstraction for creating a query over data stored in the Cloud Datastore.
Parameters
client
kind
str
The kind to query.
project
str
(Optional) The project associated with the query. If not passed, uses the client's value.
namespace
str
(Optional) The namespace to which to restrict results. If not passed, uses the client's value.
ancestor
filters
tuple[str, str, str]
Property filters applied by this query. The sequence is (property_name, operator, value)
.
projection
sequence of string
fields returned as part of query results.
order
sequence of string
field names used to order query results. Prepend -
to a field name to sort it in descending order.
distinct_on
sequence of string
field names used to group query results.
Properties
ancestor
The ancestor key for the query.
distinct_on
Names of fields used to group query results.
sequence of string
filters
Filters set on the query.
tuple[str, str, str]
(property_name, operator, value)
.kind
Get the Kind of the Query.
str
namespace
This query's namespace
str or None
order
Names of fields used to sort query results.
sequence of string
project
Get the project for this Query.
str
projection
Fields names returned by the query.
sequence of string
Methods
add_filter
add_filter
(
property_name
,
operator
,
value
)
Filter the query based on a property name, operator and a value.
Expressions take the form of::
.add_filter('
where property is a property stored on the entity in the datastore
and operator is one of OPERATORS
(ie, =
, <
, <=
, >
, >=
)::
from google.cloud import datastore client = datastore.Client() query = client.query(kind='Person') query = query.add_filter('name', '=', 'James') query = query.add_filter('age', '>', 50)
property_name
str
A property name.
operator
str
One of =
, <
, <=
, >
, >=
.
value
`ValueError
operation
is not one of the specified values, or if a filter names '__key__'
but passes an invalid value (a key is required).fetch
fetch
(
limit
=
None
,
offset
=
0
,
start_cursor
=
None
,
end_cursor
=
None
,
client
=
None
,
eventual
=
False
,
)
Execute the Query; return an iterator for the matching entities.
For example::
from google.cloud import datastore client = datastore.Client() query = client.query(kind='Person') result = query.add_filter('name', '=', 'Sally').fetch() list(result) [
limit
int
(Optional) limit passed through to the iterator.
offset
int
(Optional) offset passed through to the iterator.
start_cursor
bytes
(Optional) cursor passed through to the iterator.
end_cursor
bytes
(Optional) cursor passed through to the iterator.
client
eventual
bool
(Optional) Defaults to strongly consistent (False). Setting True will use eventual consistency, but cannot be used inside a transaction or will raise ValueError.
Iterator
key_filter
key_filter
(
key
,
operator
=
"="
)
Filter on a key.
key
operator
str
(Optional) One of =
, <
, <=
, >
, >=
. Defaults to =
.
keys_only
keys_only
()
Set the projection to include only keys.