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:
skip
:0
limit
:50
descending
: by_createdDate
include
: none
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()
.
Items marked as hidden in the collection are not returned.
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 thequery()
orget()
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 thesetTimeout()
function to delay retrieving data following any changes to your database collection.
Syntax
function query(collectionId: string): WixDataQuery
query Parameters
NAME
TYPE
DESCRIPTION
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.
Was this helpful?
1import wixData from 'wix-data';23// ...45let query = wixData.query("myCollection");
1import wixData from 'wix-data';23// ...45wixData.query("myCollection")6 .find()7 .then((results) => {8 if(results.items.length > 0) {9 console.log(results.items[0]); //see item below10 } else {11 // handle case where no matching items found12 }13 })14 .catch((err) => {15 console.log(err);16 });1718/* 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 */
1import wixData from 'wix-data';23// ...45wixData.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 below11 } else {12 // handle case where no matching items found13 }14 })15 .catch((err) => {16 console.log(err);17 });1819/* 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 */
1import wixData from 'wix-data';23// ...45wixData.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 found25 }26 })27 .catch((error) => {28 let errorMsg = error.message;29 let code = error.code;30 });
This example builds a query based on certain conditions. It then performs the query and uses the results to populate a table.
1import wixData from 'wix-data';23// ...45function runQuery(low, high) {6 let myQuery = wixData.query("myCollection");78 if(low) {9 myQuery = myQuery.ge("price", low);10 }1112 if(high) {13 myQuery = myQuery.le("price", high);14 }1516 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
include()
to include the referenced collection cities, based on the field "city" from myCollection1import wixData from 'wix-data';23wixData.query("myCollection")4 .include("city")5 .find()6 .then((results) => {7 let firstItem = results.items[0]8 console.log(firstItem);9 });101112 /* 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 */
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.
1import wixData from 'wix-data';23// Code for a read operation using query() //45async function readGreetings() {6 const results = await wixData.query('Greetings').ascending('language').find();78 return results.items;9}10