Back

Query the list of values for a specific attribute but remove duplicates

  • 1
  • Databases
  • Functions
ewic
30 Mar, 2024, 22:21

Imagine I have a collection of stock data, where there is a "symbol" attribute for each document. In my python function I want to query my collection to return a list of "symbol" attribute values that are unique.

i.e. If I had 3 documents and 2 of the documents had their symbol attribute as "AAPL", and the third document had their symbol attribute as "MSFT", the query would return a list of ["AAPL", "MSFT"]

TL;DR
Query the database to retrieve unique values of a specific attribute. To achieve this, the developer can use the `distinct` option in the `list_documents` method. Here's how to modify the existing code: ```python docs = databases.list_documents( DATABASE_ID, COLLECTION_ID, queries=[ Query.distinct(['symbol']) ] ) ``` By adding `Query.distinct(['symbol'])`, the query will return a list of unique values for the "symbol" attribute.
ewic
30 Mar, 2024, 22:49
TypeScript
# Query Database for list of unique symbols
client = (
    Client()    
    .set_endpoint('https://cloud.appwrite.io/v1')
    .set_project(PROJECT_ID)
    .set_key(APPWRITE_API_KEY)
)

databases = Databases(client)   

docs = databases.list_documents(
    DATABASE_ID, 
    COLLECTION_ID,
    queries=[
        # What queries do I add here?
        Query.select(['symbol'])
    ] 
)
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