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 string attributes, array element matching on array attributes
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.
db.listDocuments(
'<DATABASE_ID>',
'<COLLECTION_ID>',
[
Query.contains('content', ['happy', 'love']),
]
)
It’s important to note that the contains operator also works on array attributes as well. For example, if we set a string attribute to act as an array, you could search this array in the same way you would search any other string.
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.
db.listDocuments(
'<DATABASE_ID>',
'<COLLECTION_ID>',
[
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.