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.
javascript | Copy CodewixData.query("myCollection").eq("status", "active").gt("age", 25);
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.
javascript | Copy Codelet 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);
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
Was this helpful?
1let newQuery = query1.and(query2);
1import wixData from 'wix-data';23// ...45let statusQuery = wixData.query("myCollection")6 .eq("status", "pending")7 .or(8 wixData.query("myCollection")9 .eq("status", "rejected")10 );1112let ageQuery = wixData.query("myCollection")13 .lt("age", 25)14 .or(15 wixData.query("myCollection")16 .gt("age", 65)17 );1819statusQuery.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 found35 }36 })37 .catch((error) => {38 let errorMsg = error.message;39 let code = error.code;40 });4142 /*43 * For example, results contain items where age is:44 * 1845 * 2146 * 6747 * 9048 *49 * But not items where age is:50 * 2551 * 3052 * 4053 * 6554 */