Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug : Query an Array attribute with an array of values does not work #2838

Closed
2 tasks done
superseby2 opened this issue Feb 25, 2022 · 21 comments
Closed
2 tasks done
Assignees
Labels
bug Something isn't working product / databases Fixes and upgrades for the Appwrite Database.

Comments

@superseby2
Copy link

馃憻 Reproduction steps

I am trying to query a Collection where one attribute is an Array of string

Trying this syntax fails to retrieve anything (and it feels normal)

database.list_documents('collection',                                                                       
       [                                                                                                               
           Query.equal("array_attribute, ["value1","value2"])                             
       ]                                                                                                               
))

Trying this syntax fails also while it feels like it could work ;)

database.list_documents('collection',                                                                       
       [                                                                                                               
           Query.equal("array_attribute, '["value1","value2"]')                             
       ]                                                                                                               
))

What I am trying to do is basically an "Array IN Array" comparison. Going the "equal" way seems hackish but I thought as the attribute is stored in a String it might work (and could be a work-around until a proprer "in" or "contains" operator exists. ?)

My only other option to make this work is use a function to "explode" the values contained in array_attribute in a new collection and then I can do a OR comparaison. It works but is not as straighforward + it generates a big collection.

馃憤 Expected behavior

some content is being retrieved

馃憥 Actual Behavior

Nothing is retrieved.

馃幉 Appwrite version

Version 0.12.x

馃捇 Operating system

Linux

馃П Your Environment

No response

馃憖 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

馃彚 Have you read the Code of Conduct?

@superseby2 superseby2 added the bug Something isn't working label Feb 25, 2022
@superseby2 superseby2 changed the title 馃殌 Feature: Query an Array attribute with an array of values does not work Bug : Query an Array attribute with an array of values does not work Feb 25, 2022
@superseby2
Copy link
Author

Just tried using a fulltext index and it seems to work. Would it be the route to follow ?

@superseby2
Copy link
Author

Fulltext option works great until I want to use multiple attributes in the fulltext index. Creating the inndex works in the console (no error) but when querying using this index an error is raised

    raise AppwriteException(response.text, response.status_code)
appwrite.exception.AppwriteException: {"message":"Error: Can't find FULLTEXT index matching the column list"

@superseby2
Copy link
Author

superseby2 commented Feb 25, 2022

Creating one single fulltext index for each attribute works though and I can query using a Search Query for each attributes defined in the index.
So to be able to use an fulltext index composed with multiple attributes I had to 1) create a fulltext index for each attribut茅 2) create an Index with all the attributes in it

@anandsubbu007
Copy link

how to implement search for matching document with array attribute which match and of the data in the list

@TorstenDittmann
Copy link
Contributor

This is not really supported as of right now, every possible solution will come with its downsides 馃憤馃徎

We are working on a way to query the contents of an array.

@meepeek
Copy link

meepeek commented Aug 30, 2022

Is there any update on this issue ? I'm planing to have a tag search in an app so I search and found this thread.

@stnguyen90
Copy link
Contributor

@meepeek, @superseby2, for this type of functionality, are you expecting for documents to return if they have any least one of the values in the query? For example, let's say the document has ["flutter", "dart"] for attribute tags. Query.contains("tags", ["flutter", "react"]) would return the document because flutter is in the tags. Is this the functionality you need?

@meepeek
Copy link

meepeek commented Sep 17, 2022

@stnguyen90 I think making the single param called exact match make more sense as we can overloaded the function for partial matching. I propose sample use cases and syntax representation as follow.

  1. Partial matching (contains 'a' or 'b') - use case, tag filter where user may uncheck unwanted tags
[ Query.contains('tags', ['a'], ['b']) ]
  1. All matching (have both 'a' and 'b') - site admin query for counts or batch operation
[ Query.contains('tags', ['a', 'b']) ]
  1. All matching strictly (have both 'a' and 'b' and no others) - also site admin query
[ Query.arrayLength('tags', 2), Query.contains('tags', ['a', 'b']) ]
  1. Invert operation - if there are fewer unwanted tags, it would be more efficient to do the invert
[ Query.notContains('tags', ['a', 'b']) ]

Also, tag use case will need distinct query so we will know all available tag list before the query. I didn't see query distinct in the doc but it can be temporary fixed by having a unique list updated as each record created or updated. However, I think distinct query should be available for every attribute types.

@netapy
Copy link

netapy commented Nov 6, 2022

Are there any news on this issue ? This would be very useful

@naitbrahim
Copy link

Hi, is there any news on this topic please ? Thanks

@Ota-Prokopec
Copy link

I dont know if it is helpful, but i found out that you can use Query.search to looking for items in array.

@feschaffa
Copy link

Any plans on implementing this? Using the search method for now, but feels a bit hacky. Thanks!

@0xDjole
Copy link

0xDjole commented Nov 25, 2023

This is a useless product.

@oussamachah2020
Copy link

i have an attribute of type array and i want to update it by pushing value into, how can i do that in JS ??

@johnschatner
Copy link

Would also love this feature! Search is a hit or miss with foreign characters.

@OmidHajizadeh
Copy link

@meepeek, @superseby2, for this type of functionality, are you expecting for documents to return if they have any least one of the values in the query? For example, let's say the document has ["flutter", "dart"] for attribute tags. Query.contains("tags", ["flutter", "react"]) would return the document because flutter is in the tags. Is this the functionality you need?

That's exactly what I'm looking for

@Edijae
Copy link

Edijae commented Jan 22, 2024

Query.contains not available in node-appwrite 11.1.0 @stnguyen90

@stnguyen90 stnguyen90 self-assigned this Feb 11, 2024
@stnguyen90
Copy link
Contributor

@SimonMaluleka
Copy link

@oussamachah2020 here's an example to push into or add values to an array type attribute

export const likePost = async(userId, postId)=>{
    try {
        const currentLikes = await databases.listDocuments(
            databaseId,
            videosCollectionId,
            [Query.equal('$id', postId)]
        )

        if(!currentLikes.documents[0].likedBy.includes(userId)){
            const updatedLikes = [...currentLikes.documents[0].likedBy]
            updatedLikes.push(userId)
            const documentId = currentLikes.documents[0].$id
            const updateData = {            
                "likedBy": updatedLikes
            };
            const response = await databases.updateDocument(
                databaseId,
                videosCollectionId,
                documentId,
                updateData
            )
        }      

    } catch (error) {
        throw new Error(error)
    }
}

@SimonMaluleka
Copy link

@superseby2 I had a similar requirement and opted to retrieve the documents first and then filtered the results in my application code.

@SimonMaluleka
Copy link

For some reason this code also worked for querying the array attribute and suddenly stopped working

export const getlikedPosts = async(userId)=>{
    try {
        const posts = await databases.listDocuments(
            databaseId,
            videosCollectionId,
            [Query.search('likedBy', `${userId}"`)]
        )
    
        return posts.documents
    } catch (error) {
        throw new Error(error)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working product / databases Fixes and upgrades for the Appwrite Database.
Projects
Status: Done
Development

No branches or pull requests