Deprecated. ScriptDB was deprecated on May 15, 2014, and will be turned off on November 20, 2014. To move your data to a different type of database, see the guide to migrating from ScriptDB.
An object that provides iterator-like access to the results of a database query. Use the methods
and hasNext()
in a loop to access all the results.
next()
var db = ScriptDb.getMyDb();
var results = db.query({type: 'person'});
while (results.hasNext()) {
var item = results.next();
// Do something with the item.
}
Deprecated methods
Method | Return type | Brief description |
---|---|---|
| Integer | Returns the total number of items in the query results. |
| Boolean | Returns true if there are more items left in the query results. |
|
| Limits the number of items in the query results. |
|
| Returns the next item in the query results. |
|
| Limits the query results to a single page of items, using the passed in page number and size. |
|
| Sorts the query results by the specified field, lexically and ascending. |
|
| Sorts the query results by the specified field, lexically and using the passed in direction. |
|
| Sorts the query results by the specified field, using the passed in direction and strategy. |
|
| Sorts the query results by the specified field, ascending and using the passed in strategy. |
|
| Starts the query results with the item at the passed in index. |
Deprecated methods
getSize()
getSize()
Deprecated. This function is deprecated and should not be used in new scripts.
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()
hasNext()
Deprecated. This function is deprecated and should not be used in new scripts.
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)
limit(number)
Deprecated. This function is deprecated and should not be used in new scripts.
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
— the results, for chainingScriptDbResult
next()
next()
Deprecated. This function is deprecated and should not be used in new scripts.
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
— the next item in the query resultsScriptDbMap
paginate(pageNumber, pageSize)
paginate(pageNumber, pageSize)
Deprecated. This function is deprecated and should not be used in new scripts.
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
— the results, for chainingScriptDbResult
sortBy(fieldPath)
sortBy(fieldPath)
Deprecated. This function is deprecated and should not be used in new scripts.
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
— the results, for chainingScriptDbResult
sortBy(fieldPath, direction)
sortBy(fieldPath, direction)
Deprecated. This function is deprecated and should not be used in new scripts.
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 |
| the direction (ascending or descending) of the sort |
Return
— the results, for chainingScriptDbResult
sortBy(fieldPath, direction, strategy)
sortBy(fieldPath, direction, strategy)
Deprecated. This function is deprecated and should not be used in new scripts.
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 |
| the direction (ascending or descending) of the sort |
strategy |
| the strategy used (lexical or numeric) to order the values of the field |
Return
— the results, for chainingScriptDbResult
sortBy(fieldPath, strategy)
sortBy(fieldPath, strategy)
Deprecated. This function is deprecated and should not be used in new scripts.
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 |
| the strategy used (lexical or numeric) to order the values of the field |
Return
— the results, for chainingScriptDbResult
startAt(number)
startAt(number)
Deprecated. This function is deprecated and should not be used in new scripts.
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
— the results, for chainingScriptDbResult