Back

Get list documents only numbers

  • 1
  • Databases
  • Flutter
Mosh Ontong
16 Apr, 2023, 12:41

For example I have gender attribute in my collection. And then, I am going to get the total documents of male gender documents. What I want is that it returns me a number only. Like, I do not want in my client side to get the total length of documents. What I want that is in server side it will get the numbers of total documents and send this to client side.

TL;DR
The user wants a JavaScript code snippet that retrieves the total number of documents with a specific attribute from a collection. The user wants the code to return only the number and not include any other information in the response. Here's a code snippet that accomplishes this: ```javascript module.exports = async function (req, res) { let total = 0; try { const res = await database.listDocuments('db', 'collections', [ sdk.Query.equal('gender', MALE), ]); total = res.total; } catch (e) { } res.json({total: total}); }; ``
Binyamin
16 Apr, 2023, 12:56

For anything that need to do things that are not available with the Database SDK then I will use functions to achieve it. So in your case this is an example of how it work for you in JavaScript

TypeScript
  module.exports = async function (req, res) {
    let total = 0;
    try {
        const res = await database.listDocuments('db', 'collections', [
            sdk.Query.equal('gender', MALE),
        ]);

        total = res.total;
    } catch (e) {
    }

    res.json({total: total});
};

I've omitted all Appwrite preparing code

Binyamin
16 Apr, 2023, 12:56

If this is didn't what you've meant lmn

Mosh Ontong
16 Apr, 2023, 12:57

I see thank you sir

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