Page 1 of 1

In Server code, how to get a count of records that will be returned for a query

Posted: Tue Aug 30, 2016 12:21 pm
by Andy Parker

Hi,

I'm trying to figure out how to perform a query on a database (Appery DB) that will simply return the number of records that would be returned.

I don't want to perform the query and get the data (as it could be large) as I'm only interested in the number that would be returned.

The mongoDB documentation says:
db.collection.count(query, options)

But I don't know how to convert that into the correct server code lines?

thanks.


In Server code, how to get a count of records that will be returned for a query

Posted: Tue Aug 30, 2016 4:24 pm
by Galyna Abramovych

Hello Andy,

You can use the following Database Collections API method:
query(dbId, collectionName[, params][, token])

["count":, ] - boolean - parameter indicating whether the number of selected objects should be included in response


In Server code, how to get a count of records that will be returned for a query

Posted: Wed Aug 31, 2016 7:19 am
by saurabh8122815

Hello Andy,

try this one in SC,
code

var responseBody = {};
var requestParams = {};
var paramKeys = request.keys()
for (var key = 0; key < paramKeys&#46;length; key++) {
requestParams[paramKeys[key]] = request&#46;get(paramKeys[key]);
}
&#47;&#47; Declare database ID and Master key
var dbId = "db_id&quot
var collection = requestParams['collection_name'];
var where = requestParams['where'];
try {
var XHRResponse = XHR&#46;send("GET", "https:&#47;&#47;api&#46;appery&#46;io/rest/1/db/collections/" + "db_products", {
"headers": {
"X-Appery-Database-Id": dbId&#47;&#47;,
&#47;&#47;"X-Appery-Master-Key": masterKey
},
"parameters": {
"where": where,
"count": "1"
}
});
responseBody&#46;count = XHRResponse&#46;body[0]&#46;count;
response&#46;success(responseBody, "application/json");
} catch (e) {
response&#46;success("message: " + e&#46;message + "\ncode: " + e&#46;code);
}

/code


In Server code, how to get a count of records that will be returned for a query

Posted: Wed Aug 31, 2016 8:11 am
by Andy Parker

thanks for this great help!
This is all about speed, I want the query returning the count to be as fast as possible, hence not returning data.

Can you confirm the above does not return queried data, just the count?


In Server code, how to get a count of records that will be returned for a query

Posted: Wed Aug 31, 2016 9:07 am
by saurabh8122815

yeah its return only count u can try it :)


In Server code, how to get a count of records that will be returned for a query

Posted: Wed Aug 31, 2016 3:34 pm
by Andy Parker

great, thanks for this - working a treat !