An object that provides iterator-like access to the results of a database query. Use the methods
hasNext()
and next()
in a loop to access all the results.
var db = ScriptDb.getMyDb();
var results = db.query({type: 'person'});
while (results.hasNext()) {
var item = results.next();
// Do something with the item.
}
Methods
Method | Return type | Brief description |
---|---|---|
getSize() | Integer | Returns the total number of items in the query results. |
hasNext() | Boolean | Returns true if there are more items left in the query results. |
limit(number) | ScriptDbResult | Limits the number of items in the query results. |
next() | ScriptDbMap | Returns the next item in the query results. |
paginate(pageNumber, pageSize) | ScriptDbResult | Limits the query results to a single page of items, using the passed in page number and size. |
sortBy(fieldPath) | ScriptDbResult | Sorts the query results by the specified field, lexically and ascending. |
sortBy(fieldPath, direction) | ScriptDbResult | Sorts the query results by the specified field, lexically and using the passed in direction. |
sortBy(fieldPath, direction, strategy) | ScriptDbResult | Sorts the query results by the specified field, using the passed in direction and strategy. |
sortBy(fieldPath, strategy) | ScriptDbResult | Sorts the query results by the specified field, ascending and using the passed in strategy. |
startAt(number) | ScriptDbResult | Starts the query results with the item at the passed in index. |
Detailed documentation
getSize()
Returns the total number of items in the query results.
var db = ScriptDb.getMyDb();
var results = db.query({type: 'person'});
Logger.log('Number of people: ' + results.getSize());
Please note that after calling this method you will no longer be able to adjust the sorting,
paging, startAt, or limit settings of the results.Return
Integer
— the total number of items in the query results
hasNext()
Returns true if there are more items left in the query results.
var db = ScriptDb.getMyDb();
var results = db.query({type: 'person'});
while (results.hasNext()) {
var item = results.next();
// Do something with the item.
}
Please note that after calling this method you will no longer be able to adjust the sorting,
paging, startAt, or limit settings of the results.Return
Boolean
— true if there are more items left in the query results, false if all items have been
retrieved
limit(number)
Limits the number of items in the query results.
var db = ScriptDb.getMyDb();
// Get the first ten people.
var results = db.query({type: 'person'}).limit(10);
while (results.hasNext()) {
var item = results.next();
// Do something with the item.
}
The default limit is 200 and the maximum limit allowed is 50,000.Parameters
Name | Type | Description |
---|---|---|
number | Integer | the maximum number of items to return |
Return
ScriptDbResult
— the results, for chaining
next()
Returns the next item in the query results. Once this method called it's not possible to go back to a previous item.
var db = ScriptDb.getMyDb();
var results = db.query({type: 'person'});
while (results.hasNext()) {
var item = results.next();
// Do something with the item.
}
Please note that after calling this method you will no longer be able to adjust the sorting,
paging, startAt, or limit settings of the results.Return
ScriptDbMap
— the next item in the query results
paginate(pageNumber, pageSize)
Limits the query results to a single page of items, using the passed in page number and size. Please note that page numbers start at zero.
var db = ScriptDb.getMyDb();
var pageNumber = 0;
var pageSize = 10;
var page;
do {
page = db.query({type: 'person'}).paginate(pageNumber, pageSize);
var items = [];
while (page.hasNext()) {
items.push(page.next());
}
// Do something with the page of items.
pageNumber++;
} while (page.getSize() == pageSize);
Parameters
Name | Type | Description |
---|---|---|
pageNumber | Integer | the page of results to return |
pageSize | Integer | the number of items per page |
Return
ScriptDbResult
— the results, for chaining
sortBy(fieldPath)
Sorts the query results by the specified field, lexically and ascending. To sort by an inner field specify the dot-separated path to the field (for example "business.address.city").
var db = ScriptDb.getMyDb();
// Get people sorted by name.
var results = db.query({type: 'person'}).sortBy('name');
while (results.hasNext()) {
var item = results.next();
// Do something with the item.
}
Please note, sorting by multiple fields is not supported, and only the last call to sortBy will
be used.Parameters
Name | Type | Description |
---|---|---|
fieldPath | String | the name or path of the field to sort by |
Return
ScriptDbResult
— the results, for chaining
sortBy(fieldPath, direction)
Sorts the query results by the specified field, lexically and using the passed in direction.
var db = ScriptDb.getMyDb();
// Get people sorted by name, in reverse alphabetical order.
var results = db.query({type: 'person'}).sortBy('name', db.DESCENDING);
while (results.hasNext()) {
var item = results.next();
// Do something with the item.
}
Parameters
Name | Type | Description |
---|---|---|
fieldPath | String | the name or path of the field to sort by |
direction | SortDirection | the direction (ascending or descending) of the sort |
Return
ScriptDbResult
— the results, for chaining
sortBy(fieldPath, direction, strategy)
Sorts the query results by the specified field, using the passed in direction and strategy.
var db = ScriptDb.getMyDb();
// Get people sorted by age, oldest to youngest.
var results = db.query({type: 'person'}).sortBy('age', db.DESCENDING, db.NUMERIC);
while (results.hasNext()) {
var item = results.next();
// Do something with the item.
}
Parameters
Name | Type | Description |
---|---|---|
fieldPath | String | the name or path of the field to sort by |
direction | SortDirection | the direction (ascending or descending) of the sort |
strategy | SortStrategy | the strategy used (lexical or numeric) to order the values of the field |
Return
ScriptDbResult
— the results, for chaining
sortBy(fieldPath, strategy)
Sorts the query results by the specified field, ascending and using the passed in strategy.
var db = ScriptDb.getMyDb();
// Get people sorted by age.
var results = db.query({type: 'person'}).sortBy('age', db.NUMERIC);
while (results.hasNext()) {
var item = results.next();
// Do something with the item.
}
Parameters
Name | Type | Description |
---|---|---|
fieldPath | String | the name or path of the field to sort by |
strategy | SortStrategy | the strategy used (lexical or numeric) to order the values of the field |
Return
ScriptDbResult
— the results, for chaining
startAt(number)
Starts the query results with the item at the passed in index.
var db = ScriptDb.getMyDb();
// Get all but the first ten people.
var results = db.query({type: 'person'}).startAt(10);
while (results.hasNext()) {
var item = results.next();
// Do something with the item.
}
Parameters
Name | Type | Description |
---|---|---|
number | Integer | the index to start the results at |
Return
ScriptDbResult
— the results, for chaining