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.
Summary
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
Apr 16, 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
JavaScript
module.exports=asyncfunction(req, res){
let total =0;
try{
const res =await database.listDocuments('db','collections',[