Back

Can i Query based on an attributes total items?

  • 0
  • Databases
  • Web
Ray
17 Nov, 2023, 13:32

Hi, im currently making a social media app that have a feature that can display top users ordered by their number of post/s. So in my "Users" collection it already have an attribute of posts which refers to the postId of the post/s they posted. I want to call the users collection and sort it descendingly by its posts total count (which is visible in the appwrite that says 11 items). I also have written a code to call the collection based on posts order descendingly but that doesnt work. Should i have created another attributes which stores an integer for postsCount or is there a way to access attribute 'posts' total items count? What should i change in the code Query.orderDesc()?

TL;DR
The user wants to query users based on the total count of their 'posts' attribute. They are wondering if they can do this without creating a 'postCount' attribute. The code they have tried using Query.orderDesc('posts') is not working and is returning a status 500 error. They are asking for assistance with resolving this issue. Solution: It is recommended to create a separate 'postCount' attribute to store the number of posts for each user. This will make it easier to query and sort the users based on their post count. To access the total count of posts in the 'posts' attribute, you will need to
Meldiron
17 Nov, 2023, 13:43

Hi 👋 You mensioned your solution doesnt work, what exactly is the issue? Are you getting some error message, or empty list? Or wrong order or values?

Ray
17 Nov, 2023, 15:51

its returning error because the query wont even run if i put Query.orderDesc('posts') It returns status 500

Ray
17 Nov, 2023, 15:51

i tried console.log() something and it doesnt reach if(!users) throw Error; line

Ray
17 Nov, 2023, 15:54

What i actually want to do is query users (in the picture theres only 1 user but later there will be more than 1) based on the attribute 'posts' items count (total result for posts attributes which in the picture says 11) so i can sort them based on their posts posted straight from the query with databases.listDocuments

Ray
17 Nov, 2023, 15:55

can i actually do that without creating postCount attribute? or i have to create another attribute for a user to store posts count seperately so i can just orderDesc('postCount')?

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