skip to Main Content

I am using Appwrite PHP SDK to query a collection that has more than 25 documents. I want to get all the documents in one query, so I tried to set the limit parameter like this:

$query = [
'limit' => 10 // Set the query limit to retrieve only 10 records
];

$staff_member_permissions = $databases->listDocuments('6421819e68517dc04814', '6422a0ea9c35c00f9e6c',$query);

However, this does not work and I still get only 25 documents. How can I get all the documents in one query using Appwrite PHP SDK?

2

Answers


  1. You’re getting only 25 result becuase that’s the default limit.
    Reference:
    https://appwrite.io/docs/queries

    For what I know, you can’t query "unlimited" documents.
    If I remember, max limit is 5000…you can pass 5000 as parameter to limit.

    Reference:
    https://appwrite.io/docs/client/databases?sdk=web-default#databasesListDocuments

    Login or Signup to reply.
  2. As mentioned in another answer there may be a max limit allowed for a single query, but you are also not passing the query parameter in the expected way. From the documentation you should use the Query class from the SDK.

    In this case to set the limit, you may need to instead add Query::limit(10) to your queries array, like this:

    // Use the Query class...
    use AppwriteQuery;
    
    $query = [
      Query::limit(10), // Set the query limit to retrieve only 10 records
    ];
    

    More details on the Queries can be found in the docs.

    EDIT: Looking at the issues, there seems to be confusion around how to use queries, you may want to reference this issue where the syntax is a bit different:

    use AppwriteQuery;
    
    $q = new AppwriteQuery();
    
    $query = [
      $q->limit(10), // Set the query limit to retrieve only 10 records
    ];
    

    You can try increasing that limit, but if you find that you need to retrieve more than the max allowed you will likely have to paginate your request. There is an example of how to do that with the use of limit and offset here. This is the example provided for the Node Client SDK:

    import { Databases, Query } from "appwrite";
    
    const client = new Client()
        .setEndpoint("https://cloud.appwrite.io/v1")
        .setProject("[PROJECT_ID]");
    
    const databases = new Databases(client);
    
    // Page 1
    const page1 = await databases.listDocuments(
        '[DATABASE_ID]',
        '[COLLECTION_ID]',
        [
            Query.limit(25),
        ]
    );
    
    const lastId = page1.documents[page1.documents.length - 1].$id;
    
    // Page 2
    const page2 = await databases->listDocuments(
        '[DATABASE_ID]',
        '[COLLECTION_ID]',
        [
            Query.limit(25),
            Query.cursorAfter(lastId),
        ]
    );
    

    You could translate that to PHP and if needed turn it into a loop of some kind to iterate through the results instead of hardcoding the pages.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search