Cloud DB uses the query() method to query objects and provides various query predicates, such as equalTo(), notEqualTo(), and in(). By using one or more chain query conditions, you can query objects that meet the specific query conditions from a Cloud DB zone, or sort query results or limit the number of returned query results by using the corresponding predicates. For details about query conditions, please refer to DatabaseQuery.
The app directly queries data from the server in the Cloud DB zone on the cloud and does not cache data locally.
When calling the data query method, there are two ways to return the result: either by returning a Promise object or by passing a callback object in the parameters. The following example explains the Promise approach in detail.
Precautions:
You can query the data of only one object type at a time.
You can obtain all objects of an object type when no query conditions are set. You can also specify a query condition to obtain desired objects.
async queryAll() {
try {
let resultArray = await databaseZone.query(condition);
console.info(`Succeeded in querying , result: ` + JSON.stringify(resultArray));
} catch (err) {
console.error(`Failed to query , Code: ${err.code}, message: ${err.message}`);
}
}async queryOneBook() {
try {
condition.equalTo('bookName', 'A Tale of Two Cities');
let resultArray = await databaseZone.query(condition);
console.info(`Succeeded in querying , result: ` + JSON.stringify(resultArray));
} catch (err) {
console.error(`Failed to query , Code: ${err.code}, message: ${err.message}`);
}
}You can use multiple chain query conditions to obtain desired objects. By default, multiple chain conditions are connected using the AND operation.
condition.contains('bookName', 'Database')
.greaterThan('price', 20.0)
.and()
.lessThan('price', 50.0);
let resultArray = await databaseZone.query(condition);condition.contains('bookName', 'Database')
.lessThan('price', 20.0)
.or()
.greaterThan('price', 50.0);
let resultArray = await databaseZone.query(condition);condition.contains('bookName', 'A Tale of Two Cities')
.equalTo('author', 'Charles Dickens')
.greaterThan('price', 60.0);
let resultArray = await databaseZone.query(condition);condition.contains('bookName', 'Autobiography')
.beginGroup()
.equalTo('author', 'William Shakespeare')
.or()
.equalTo('author', 'Charles Dickens')
.endGroup()
.greaterThan('price', 60.0);
let resultArray = await databaseZone.query(condition);You can use orderByAsc() or orderByDesc() to sort the objects in the query result set in ascending or descending order by a certain field. The sorting predicate must be placed after other query predicates and before the predicate that limits the number of returned query results.
condition.lessThan('price', 50.0)
.orderByDesc('price');
let resultArray = await databaseZone.query(condition); During data query, you can use limit() to limit the start position and number of query results to be returned, implementing data pagination. For example, use it together with the sorting query predicate to obtain top N data records.
To limit the number of returned query results, the limiting predicate should be placed after all other query predicates.
condition.lessThan('price', 50.0)
.limit(10);
let resultArray = await databaseZone.query(condition);condition.lessThan('price', 50.0)
.orderByDesc('price')
.limit(10, 6);
let resultArray = await databaseZone.query(condition);When querying data, you can use calculateQuery() to perform arithmetic calculation on a field in the query result object and return the calculation result.
Construct query conditions and call the calculateQuery() method to query all books cheaper than 50.0 and calculate the average price of these books.
async calculateQuery() {
try {
condition.lessThan('price', 50.0);
let resultNum = await databaseZone.calculateQuery(condition, 'price', cloudDatabase.QueryCalculate.AVERAGE);
console.info(`Succeeded in calculateQuerying , result: ` + JSON.stringify(resultNum));
} catch (err) {
console.error(`Failed to calculateQuery , Code: ${err.code}, message: ${err.message}`);
}
}