Search.../

query( )

Creates a query for retrieving items from a database collection.

Description

The query() function builds a query on the specified collection and returns a WixDataQuery object.

The returned object contains the query definition which is typically used to run the query using the find() function.

You can refine the query by chaining WixDataQuery functions onto the query. WixDataQuery functions enable you to sort, filter, and control the results a query returns.

The query() runs with the following WixDataQuery defaults that you can override:

The functions that are chained to query() are applied in the order they are called. For example, if you sort on an age property in ascending order and then on a name property in descending order, the results are sorted first by the age of the items and then, if there are multiple results with the same age, the items are sorted by name in descending order, per age value.

If the collection that you are querying has references to other collections, by default, the data from referenced collections will not be retrieved. To get the data from the referenced items, you must either use the include() chained function before find() or use the queryReferenced() function instead of query().

When working with Wix app collections, fields in the collections have the following permissions:

  • Can connect to data
  • Can use in dynamic page URL
  • Can be sorted
  • Can be filtered
  • Read-only

Check which fields can be used in a query.

Note:
When using the query() or get() functions or another data retrieval method following a change to your database collection, the data retrieved may not contain your most recent changes. See Wix-data and Eventual Consistency for more information. To solve this problem, you can use the setTimeout() function to delay retrieving data following any changes to your database collection.

Syntax

function query(collectionId: string): WixDataQuery

query Parameters

NAME
TYPE
DESCRIPTION
collectionId
string

The ID of the collection to run the query on.

To find your collectionId, select the Databases tab in the Velo Sidebar. Hover over your collection, click the three dots, and select Edit Settings.

Returns

A query object.

Return Type:

Was this helpful?

Build a query

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let query = wixData.query("myCollection");
Build and perform a query

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.query("myCollection")
6 .find()
7 .then((results) => {
8 if(results.items.length > 0) {
9 console.log(results.items[0]); //see item below
10 } else {
11 // handle case where no matching items found
12 }
13 })
14 .catch((err) => {
15 console.log(err);
16 });
17
18/* firstItem is:
19 *
20 * {
21 * "_id": "00001",
22 * "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb",
23 * "_createdDate": "2017-05-24T12:33:18.938Z",
24 * "_updatedDate": "2017-05-24T12:33:18.938Z",
25 * "title": "Mr.",
26 * "first_name": "John",
27 * "last_name": "Doe"
28 * }
29 */
Build and perform a query using data options

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.query("myCollection")
6 .eq("title", "Dr.")
7 .find()
8 .then((results) => {
9 if(results.items.length > 0) {
10 console.log(results.items[0]); //see firstItem below
11 } else {
12 // handle case where no matching items found
13 }
14 })
15 .catch((err) => {
16 console.log(err);
17 });
18
19/* firstItem is:
20 *
21 * {
22 * "_id": "00002",
23 * "_owner": "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb",
24 * "_createdDate": "2017-05-24T12:33:18.938Z",
25 * "_updatedDate": "2017-05-24T12:33:18.938Z",
26 * "title": "Dr.",
27 * "first_name": "Jane",
28 * "last_name": "Doe"
29 * }
30 */
Create, filter, sort, limit, and run a query

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5wixData.query("myCollection")
6 .eq("status", "active")
7 .gt("age", 25)
8 .ascending("last_name", "first_name")
9 .limit(10)
10 .find()
11 .then((results) => {
12 if(results.items.length > 0) {
13 let items = results.items;
14 let firstItem = items[0];
15 let totalCount = results.totalCount;
16 let pageSize = results.pageSize;
17 let currentPage = results.currentPage;
18 let totalPages = results.totalPages;
19 let hasNext = results.hasNext();
20 let hasPrev = results.hasPrev();
21 let length = results.length;
22 let query = results.query;
23 } else {
24 // handle case where no matching items found
25 }
26 })
27 .catch((error) => {
28 let errorMsg = error.message;
29 let code = error.code;
30 });
Build a query incrementally

This example builds a query based on certain conditions. It then performs the query and uses the results to populate a table.

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5function runQuery(low, high) {
6 let myQuery = wixData.query("myCollection");
7
8 if(low) {
9 myQuery = myQuery.ge("price", low);
10 }
11
12 if(high) {
13 myQuery = myQuery.le("price", high);
14 }
15
16 myQuery.find()
17 .then((results) => {
18 if(results.items.length > 0) {
19 $w("#myTable").rows = results.items;
20 } else {
21 $w("#myTable").collapse();
22 }
23 });
24}
25
Build a query using include() to include the referenced collection cities, based on the field "city" from myCollection

Copy Code
1import wixData from 'wix-data';
2
3wixData.query("myCollection")
4 .include("city")
5 .find()
6 .then((results) => {
7 let firstItem = results.items[0]
8 console.log(firstItem);
9 });
10
11
12 /* firstItem is:
13 *
14 * {
15 * "_id": "1d8a1d97-93d3-4d6c-9a0f-c279058b4aa5",
16 * "_owner": "81c9168e-95b8-47fd-8e6a-ad9fdf71b38e",
17 * "_createdDate": "2020-08-03T10:26:26.825Z",
18 * "_updatedDate": "2020-08-03T10:27:01.558Z",
19 * "first_name": "Betty",
20 * "last_name": "Boop",
21 * "city":
22 * {
23 * "_id": "a99daca6-0400-4ef1-8d74-de3f9095bf0b",
24 * "_owner": "81c9168e-95b8-47fd-8e6a-ad9fdf71b38e",
25 * "_createdDate": "2020-08-03T10:22:39.114Z",
26 * "_updatedDate": "2020-08-03T10:23:25.042Z" ,
27 * "city_name": "Los Angeles",
28 * "state": "California"
29 * }
30 */
Query a collection using a custom function

In this example, we demonstrate how you can you can build a CRUD "read" function using query(). You can test out the code in our example template.

Copy Code
1import wixData from 'wix-data';
2
3// Code for a read operation using query() //
4
5async function readGreetings() {
6 const results = await wixData.query('Greetings').ascending('language').find();
7
8 return results.items;
9}
10