Skip to content
Init is coming / May 19 - 23
Back

Logical AND and OR queries

  • 0
  • Databases
  • Flutter
mauricev
8 Mar, 2023, 07:09

I have the following query List<String>? rackQuery = [ Query.equal("facility_fk", document_id), Query.equal("rack_fk", absolutePosition), ]; According to the docs, this is an OR query. What is the exact syntax for an AND query? Also, if I understand correctly, both attributes must part of a combined index.

TL;DR
The provided query is actually an AND query, not an OR query. To perform an OR query, you need to use the `Query.equal` method with multiple values for the same field. For example: ```dart List<String>? rackQuery = [ Query.equal("facility_fk", [docId1, docId2, docId3]), ]; ``` This will result in `facility_fk = docId1 OR facility_fk = docId2 OR facility_fk = docId3`. However, performing an OR query between multiple keys is not currently possible. One solution is to introduce a new attribute, such as "verified",
Meldiron
8 Mar, 2023, 07:23

Hey there 👋 WHat you provided is AND query. In SQL world, this would look like facility_fk = document_id AND rack_fk =absolutePosition. This is how you achieve OR queries:

TypeScript
List<String>? rackQuery = [
      Query.equal("facility_fk", [docId1, docId2, docId3]),
    ];

it would result in facility_fk = docId1 OR facility_fk = docId2 OR facility_fk = docId3.

Doing OR between multiple keys is not possible at the moment. I have seen people solving this by introducing a new attribute that is only used for querying. For example, let's say I want a feature of "verified" articles.. And I want to query only verified articles. Let's say an article is considered verified if the length is > 1500 chars, or if it has at least 5 tags..

How I would recommend to do it is to introduce new attribute called verified and every time a document is created/updated, I would do the check client-side (or server-side with Appwrite Functions and events). In the check I would look at length and tags. Based on that, I set verified to true or false.

Later, to query only verified ones, I would do [ Query.equal("verified", true) ].

Makes sense?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more