Andy Parker
Posts: 0
Joined: Wed Mar 27, 2013 10:21 am

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

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.

Galyna Abramovych
Site Admin
Posts: 84
Joined: Tue Mar 22, 2016 6:03 pm

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

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

saurabh8122815
Posts: 0
Joined: Mon Mar 21, 2016 5:08 am

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

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

Andy Parker
Posts: 0
Joined: Wed Mar 27, 2013 10:21 am

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

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?

saurabh8122815
Posts: 0
Joined: Mon Mar 21, 2016 5:08 am

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

yeah its return only count u can try it :)

Andy Parker
Posts: 0
Joined: Wed Mar 27, 2013 10:21 am

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

great, thanks for this - working a treat !

Return to “Issues”