Get Count for Records matching query\criteria

0 votes
Sorry, the answer given to me for the previous related question told me to use Find All Document Count but I need to count only those that match a specific query not all those in a collection. I have not experimented with CountByQuery because I read an answer to a different user's question that that function was going to be depreciated, is there an alternative? The first call to FindDocumentByQueryWithPaging() works but I am trying to work out if/when I need to call that function again if there are more records than the maximum 100. However storage.GetTotalRecords() always gives me -1 regardless of how many documents were successfully returned or how many are remaining and storage.CountRecords() just tells me how many records were given not how many there are in total.
asked Mar 25 in Unity by ant (13 points)

1 Answer

–1 vote

Hi Ant,

You can achieve your objective with the Find Document By Query API, please have a look at the below API where you can get the count of the records which has matched to your query.

In the case of any concern please do feel free to reach out to us, we will be happy to help you.

Code snippet:

String dbName = "<Enter_the_dbName>";  
String collectionName = "<Your_Collection_Name>";   
String key1 = "name";   
String value1 = "Nick";      
String key2 = "age";  
int value2 = 30;    
Query q1 = QueryBuilder.Build(key1, value1, Operator.EQUALS); // Build query q1 for key1 equal to name and value1 equal to Nick
Query q2 = QueryBuilder.Build(key2, value2, Operator.GREATER_THAN); // Build query q2 for key2 equal to age and value2 equal to 30
Query query = QueryBuilder.CompoundOperator(q1, Operator.OR, q2);   
// Pass aggregated query to finder method below. Similarly you can aggregate more conditions in querying object.  
App42Log.SetDebug(true); //Print output in your editor console
App42API.Initialize("API_KEY","SECRET_KEY");
StorageService storageService = App42API.BuildStorageService(); 
storageService.FindDocumentsByQuery(dbName,collectionName,query, new UnityCallBack()); 
public class UnityCallBack : App42CallBack
{
public void OnSuccess(object response)
{
Storage storage = (Storage) response;
IList<Storage.JSONDocument> jsonDocList = storage.GetJsonDocList(); 
 
// following line of code use for getting size of json doucumemt
 
Int totalSize=jsonDocList.Count
 
for(int i=0;i <jsonDocList.Count;i++)
{   
App42Log.Console("objectId is " + jsonDocList[i].GetDocId());
App42Log.Console("jsonDoc is " + jsonDocList[i].GetJsonDoc());
}  
}
public void OnException(Exception e)
{
App42Log.Console("Exception : " + e);
}

}

answered Mar 26 by priyanka.singh (166 points)
Sorry again, this doesn't tell me the TOTAL number of records if above the 100 record limit per query. I need a way of finding the total count so that I know how many paged queries I need to run to download all matching records.
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...