Optional
scope: Datastore | TransactionOptional
kinds: null | string[]Optional
scope: Datastore | TransactionOptional
namespace: null | stringOptional
kinds: string[]Optional
namespaceOptional
scopeSet an ending cursor to a query.
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
const companyQuery = datastore.createQuery('Company');
const cursorToken = 'X';
// Retrieve results limited to the extent of cursorToken.
const endQuery = companyQuery.end(cursorToken);
Datastore allows querying on properties. Supported comparison operators
are =
, <
, >
, <=
, >=
, !=
, HAS_ANCESTOR
, IN
and NOT_IN
.
To filter by ancestors, see {module:datastore/query#hasAncestor}.
The field name.
Value to compare property to.
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
const query = datastore.createQuery('Company');
//-
// List all companies that are located in California.
//-
const caliQuery = query.filter('state', 'CA');
//-
// List all companies named Google that have less than 400 employees.
//-
const companyQuery = query
.filter('name', 'Google')
.filter('size', '<', 400);
//-
// To filter by key, use `__key__` for the property name. Filter on keys
// stored as properties is not currently supported.
//-
const key = datastore.key(['Company', 'Google']);
const keyQuery = query.filter('__key__', key);
Group query results by a list of properties.
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
const companyQuery = datastore.createQuery('Company');
const groupedQuery = companyQuery.groupBy(['name', 'size']);
Filter a query by ancestors.
Key object to filter by.
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
const query = datastore.createQuery('MyKind');
const ancestoryQuery = query.hasAncestor(datastore.key(['Parent', 123]));
Set a limit on a query.
The number of results to limit the query to.
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
const companyQuery = datastore.createQuery('Company');
// Limit the results to 10 entities.
const limitQuery = companyQuery.limit(10);
Set an offset on a query.
The offset to start from after the start cursor.
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
const companyQuery = datastore.createQuery('Company');
// Start from the 101st result.
const offsetQuery = companyQuery.offset(100);
Sort the results by a property name in ascending or descending order. By default, an ascending sort order will be used.
The property to order by.
Optional
options: OrderOptionsOptions object.
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
const companyQuery = datastore.createQuery('Company');
// Sort by size ascendingly.
const companiesAscending = companyQuery.order('size');
// Sort by size descendingly.
const companiesDescending = companyQuery.order('size', {
descending: true
});
Run the query.
Optional
options: RunQueryOptionsOptional configuration.
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
const query = datastore.createQuery('Company');
query.run((err, entities, info) => {
// entities = An array of records.
// Access the Key object for an entity.
const firstEntityKey = entities[0][datastore.KEY];
});
//-
// A keys-only query returns just the keys of the result entities instead
of
// the entities themselves, at lower latency and cost.
//-
query.select('__key__');
query.run((err, entities) => {
const keys = entities.map((entity) => {
return entity[datastore.KEY];
});
});
//-
// If the callback is omitted, we'll return a Promise.
//-
query.run().then((data) => {
const entities = data[0];
});
Run the query as a readable object stream.
Optional
options: RunQueryOptionsOptional configuration. See run for a complete list of options.
Query#runStream
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
const query = datastore.createQuery('Company');
query.runStream()
.on('error', console.error)
.on('data', function (entity) {
// Access the Key object for this entity.
const key = entity[datastore.KEY];
})
.on('info', (info) => {})
.on('end', () => {
// All entities retrieved.
});
//-
// If you anticipate many results, you can end a stream early to prevent
// unnecessary processing and API requests.
//-
query.runStream()
.on('data', function (entity) {
this.end();
});
Retrieve only select properties from the matched entities.
Queries that select a subset of properties are called Projection Queries.
Properties to return from the matched entities.
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
const companyQuery = datastore.createQuery('Company');
// Only retrieve the name property.
const selectQuery = companyQuery.select('name');
// Only retrieve the name and size properties.
const selectQuery = companyQuery.select(['name', 'size']);
Set a starting cursor to a query.
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();
const companyQuery = datastore.createQuery('Company');
const cursorToken = 'X';
// Retrieve results starting from cursorToken.
const startQuery = companyQuery.start(cursorToken);
Generated using TypeDoc
Build a Query object.
Queries are built with {module:datastore#createQuery} and createQuery.
See
Datastore Queries
Param
The parent scope the query was created from.
Param
Namespace to query entities from.
Param
Kind to query.
Example