Moralis cloud function not returns whole data if there are 100+ rows

Hi Team,

I have a DB setup wherein one of tables I have 700+ entries (to be precise 711). I created getData Cloud function (like below) to return all the entries (711) from the DB but it only returns 100 entries always. Here is the code:

const commentsQuery = new Moralis.Query("Comments");
return await commentsQuery.find();

What should be done to return all the entires (711 entries) instead of 100? Or Is there any limit set into the account which I reset somehow?

Thanks.

you can set limit to that query, by default the limit is of 100, you can also use pagination

you can use query.limit(10); to get only 10 instead of 100 for example

you can read more about it here: https://docs.moralis.io/moralis-server/database/queries#basic-queries

1 Like

Is there a way to return all rows of a table instead of only 100? using masterkey or something?

I would like to return all because I need to add a property in each object and then order by this property to send to the frontend sorted.

example:

const final = await robotsQuery.withCount().find();

  for (let index = 0; index < final.results.length; index++) {
    const mktrobot = await new Moralis.Query("RobotsOnSale")
      .descending("block_timestamp")
      .equalTo("tokenId", final.results[index].attributes.TokenId.toString())
      .first();

    if (!mktrobot) continue;

    const result = Moralis.Cloud.units({
      method: "fromWei",
      value: mktrobot.attributes.startingPrice,
    });

    final.results[index].price = +result;
  }

  const robots = await GetRobots(final.results);

  const sortedRobots = robots.sort((a, b) => a.price - b.price);

  if (
    sortedRobots.slice(pagesToSkip, pagesToSkip + robotsPerPage).length == 0
  ) {
    page = 1;
    pagesToSkip = 0;
  }

note that “robotsQuery” is a different query than “RobotsOnSale”. Doing it through aggregator is too painful so I think this way was much easier…

Wasn’t this answered in the previous post?
You can do that by using limit

your example was with a number less than 100, I thought that couldn’t have more than that default limit. Ok then, if I got it right if I set “query.limit(1000)” it will return 1000 records right?

Let me change my question then, is there a way to remove the limitation at all? not having to set manually the limit? something like only runing with “.find({useMasterKey:true})” then it return no matter how many rows are there, is there anything for it?

There are more complicated ways, like using pagination, it may be the easier way to use a big limit.
You can also get the count first to get the number of rows