query

require(["esri/rest/query"], function(query) { /* code goes here */ });
Object: esri/rest/query
Since: ArcGIS API for JavaScript 4.19

Executes different types of query operations on a layer. The most common method used is executeQueryJSON(), which executes the query for JSON results as defined in the Query object that is passed as a parameter to the function. executeQueryJSON() returns a Promise that resolves to a FeatureSet, which contains the features in the layer that satisfy the Query.

You can also obtain the number of features that satisfy the query, the extent of features, related features or records associated with a feature, attachments for features or the featureIds of features.

For example, when working with a feature layer of world cities, to obtain a FeatureSet of cities with a population greater than one million people, use the following code:

Method Overview

Name Return Type Summary Object
Promise<Object>

Query information about attachments associated with features from a feature layer specified in the url.

more details
more detailsquery
Promise<Number>

Gets a count of the number of features that satisfy the input query.

more details
more detailsquery
Promise<Object>

Gets the extent of the features that satisfy the input query.

more details
more detailsquery
Promise<Number[]>

Executes a Query against the layer specified in the url.

more details
more detailsquery
Promise<FeatureSet>

Executes a Query against the layer specified in the url.

more details
more detailsquery
Promise<FeatureSet>

Executes a Query against the layer specified in the url.

more details
more detailsquery
Promise<Object>

Executes a RelationshipQuery against the layer or table specified in the url.

more details
more detailsquery

Method Details

executeAttachmentQuery(attachmentQuery, requestOptions){Promise<Object>}

Query information about attachments associated with features from a feature layer specified in the url. It will return an error if the layer's capabilities.data.supportsAttachment is set to false.

Parameters:
attachmentQuery AttachmentQuery autocast
Autocasts from Object

Specifies the attachment parameters for query.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
Type Description
Promise<Object> When resolved, returns an object containing AttachmentInfos grouped by the source feature objectIds.
See also:
executeForCount(query, requestOptions){Promise<Number>}

Gets a count of the number of features that satisfy the input query.

Parameters:
query Query autocast
Autocasts from Object

Specifies the attributes and spatial filter of the query.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
Type Description
Promise<Number> When resolved, the result is the number of features that satisfy the input query.
Example:
require([
  "esri/rest/query/executeForCount", ...
], function(execute, ... ) {
  execute.executeForCount(url, {  // autocasts as new Query()
     where: "POP90_SQMI &lt; 100"
  }).then(function(count){
    console.log(count, " features matched the input query");
  }, function(error){
       console.log(error); // Will print error in console if unsupported layers are used
     });
});
executeForExtent(params, requestOptions){Promise<Object>}

Gets the extent of the features that satisfy the input query. The count of features that satisfy the input query is returned upon resolution as well.

Parameters:
params Query

Specifies the attributes and spatial filter of the query.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
Type Description
Promise<Object> When resolved, returns the extent and count of the features that satisfy the input query. See the object specification table below for details.
Property Type Description
count number The number of features that satisfy the input query.
extent Extent The extent of the features that satisfy the query.
executeForIds(query, requestOptions){Promise<Number[]>}

Executes a Query against the layer specified in the url. The result is an array of the object IDs of features that satisfy the input query.

Parameters:
query Query autocast
Autocasts from Object

Specifies the attributes and spatial filter of the query.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
Type Description
Promise<Number[]> When resolved, the result is an array of object IDs for features that satisfy the input query.
Example:
require([
  "esri/rest/query/executeForIds", ...
], function(execute, ... ) {
  execute.executeForIds(url, {  // autocasts as new Query()
     where: "region = 'Southern California'"
  }).then(function(results){
    console.log(results);  // an array of object IDs
  });
  ...
});
executeQueryJSON(query, requestOptions){Promise<FeatureSet>}

Executes a Query against the layer specified in the url. The result is returned as a FeatureSet, which can be accessed using the .then() method. A FeatureSet contains an array of Graphic features, which can be added to the map. This array will not be populated if no results are found.

Parameters:
query Query autocast
Autocasts from Object

Specifies the attributes and spatial filter of the query.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
Type Description
Promise<FeatureSet> When resolved, a FeatureSet containing an array of graphic features is returned.
Example:
require([
  "esri/tasks/support/Query", "esri/rest/query/executeQueryJSON", ...
], function(Query, execute, ... ) {
  let query = new Query();
  query.where = "STATE_NAME = 'Washington'";
  query.outSpatialReference = { wkid:102100 };
  query.returnGeometry = true;
  query.outFields = [ "CITY_NAME" ];
  execute.executeQueryJSON(url, query).then(function(results){
    // Results.graphics contains the graphics returned from query
  });
  ...
});
executeQueryPBF(query, requestOptions){Promise<FeatureSet>}

Executes a Query against the layer specified in the url. The result is returned as a FeatureSet, which can be accessed using the .then() method. A FeatureSet contains an array of Graphic features, which can be added to the map. This array will not be populated if no results are found.

Parameters:
query Query autocast
Autocasts from Object

Specifies the attributes and spatial filter of the query.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
Type Description
Promise<FeatureSet> When resolved, a FeatureSet containing an array of graphic features is returned.
Example:
require([
  "esri/tasks/support/Query", "esri/rest/query/executeQueryPBF", ...
], function(Query, execute, ... ) {
  let query = new Query();
  query.where = "STATE_NAME = 'Washington'";
  query.outSpatialReference = { wkid:102100 };
  query.returnGeometry = true;
  query.outFields = [ "CITY_NAME" ];
  execute.executeQueryPBF(url, query).then(function(results){
    // Results.graphics contains the graphics returned from query
  });
  ...
});
executeRelationshipQuery(relationshipQuery, requestOptions){Promise<Object>}

Executes a RelationshipQuery against the layer or table specified in the url. If the query is successful, the returned results are FeatureSets grouped by source layer or table objectIds.

Parameters:
relationshipQuery RelationshipQuery autocast
Autocasts from Object

Specifies relationship parameters for querying related features or records from a layer or a table.

requestOptions Object
optional

Additional options to be used for the data request (will override requestOptions defined during construction).

Returns:
Type Description
Promise<Object> When resolved, the results are FeatureSets grouped by source layer or table objectIds. Each FeatureSet contains an array of Graphic features including the values of the fields requested by the user.
Example:
// specify relationship query parameter
const query = new RelationshipQuery({
  outFields: ["*"],
  relationshipId: relationshipId,
  objectIds: [385, 416]
});

// query related features that meet the query parameters
queryTask.executeRelationshipQuery(query).then(function (results) {
  console.log("queryTask results", results);
})
.catch(function (error) {
  console.log("query task error", error);
});