Back

[SOLVED] How can i write OR query to match an array?

  • 1
  • Web
xue
18 Sep, 2023, 15:11
TypeScript
databases.listDocuments(
        environment.appwrite.APP_DATABASE,
        environment.appwrite.ROOMS_COLLECTION,
              [
                   Query.equal('members', [userID, currentUserID]),
                   Query.equal('members', [currentUserID, userID]),
             ]
      );

my collection member attribute is: string[]

I want to either Query.equal('members', [userID, currentUserID]), OR Query.equal('members', [currentUserID, userID]),

not AND

Thanks!

TL;DR
There is a bug in the app and the implementation does not work as expected. The user wants to write an OR query to match an array. They have tried using two separate queries, but it only returns results when `cUserId` is found and does not care about `userId`. The solution is to use an AND query with `Query.search` after full-text indexing. Instead of using two separate `search` functions, the user can combine the `cUserId` and `userId` values in one attribute, separated by a space. If the `members` attribute is an array, the user can create another attribute called `
Binyamin
18 Sep, 2023, 15:13

As of now you can't use logical or in queries , Make sure to check this feature request https://github.com/appwrite/appwrite/issues/2740

xue
18 Sep, 2023, 15:16

Yesterday i met with appwrite first time and came to the limits.. sad story. Do you have any ideas about how I can go around?

Binyamin
18 Sep, 2023, 15:16

Yes You have two solutions

btme0011
18 Sep, 2023, 15:17

U can do this but the drawback is that you are making two request which might increase the latency

const result1 = await databases.listDocuments( environment.appwrite.APP_DATABASE, environment.appwrite.ROOMS_COLLECTION, [Query.equal('members', [userID, currentUserID])] );

const result2 = await databases.listDocuments( environment.appwrite.APP_DATABASE, environment.appwrite.ROOMS_COLLECTION, [Query.equal('members', [currentUserID, userID])] );

const combinedResults = [...result1.documents, ...result2.documents];

Binyamin
18 Sep, 2023, 15:19
  1. Fetch the documents twice.
  2. Create another attribute name id search inside that attribute you can put all the userIDs values seprated by space. Then add fulltext index to that attribute and you'll be able to search that field using or logic like so πŸ‘‡
TypeScript
databases.listDocuments(
        environment.appwrite.APP_DATABASE,
        environment.appwrite.ROOMS_COLLECTION,
              [
                   Query.search('search', `${userID} ${currentUserID}`),
             ]
      );
xue
18 Sep, 2023, 16:23

when i tried to examine this way, members has attribute of string[] (array) in docs, its written that if members has currentUserID OR userID then it will return the doc.

in my scenario, it is an array thats why it returns empty array when i tried everything manually.

TypeScript
const res1 = this.appwrite.listDocuments(environment.appwrite.ROOMS_COLLECTION, 
[
    Query.equal('members', ['Xvxuht5IIBPcV8bN1Q1UUhyYeil2', 'JNEB25J6hsdKjnHMOiZfuvQJ5dh2']),
]);

const res2 = this.appwrite.listDocuments(environment.appwrite.ROOMS_COLLECTION,
[
    Query.equal('members', ['JNEB25J6hsdKjnHMOiZfuvQJ5dh2', 'Xvxuht5IIBPcV8bN1Q1UUhyYeil2']),
]);

return Promise.all([res1, res2]).then((values) => {
    console.log(values);
    return [...values[0].documents, ...values[1].documents];
});

console

TypeScript
[
  {
    "total": 0,
    "documents": []
  },
  {
    "total": 0,
    "documents": []
  }
]
Drake
18 Sep, 2023, 16:26

uhh why not have one query wiht all the member values 🧐 That's the only OR we support at the moment

xue
20 Sep, 2023, 12:20

[SOLVED] How can i write OR query to match an array?

xue
20 Sep, 2023, 12:23

The solution is not to use OR query like i tried first post. I used AND query with Query.search ofc after full-text index.

TypeScript
databases.listDocuments(
        environment.appwrite.APP_DATABASE,
        environment.appwrite.ROOMS_COLLECTION,
              [
                   Query.search('users', cUserId),
                   Query.search('users', userId)],
             ]
      );    
Binyamin
20 Sep, 2023, 12:24

In this case you don't need to have two search function. You can have one with iser id's separate with a space

xue
20 Sep, 2023, 12:28

Really? in lib, search function accepts only one attribute and value (property) Query.search: (attribute: string, value: string) => string

Binyamin
20 Sep, 2023, 12:29

I know. But this string can have spaces in it like you can see here

Binyamin
20 Sep, 2023, 12:30
xue
20 Sep, 2023, 12:34

do you mean?

TypeScript
      [Query.search('users', `${cUserId} ${userId}`)]
Binyamin
20 Sep, 2023, 12:34

Yes

xue
20 Sep, 2023, 12:35

it works! Thanks a lot

xue
21 Sep, 2023, 23:25

small update: i found a bug in my app, that wanted to share it here, related topic this implementation does not work, it returns when cUserId found, not care about userId. i want both of the array contains cUserId AND userId

TypeScript
[
  Query.search('users', cUserId),
  Query.search('users', userId)
]```
for now, this is the best and working option.
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