The "getRecordInKeyRange" action returns all records within a range of values. This action is the fastest and most efficient way to retrieve a bounded set of records in index order ascending or descending. When the range contains too many records to retrieve all at once, use a cursor, which makes it easy, fast, and efficient to paginate results. See Most effective ways to return data from the JSON DB API. Returning a cursor and returning records directly are mutually exclusive operations.
The "getRecordInKeyRange" action includes records by matching index keys against values supplied by you, such as first_name > "M" and < "Peter". You can specify a separate range for each field in the index. A value may be a partial key match, such as "M" or a complete match, such as "Peter".
You may leave the range open ended. For example, you may omit the lower part of the range to include all records before an upper limit, or you may omit the upper part of the range to includes all records after the lower limit.
Request examples
Minimal
{
"action": "getRecordsInKeyRange",
"params": {
"tableName": "athlete",
"databaseName": "ctreeSQL",
"indexFilter": {
"indexName": "admin_athlete_id_pk",
"indexFieldFilters": [
{
"fieldName": "id",
"operator": ">=",
"value": ""
}
]
}
},
"authToken": "replaceWithAuthTokenFromCreateSession"
}Cursor
{
"api": "db",
"apiVersion": "1.0",
"requestId": "2",
"action": "getRecordsInKeyRange",
"params": {
"tableName": "athlete",
"indexFilter": {
"indexName": "earnings",
"indexFieldFilters": [
{
"fieldName": "earnings",
"operator": "<",
"value": 2000000
}
]
},
"returnCursor": true
},
"authToken": "replaceWithAuthTokenFromCreateSession"
}Maximal
{
"api": "db",
"apiVersion": "1.0",
"requestId": "3",
"action": "getRecordsInKeyRange",
"params": {
"databaseName": "ctreeSQL",
"ownerName": "admin",
"tableName": "athlete",
"tableFilter": "name < \"W\"",
"indexFilter": {
"indexName": "ranking",
"indexFieldFilters": [
{
"fieldName": "ranking",
"operator": "<=",
"value": 3
}
]
},
"returnCursor": false,
"reverseOrder": false,
"fixedLengthCharFormat": "sql",
"skipRecords": 0,
"maxRecords": 20
},
"responseOptions": {
"includeBookmarks": false,
"binaryFormat": "hex",
"dataFormat": "objects",
"numberFormat": "string",
"variantFormat": "variantObject",
"includeFields": [
"name",
"ranking"
],
"excludeFields": []
},
"debug": "max",
"authToken": "replaceWithAuthTokenFromCreateSession"
}
Response examples
Minimal
{
"result": {
"dataFormat": "objects",
"binaryFormat": "hex",
"fields": [
{
"name": "id",
"type": "bigint",
"length": null,
"scale": null,
"defaultValue": null,
"nullable": false,
"primaryKey": 1,
"autoValue": "incrementOnInsert"
},
{
"name": "changeId",
"type": "bigint",
"length": null,
"scale": null,
"defaultValue": null,
"nullable": true,
"primaryKey": 0,
"autoValue": "changeId"
},
{
"name": "name",
"type": "varchar",
"length": 30,
"scale": null,
"defaultValue": null,
"nullable": true,
"primaryKey": 0,
"autoValue": "none"
},
{
"name": "ranking",
"type": "smallint",
"length": null,
"scale": null,
"defaultValue": null,
"nullable": false,
"primaryKey": 0,
"autoValue": "none"
},
{
"name": "birthDate",
"type": "date",
"length": null,
"scale": null,
"defaultValue": null,
"nullable": true,
"primaryKey": 0,
"autoValue": "none"
},
{
"name": "playerNumber",
"type": "number",
"length": 32,
"scale": 6,
"defaultValue": null,
"nullable": true,
"primaryKey": 0,
"autoValue": "none"
},
{
"name": "livedPast2000",
"type": "bit",
"length": null,
"scale": null,
"defaultValue": null,
"nullable": true,
"primaryKey": 0,
"autoValue": "none"
},
{
"name": "earnings",
"type": "money",
"length": 32,
"scale": 4,
"defaultValue": null,
"nullable": true,
"primaryKey": 0,
"autoValue": "none"
},
{
"name": "favoriteSaying",
"type": "varchar",
"length": 500,
"scale": null,
"defaultValue": null,
"nullable": true,
"primaryKey": 0,
"autoValue": "none"
}
],
"data": [
{
"birthDate": "1963-02-17",
"changeId": 1291366,
"earnings": 1700000000,
"favoriteSaying": "There is no 'i' in team but there is in win.",
"id": 1,
"livedPast2000": true,
"name": "Michael Jordan",
"playerNumber": 23,
"ranking": 1
},
{
"birthDate": "1895-02-06",
"changeId": 1291366,
"earnings": 800000,
"favoriteSaying": "Every strike brings me closer to the next home run.",
"id": 2,
"livedPast2000": false,
"name": "Babe Ruth",
"playerNumber": 3,
"ranking": 2
},
{
"birthDate": "1942-01-17",
"changeId": 1291366,
"earnings": 60000000,
"favoriteSaying": "Float like a butterfly, sting like a bee.",
"id": 3,
"livedPast2000": true,
"name": "Muhammad Ali",
"playerNumber": 1,
"ranking": 3
},
{
"birthDate": "1940-10-23",
"changeId": 1291366,
"earnings": 115000000,
"favoriteSaying": "Everything is practice.",
"id": 4,
"livedPast2000": true,
"name": "Pele",
"playerNumber": 10,
"ranking": 4
},
{
"birthDate": "1961-01-26",
"changeId": 1291366,
"earnings": 1720000,
"favoriteSaying": "You miss 100 percent of the shots you never take.",
"id": 5,
"livedPast2000": true,
"name": "Wayne Gretzky",
"playerNumber": 99,
"ranking": 5
},
{
"birthDate": "1969-01-03",
"changeId": 1291366,
"earnings": 990000000,
"favoriteSaying": "Once something is a passion, the motivation is there.",
"id": 6,
"livedPast2000": true,
"name": "Michael Schumacher",
"playerNumber": 1,
"ranking": 6
}
],
"primaryKeyFields": [
"id"
],
"changeIdField": "changeId",
"moreRecords": false,
"requestedRecordCount": 20,
"returnedRecordCount": 6,
"totalRecordCount": 6
},
"errorCode": 0,
"errorMessage": "",
"authToken": "replaceWithAuthTokenFromCreateSession"
}Cursor
{
"result": {
"cursorId": "cursor",
"totalRecordCount": -1
},
"requestId": "2",
"errorCode": 0,
"errorMessage": "",
"authToken": "replaceWithAuthTokenFromCreateSession"
}Maximal
{
"result": {
"dataFormat": "objects",
"binaryFormat": "hex",
"fields": [
{
"name": "name",
"type": "varchar",
"length": 30,
"scale": null,
"defaultValue": null,
"nullable": true,
"primaryKey": 0,
"autoValue": "none"
},
{
"name": "ranking",
"type": "smallint",
"length": null,
"scale": null,
"defaultValue": null,
"nullable": false,
"primaryKey": 0,
"autoValue": "none"
}
],
"data": [
{
"name": "Michael Jordan",
"ranking": "1"
},
{
"name": "Babe Ruth",
"ranking": "2"
},
{
"name": "Muhammad Ali",
"ranking": "3"
}
],
"primaryKeyFields": [
"id"
],
"changeIdField": "changeId",
"moreRecords": false,
"requestedRecordCount": 20,
"returnedRecordCount": 3,
"totalRecordCount": 3
},
"requestId": "3",
"debugInfo": {
"request": {
"api": "db",
"action": "getRecordsInKeyRange",
"params": {
"databaseName": "ctreeSQL",
"ownerName": "admin",
"tableName": "athlete",
"tableFilter": "name < \"W\"",
"indexFilter": {
"indexName": "ranking",
"indexFieldFilters": [
{
"fieldName": "ranking",
"operator": "<=",
"value": 3
}
]
},
"returnCursor": false,
"reverseOrder": false,
"skipRecords": 0,
"maxRecords": 20
},
"apiVersion": "1.0",
"requestId": "3",
"responseOptions": {
"binaryFormat": "hex",
"dataFormat": "objects",
"numberFormat": "string",
"variantFormat": "variantObject",
"includeFields": [
"name",
"ranking"
],
"excludeFields": []
},
"debug": "max",
"authToken": "replaceWithAuthTokenFromCreateSession"
},
"serverSuppliedValues": {
"databaseName": "ctreeSQL",
"ownerName": "admin"
},
"errorData": {
"errorData": null
},
"warnings": []
},
"errorCode": 0,
"errorMessage": "",
"authToken": "replaceWithAuthTokenFromCreateSession"
}
Properties
Request properties ("params")
| Property | Description | Default | Type | Limits (inclusive) |
|---|---|---|---|---|
databaseName |
The You specify this property when you want to use a different database instead of the default. This property is useful because objects, such as tables and code packages, can have the same name in multiple databases. This feature allows you to create multiple environments in the same server and reuse the same JSON actions in each environment. For example, you can create It is an error to set If no default database is specified during |
Defaults to the session's "defaultDatabaseName" property |
string | 1 to 64 bytes |
fixedLengthCharFormat |
The
For more details, see |
Optional with default "sql"
|
string |
|
indexFilter |
The
One-field index"indexFilter":
{
"indexName": "name_livedpast2000",
"partialKey": "Mi"
}Multi-field index"indexFilter":
{
"indexName": "name_livedpast2000",
"partialKey": [ "2023-09-22", "Mi" ]
} |
Required - No default value |
object |
|
|
indexFilter .indexFieldFilters |
The "indexFieldFilters":
[
{
"fieldName": "name",
"operator": ">=",
"value": "Michael"
},
{
"fieldName": "rank",
"operator": ">=",
"value": "1"
},
{
"fieldName": "rank",
"operator": "<",
"value": "3"
}
] |
Required - No default value | array of objects |
|
|
indexFilter indexFieldFilters .fieldName |
The "fieldName" property specifies the name of a field in a table. |
Required - No default value | string | 1 to 64 bytes |
|
indexFilter indexFieldFilters .operator |
The
The following comparison operators are available:
|
Required - No default value | string |
|
|
indexFilter indexFieldFilters .value |
The
In Key-Value actions, the required |
Required - No default value | string |
|
|
indexFilter .indexName |
The "params": {
"indexName": "index1"
} |
Required - No default value |
string | 1 to 64 bytes |
maxRecords |
The "maxRecords" property specifies the maximum number of records to be returned. It is used with "skipRecords" to paginate the results. If the value is not null or omitted, the server returns the maximum number of results specified by "maxRecords". |
Optional with default of 20 |
integer |
-1 to 65535
|
ownerName |
The You specify this property when you want to use a different account instead of the default. Your session's account must have the appropriate privileges to access the code package. This property is useful because objects, such as tables and code packages, can have the same name in the same database as long as different accounts own each object. This feature allows you to create duplicate objects for different users on the same server and reuse the same JSON actions on those objects. For example, an administrator can copy objects from a production environment to her account so she can troubleshoot an issue using the same JSON actions, JavaScript, and SQL code. It is an error to set If no default owner is specified during |
Optional with default of the session's "defaultOwnerName" property |
string | 1 to 64 bytes |
returnCursor |
The
|
Optional with default of false
|
Boolean |
|
reverseOrder |
The
|
Optional with default of false
|
Boolean |
|
skipRecords |
The "skipRecords" property specifies the number of records to skip over when paginating the results. It is used with "maxRecords" to paginate the results. If the value is not null or omitted, the server returns results from the beginning. If it is > 0, the server skips over the specified number of records and returns results starting from that point up until it returns the maximum number of results as defined by "maxRecords". |
Optional with default of 0
|
integer |
0 to 9223372036854775807
|
tableFilter |
The
|
Optional with default of ""
|
string | 0 to unlimited bytes |
tableName |
The See table name in System specifications for the table naming requirements and limitations.
"params": {
"tableName": "ctreeTable"
} |
Required - No default value | string | 1 to 64 bytes |
Response properties ("result")
| Property | Description | Type | Limits (inclusive) |
|---|---|---|---|
binaryFormat |
The "binaryFormat" property designates the format of binary values embedded in JSON strings. See binaryFormat for more details. |
string | One of the following: "base64", "hex", or "byteArray". |
changeIdField |
This property's value designates the name of the field used for change-tracking functionality if you are not using the
However, if you use the name |
string | 1 to 64 bytes |
cursorid |
The
|
string | 0 to 255 bytes |
data |
The |
array of objects | The action determines its contents. |
dataFormat |
The
|
string |
|
fields |
The
"fields": [
{
"autoValue": "none",
"name": "name",
"type": "varchar",
"length": 50,
"scale": null,
"defaultValue": null,
"nullable": false
}
] |
array |
updateRecords and deleteRecords only:
|
|
fields .autoValue |
The "autoValue" property indicates when and how the server automatically sets the field value. See autoValue for more details. |
string |
Some actions only:
|
|
fields .defaultValue |
The "defaultValue" property specifies the default value of a field. |
string | 0 to 65,500 bytes |
|
fields .length |
Identifies the length of the field. | integer |
1 to 65500 |
|
fields .name |
The The The
|
string | 1 to 64 bytes |
|
fields .nullable |
"nullable" identifies whether a field can contain a NULL value. |
Boolean |
|
|
fields .primaryKey |
When > 0, the "primaryKey" property identifies the ordinal position of the field within the table's primary key. |
integer |
0 to 32
|
|
fields .scale |
If the type is "number" or "money", the "scale" property identifies the number of places to the right of the decimal point,. |
integer |
0 to 32
|
|
fields .type |
Identifies the type of the field. See Data types. | string |
|
moreRecords |
The "moreRecords" property indicates if there are more records that match the filters in the request. |
Boolean |
|
primaryKeyFields |
This property specifies the fields of the table’s primary key when multiple fields are combined to form the primary key.
The order of fields in this property is the order of fields in the primary key index. The A primary key with multiple fields has an index named If only one field is used as the primary key, the
Example "primaryKeyFields": [
"a",
"b",
"c"
],
"fields": [
{
"name": "a",
"type": "tinyint"
},
{
"name": "b",
"type": "smallint"
},
{
"name": "c",
"type": "integer"
}
] |
array | an array |
requestedRecordCount |
The
|
integer |
|
returnedRecordCount |
The
|
integer |
|
totalRecordCount |
The
|
integer |
|
Additional information
Important The index is required and is used to set an upper and/or lower limit on the records that can be retrieved. Records are retrieved in index order.
- Each range has one of the following:
- a lower bound
- an upper bound
- both lower and upper bounds
- no bounds
- Results can be filtered further using a table filter expression (which filters records using any field in the table, not just the fields in the index). A table filter is applied after a record is retrieved from the index; the index filter primarily controls which records are returned and the table filter subsequently filters the records.
- You may omit the index filter criteria to return all indexed records. This is useful when retrieving all indexed records in index order.
- An index may not contain all records in a table. Its setting may prevent it from indexing records with null key values or index only those records that match a conditional expression. Thus, an index itself may be all the filter you need.
- Results are returned in ascending or descending index order and are formatted as JSON or an array of fields.
- You can specify which fields and JSON properties are included in the results.
- Results are returned quickly when the index range returns few records.
- Results return less quickly when:
- The
"indexFilter"property creates a large range of records. - The
"skipRecords"property skips over many records. - The
"tableFilter"property further filters the results.
- The
How to use "getRecordsinKeyRange"
- Set the
"tableName"property to specify which table's records to return. - Set
"includeFields"to the empty array to include all tables fields or omit the entire property. - Set
"includePaths"to the empty array to include all JSON property paths or omit the entire property. - Set
"skipRecords"to0to include the table’s first record, or omit the entire property. - Set
"maxRecords"to-1to include all records in the table, or omit the entire property. - Set
"tableFilter"to an empty string to not filter records using any of the fields in the table, or omit the entire property. - Set
"reverseOrder"totrueto return results in reverse order. Set to false or omit the property to return results in normal order. - Set
"indexFieldFilters"to the empty array to return all indexed records, or omit the entire property.
- Set the
"tableName"property to specify which table’s records to return. - Set
"includeFields"to an array containing the names of table fields you want to include; an empty array includes all fields. Or set"excludeFields"to an array containing the names of fields you want to exclude. You cannot put field names in both"includeFields"and"excludeFields"at the same time. - Set
"includePaths"to an array containing the JSON property paths you want to include; an empty array includes all JSON. This is used when a table field contains a JSON document and you want to filter the properties in it. Or set"excludePaths"to an array containing the paths of JSON properties you want to exclude. You cannot put paths in both"includePaths"and"excludePaths"at the same time. - Set
"skipRecords"to a positive or negative value to skip forward or backward the specified number records before returning records. - Set
"maxRecords"to a value >0to include that many records in the results. - Set
"tableFilter"to an empty string to not filter records using any of the fields in the table. - Set
"reverseOrder"totrueto return results in reverse order. Set tofalseor omit the property to return results in normal order. - Set
"indexFieldFilters"to an array of range objects to filter the results. A range object includes a field name, comparison operator, and a comparison value for the following reasons:
- To match all values in a field, omit a range for a field.
- To set a range between two values, assign two range objects to the same field: one for the lower limit and another for the upper limit.
- To set only an upper limit, assign one range object to a field using a < or <= operator.
- To set only a lower limit, assign one range object to a field using a > or >= operator.
- To match a specific value in a field, assign one range object to the field using the = operator.