Back

[Solved] How to query relationships in database.listDocuments()

  • 1
  • Databases
  • Web
Axentioi
26 Jun, 2023, 11:43

I have a list of documents that I want to query only for the respective "clients" collection that is in relationship with the "projects" collection.

Is that possible? Or should I query all the projects and then filter by clients?

TypeScript
    const queryBuilder = () => {
        const queryArray = []

        if (search) {
            queryArray.push(Query.search('name', search))
        }

        queryArray.push(Query.limit(limit))
        queryArray.push(Query.offset(offset))

        return queryArray
    }

    const getDocuments = async () => {
        if (!database) return

        await database.listDocuments(
            Server.databaseId!,
            Server.collections.projects!,
            queryBuilder())
            .then((res: any) => {
                setprojects(res.documents)
                setDocumentsCount(res.total)
            })
            .catch((err: Error) => {
                showSnackbar(err.message, 'error')
            })
    }
TL;DR
The user is asking if it is possible to query only the projects that are related to a specific client when using the `database.listDocuments()` method. Another user explains that the query is being performed on the parent attributes, not the relationships. The solution provided is to query all the projects and then filter the results by the related client. The code provided shows how to implement this filtering in the `queryBuilder()` function.
Guille
26 Jun, 2023, 15:05

Queries are not available for relationship at the moment

https://appwrite.io/docs/databases-relationships#query

So you will have to query all projects and filter by clients as you mention it

Axentioi
26 Jun, 2023, 15:16

@Guille huh... but this works as expected thought... :

TypeScript
        const queryArray = []

        if (search) {
            queryArray.push(Query.search('name', search))
        }

        if (clientId) {
            queryArray.push(Query.equal('clients', clientId))
        }

        queryArray.push(Query.limit(limit))
        queryArray.push(Query.offset(offset))

        return queryArray
    }
Guille
26 Jun, 2023, 15:22

But seems like you are performing the query in the parent attributes not relational or I am wrong? πŸ€”

Guille
26 Jun, 2023, 15:23

@Axentioi

Axentioi
26 Jun, 2023, 15:34

I am querying the documents from the projects collection, and I want to only take the projects only for the related client so I added the Query.equal('clients'', clientId) which is the related client for the project so I get back the correct response

Axentioi
27 Jun, 2023, 19:16

[Solved] How to query relationships in database.listDocuments()

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