How to show the amount of items in a collection as a number on a page?

To get the whole data (data-count) of a database…

Versio-1: hasNext-mode:

import wixData from 'wix-data'

$w.onReady(async()=>{
    try{
        const res = await wixData.query("MyDB").find();
        let itemsTotal = res.items; 
        if(res.hasNext()){   
            while(res.hasNext()){
                res = await res.next(); 
                itemsTotal.concat(res.items);
            }
        }
        console.log(itemsTotal);
    }catch(err){console.error('Error: ', err);}
});

Versio-2: promiseAll-mode:

return Promise.all([
    query('collection').limit(1000).find(),
    query('collection').limit(1000).skip(1000).find(),
    query('collection').limit(1000).skip(2000).find(),
    query('collection').limit(1000).skip(3000).find(),
]).then((res) => {
    const cache = { items: [] }
    for (const result of res) {
        cache.items = cache.items.concat(result.items);
    }
})

An example to find ALL paid orders…

  return wixData.query("Stores/Orders")
      .limit(1000)
      .eq("paymentStatus", "PAID")
      .find(options)
      .then(async (ordersData) => {
          let totalPage = ordersData.totalPages;
          let array = [];
          let skip = 0;

          for (let i = 0; i < totalPage; i++) {
              array.push(wixData.query("Stores/Orders").limit(1000).eq("paymentStatus", "PAID").skip(skip).find(options))
              skip = skip + 1000;
          }

          return Promise.all(array)
              .then((res) => {
                  const cache = { items: [] }
                  for (const result of res) {
                      cache.items = cache.items.concat(result.items);
                  }
                  return cache;
              })
      })

Another method…

import wixData from 'wix-data';
$w.onReady(function () {
let r1;
wixData.query("CollectionName").limit(1000).find()
.then(r => {
    r1 = r.items;
 return r.totalCount;
})
.then(r => {
let numberOfQueries = Math.ceil(r/1000);
let queries = [];
 for(let i = 1; i < numberOfQueries; i++){       queries.push(wixData.query("CollectionName").limit(1000).skip(i * 1000));
    }
 return Promise.all(queries.map(e => e.find()));
    })
    .then(r => {
        r = r.map(e => e.items);
        r.unshift(r1);
        r = r.flat();
        console.log(r.length);
 //r is the results, you can use it.
    }).catch(err => err);
});

And another one…

Surely more can be found.

The max limit is always → 1000, exept DATABASES like for sure PRODUCTS and maybe BLOG-POSTS → LIMIT = 100 as i can remember.

1 Like