Skip to content
Blog / Introducing new Database operators: or & contains query methods
5 min

Introducing new Database operators: or & contains query methods

What is a database without queries? Not a lot. That's why we’re happy to announce a new set of query methods, array contains, text contains, and OR operators to Appwrite Databases.

Introducing new Database operators: or & contains query methods
Updated:

We've added two new query methods, or and contains, to Appwrite Databases. By adding array element matches, partial text matches, as well as logical OR queries, we allow for more flexibility in managing your data.

These two query methods have been highly requested by the Appwrite community, and we’re excited to show you how to use them, so let’s jump in and take a look!

  • contains - partial text matches on text columns, array element matching on array columns
  • or - write logical OR queries

Contains operator

The contains operator is a great addition to the existing text search operators such as startsWith & endsWith, and can be used in combination with these two. With contains, we can now perform a broader search by matching against any text within a substring. This is extremely useful when searching a large body of text or when the placement of keywords is unknown.

JavaScript
tablesDB.listRows({
    databaseId: '<DATABASE_ID>',
    tableId: '<TABLE_ID>',
    queries: [
        Query.contains('content', ['happy', 'love'])
    ]
});

It’s important to note that the contains operator also works on array columns as well. For example, if we set a text column to act as an array, you could search this array in the same way you would search any other text value.

JavaScript
 Query.contains('tags', ['mystery', 'comedy', 'PG-13'])

 

Or operator

The logical OR operator allows us to nest queries in an OR condition. This gives us the ability to group queries together for more dynamic search.

To use the OR operator pass Query.or([...]) to the queries array and provide at least two queries within the nested array.

JavaScript
tablesDB.listRows({
    databaseId: '<DATABASE_ID>',
    tableId: '<TABLE_ID>',
    queries: [
        Query.or([
            Query.contains('name','ivy'),
            Query.greaterThan('age',30)
        ])
    ]
});

The example shown will return all people that contain the substring 'ivy' somewhere in their name OR are older than 30 years old. Previously, there was no native way to do this kind of search from Appwrite's SDKs.

Database improvements

With these new database improvements you have more possibilities for data retrieval and manipulation, and add powerful new queries and logic to Appwrite Databases for you to manage your data. Making Appwrite Databases an even more powerful and complete product for you to build with.

Resources

Visit our documentation to learn more about Database operators, join us on Discord to be part of the discussion, view our blog and YouTube channel, or visit our GitHub repository to see our open-source code.

Database operators will be available as part of the Appwrite 1.5 release on GitHub and Cloud in March 2024.

Frequently asked questions

  • What does the contains query do?

    Query.contains matches a substring inside a text column or an element inside an array column. It complements startsWith and endsWith and is the right choice when you don't know where in the value the match will appear. See Appwrite Databases.

  • How does the OR operator work in Appwrite queries?

    Pass two or more queries inside Query.or([...]) and any row that satisfies at least one of them is returned. You can nest it with other queries to build expressions like 'category equals X AND (name contains Y OR rating greater than 4)'.

  • Can I combine contains and OR in the same request?

    Yes. Both operators are regular query expressions, so you can pass several of them in the same queries array, or nest Query.contains calls inside a Query.or block.

  • Does contains do a full text search?

    No. Contains is a substring match, not a tokenized full text search. If you need ranking, stemming, or fuzzy matching, pair Appwrite Databases with a dedicated search engine like Meilisearch or Typesense.

  • When did these operators ship?

    Both contains and or shipped as part of the Appwrite 1.5 release on Appwrite Cloud and self hosted Appwrite in March 2024. They are available across every SDK.

  • Are there performance implications when using contains?

    Substring matches can't always use an index, so very large tables will be slower than equality queries. Where possible, narrow the result set first with indexed columns (equality, ranges) and then apply contains to filter further.

Start building with Appwrite today