Search.../

and( )

Adds an and condition to the query or filter.

Description

The and() function adds an and condition to a WixDataQuery or WixDataFilter. A query or filter with an and returns all the items that match the query or filter as defined up to the and function and also match the query or filter passed to the and function.

Note that when chaining multiple WixDataFilter functions to a query an and condition is assumed. In such cases, you do not need to add a call to the and() function. For example, this query returns results where status is active and age is greater than 25.

wixData.query("myCollection")
.eq("status", "active")
.gt("age", 25);
javascript | Copy Code

The and() function is needed when performing compound queries. For example, the final query in this set of queries returns results where status is either pending or rejected and age is either less than 25 or greater than 65.

let statusQuery = wixData.query("myCollection")
.eq("status", "pending")
.or(
wixData.query("myCollection")
.eq("status", "rejected")
);
let ageQuery = wixData.query("myCollection")
.lt("age", 25)
.or(
wixData.query("myCollection")
.gt("age", 65)
);
let statusAndAgeQuery = statusQuery.and(ageQuery);
javascript | Copy Code

The collections referenced by both the initial query and the query passed to the and function must be the same.

The 'and()' function is designed to work with 2 or more queries or filters. If you use it on its own, it will return all the items in a collection.

Syntax

function and(query: WixDataQuery): WixDataQuery

and Parameters

NAME
TYPE
DESCRIPTION
query

A query to add to the initial query as an and condition.

Returns

A WixDataQuery object representing the refined query.

Return Type:

Was this helpful?

Add an and to a query

Copy Code
1let newQuery = query1.and(query2);
Create two or queries, add an and, and run it

Copy Code
1import wixData from 'wix-data';
2
3// ...
4
5let statusQuery = wixData.query("myCollection")
6 .eq("status", "pending")
7 .or(
8 wixData.query("myCollection")
9 .eq("status", "rejected")
10 );
11
12let ageQuery = wixData.query("myCollection")
13 .lt("age", 25)
14 .or(
15 wixData.query("myCollection")
16 .gt("age", 65)
17 );
18
19statusQuery.and(ageQuery)
20 .find()
21 .then((results) => {
22 if(results.items.length > 0) {
23 let items = results.items;
24 let firstItem = items[0];
25 let totalCount = results.totalCount;
26 let pageSize = results.pageSize;
27 let currentPage = results.currentPage;
28 let totalPages = results.totalPages;
29 let hasNext = results.hasNext();
30 let hasPrev = results.hasPrev();
31 let length = results.length;
32 let query = results.query;
33 } else {
34 // handle case where no matching items found
35 }
36 })
37 .catch((error) => {
38 let errorMsg = error.message;
39 let code = error.code;
40 });
41
42 /*
43 * For example, results contain items where age is:
44 * 18
45 * 21
46 * 67
47 * 90
48 *
49 * But not items where age is:
50 * 25
51 * 30
52 * 40
53 * 65
54 */