Back

[SOLVED] How do I run queries to search and filter data from 2 different collections

  • 0
  • Databases
  • Web
koolklux19
6 Jun, 2023, 04:33

I have created 2 different collections within a same DB. And I want to run some query which finds same data and sorts that out. How can I achieve this?

TL;DR
The user was trying to run queries to search and filter data from two different collections in a database. They were initially getting undefined in the console when trying to retrieve the data. Another user suggested using a function that triggers on document creation, update, or delete in either collection to check for matching documents in the other collection. The user provided their code and mentioned that they wanted to find data values that are the same in both collections and return the usernames of the matching documents. It was suggested that there isn't a single API to achieve this, but asked for more information about the use case to explore alternative solutions. The thread ends with the user
Drake
6 Jun, 2023, 05:00

There isn't a single API to return this kind of data. However, can you describe your use case? Maybe there's some other way to achieve what you're looking for

koolklux19
6 Jun, 2023, 05:19

basically I want to find data values which are same in both the collections and return the username of the documents which are same(both the collection have a common attribute of username)

koolklux19
6 Jun, 2023, 05:21

One method I found is to create a seperate function which retrieves the data and then compares it. But it is slow. From documentation I concluded that queries can run on collections I just need to figure out how to 'join' these collections and run the query.

koolklux19
6 Jun, 2023, 05:22

Any help is appreciated.

Drake
6 Jun, 2023, 05:26

Which attributes need to match? How many matches will there be?

koolklux19
6 Jun, 2023, 05:30

there are 2 attributes in each collection one is 'username' and other one is 'topic' the 'topic' attributes need to be matched/compared and return the subsequent username of the document itself.

Drake
6 Jun, 2023, 05:39

Perhaps you can have a function that triggers on document creation, update, delete in either collection. The function can check if there's a matching document in the other collection and then if there's a match create a document in a 3rd document with the topic. Then, you can list documents in that 3rd collection to get all the matching topics and fetch from the other 2 collections for documents with that topic

koolklux19
6 Jun, 2023, 05:44

I was doing exactly that but I was not using a 3rd collection except I am getting undefined in the console itself. This means comparison is correct but the data being returned is not valid. here is my code if you want to take a look if(method=='learn') { let mainuser = databases.listDocuments( '647da37c3a08636bd3ad', '647da20436fr493248ac', [ Query.equal('TopicT',[topic]) ] ) mainuser.then(function(res){ const user = res.documents.username; console.log(user); //here i am getting undefined }, function (error) { console.log(error); }); }

Drake
6 Jun, 2023, 05:45

Btw, it's best to use 3 back ticks with multi-line code. See https://www.markdownguide.org/extended-syntax/#syntax-highlighting

Drake
6 Jun, 2023, 05:46

Documents is an array

koolklux19
6 Jun, 2023, 05:48

ahhhh ok completely forgot about that

koolklux19
6 Jun, 2023, 05:48

thanks a lot man

koolklux19
6 Jun, 2023, 05:49

and thanks for the 3 back ticks tip

Drake
6 Jun, 2023, 05:51

Happy to help!

Can we mark this as solved?

koolklux19
6 Jun, 2023, 05:51

sure sure go ahead.

Drake
6 Jun, 2023, 06:01

[SOLVED] How do I run queries to search and filter data from 2 different collections

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