Page 1 of 1

Get count of records from collection query triggered from server code.

Posted: Fri Apr 25, 2014 10:29 am
by Andrew Peacock

Hi,
I'm running a query against a collection, which is queried by server code.

The query works fine in terms of pagination (start from X, show Y records).

But I can't seem to find a way of getting the total count of records whilst working within the server code.

Here is the definition of the query:
code
params.criteria = {
"post_category": {
"$in": [ categoryId, 6 ]
}
};

//Define other parameters
params.limit = maxEntries; //Show maximum 6 records
params.sort = "-post_timestamp&quot //Ascending sort by field "post_timestamp"
params.skip = start;
params.count = "1&quot
result.query = Collection.query(dbId, collectionName, params);

/code
But I can't find where to put the "count=1" parameter.
I've tried
params.count = 1;

params.count = "1";

params.count = true;

params.count = new Boolean(true);

And various other combinations that I can't remember now.

However, I end up with a variation of the following message in the trace:

message: java.lang.String cannot be cast to java.lang.Booleanncode: 0

Can you help please?

Regards,
Andy


Get count of records from collection query triggered from server code.

Posted: Fri Apr 25, 2014 12:22 pm
by Nikita

Hello,

It works fine for me. Here are the screenshots:

http://www.screencast.com/t/xnksfPFWoIep
http://www.screencast.com/t/cameuLeNXw


Get count of records from collection query triggered from server code.

Posted: Fri Apr 25, 2014 1:22 pm
by Andrew Peacock

Got it - if you add the count parameter, then the query returns ONLY the count. So I've done to queries - one for the data, one for the count, and then returned an object that as both data and count.

Thanks Nikita.