We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
GuidesApplication ServicesCloud Foundation KitCloud DBQuerying Data

Querying Data

Cloud DB uses the query() method to query objects and provides various predicate query methods, such as equalTo(), notEqualTo(), and in(). By using one or more chained predicates, 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 Cloud DB zone server and does not cache data locally.

NOTE
  • You can query the data of only one object type at a time.

  • 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.

Constraints

This feature supports phones and tablets by default. Starting from version 5.1.0(18), support for wearables is added. From version 5.1.1(19), TVs are also supported. From version 6.1.0(23), PCs/2-in-1 devices are supported as well.

Prerequisites

You have initialized database access.

Simple Query

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.

  1. Import required modules.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { cloudDatabase } from '@kit.CloudFoundationKit';
    2. import { BookInfo } from '../model/BookInfo';
    3. import { hilog } from '@kit.PerformanceAnalysisKit';
    4. import { BusinessError } from '@kit.BasicServicesKit';
  2. Query all data of the BookInfo object type.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
    2. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
    3. resultArray.forEach((value) => {
    4. hilog.info(0x0000, 'cloudDb',
    5. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
    6. });
    7. }).catch((err: BusinessError) => {
    8. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
    9. });

Condition-based Query

You can use multiple chained predicates to obtain desired objects. By default, multiple chained predicates are connected using the AND operation.

  1. Import required modules.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { cloudDatabase } from '@kit.CloudFoundationKit';
    2. import { BookInfo } from '../model/BookInfo';
    3. import { hilog } from '@kit.PerformanceAnalysisKit';
    4. import { BusinessError } from '@kit.BasicServicesKit';
  2. Query data based on specified conditions.

    • Construct query conditions: bookName is Jane Eyre. Then call the query() method to query books.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.equalTo('bookName', 'Jane Eyre');
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: bookName is not Jane Eyre. Then call the query() method to query books.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.notEqualTo('bookName', 'Jane Eyre');
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: bookName starts with Jane. Then call the query() method to query books.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.beginsWith('bookName', 'Jane');
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: bookName ends with Eyre. Then call the query() method to query books.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.endsWith('bookName', 'Eyre');
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: bookName contains d. Then call the query() method to query books.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.contains('bookName', 'd');
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: price is greater than 82. Then call the query() method to query books.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.greaterThan('price', 82.0);
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: price is lower than 82. Then call the query() method to query books.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.lessThan('price', 82.0);
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: price is greater than or equal to 82. Then call the query() method to query books.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.greaterThanOrEqualTo('price', 82.0);
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: price is lower than or equal to 82. Then call the query() method to query books.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.lessThanOrEqualTo('price', 82.0);
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: bookName is contained in ['demo', 'Jane Eyre']. Then call the query() method to query books.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.in('bookName', ['demo', 'Jane Eyre']);
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: price is empty. Then call the query() method to query books.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.isNull('price');
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: price is not empty. Then call the query() method to query books.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.isNotNull('price');
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: price is greater than 10 and lower than 50. Then call the query() method to query books.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.beginGroup()
      3. .greaterThan('price', 10.0)
      4. .lessThan('price', 50.0)
      5. .endGroup();
      6. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      7. resultArray.forEach((value) => {
      8. hilog.info(0x0000, 'cloudDb',
      9. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      10. });
      11. }).catch((err: BusinessError) => {
      12. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      13. });
    • Call the query() method to query books sorted by price in ascending order.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.orderByAsc('price');
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: price is lower than or equal to 50. Then call the query() method to query books sorted by price in descending order.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.lessThan('price', 50.0).orderByDesc('price');
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: price is lower than 50 or bookName is demo. Then call the query() method to query books.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.lessThan('price', 50.0).or().equalTo('bookName', 'demo');
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: price is greater than 40 and bookName contains j. Then call the query() method to query books.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.greaterThan('price', 40.0).and().contains('bookName', 'j');
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });
    • Construct query conditions: price is lower than or equal to 50. Then call the query() method to query books sorted by price in descending order, with a maximum of two results returned.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
      2. condition.lessThan('price', 50.0).orderByDesc('price').limit(2, 0);
      3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
      4. resultArray.forEach((value) => {
      5. hilog.info(0x0000, 'cloudDb',
      6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
      7. });
      8. }).catch((err: BusinessError) => {
      9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
      10. });

Performing Calculations on the Query Result

  1. Import required modules.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { cloudDatabase } from '@kit.CloudFoundationKit';
    2. import { BookInfo } from '../model/BookInfo';
    3. import { hilog } from '@kit.PerformanceAnalysisKit';
    4. import { BusinessError } from '@kit.BasicServicesKit';
  2. You can use calculateQuery() to perform arithmetic calculations on a field in the query result object and return the calculation result.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
    2. condition.lessThan('price', 50.0);
    3. databaseZone.calculateQuery(condition, 'price', cloudDatabase.QueryCalculate.AVERAGE).then((num: number) => {
    4. hilog.info(0x0000, 'cloudDb', `Succeeded in querying: QueryCalculate.AVERAGE price = ${num}`);
    5. }).catch((err: BusinessError) => {
    6. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
    7. });

Random Query

From version 6.0.1(21), the random query function is supported.

  1. Import required modules.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { cloudDatabase } from '@kit.CloudFoundationKit';
    2. import { BookInfo } from '../model/BookInfo';
    3. import { hilog } from '@kit.PerformanceAnalysisKit';
    4. import { BusinessError } from '@kit.BasicServicesKit';
  2. You can use orderByRandom() to display objects in the query result set in random order.

    This method applies to scenarios such as recommending random content or playing random audio and video.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. let condition = new cloudDatabase.DatabaseQuery(BookInfo);
    2. condition.orderByRandom().limit(10);
    3. databaseZone.query(condition).then((resultArray: BookInfo[]) => {
    4. resultArray.forEach((value) => {
    5. hilog.info(0x0000, 'cloudDb',
    6. `Succeeded in querying: bookName = ${value.bookName} price: ${value.price?.toString()}`);
    7. });
    8. }).catch((err: BusinessError) => {
    9. hilog.error(0x0000, 'cloudDb', `Failed to query, code: ${err.code}, message: ${err.message}`);
    10. });
Search in Guides
Enter a keyword.