Christine Stringfield
Posts: 0
Joined: Wed Apr 03, 2013 4:35 am

Query Max Value

I have a collection that has a column with number values. Is it possible to query this collection and return the row that has the maximum value in this number column?

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

Query Max Value

Hello Christine!

The following reference could be helpful http://docs.appery.io/documentation/b.... Let us know about the result.

Adrian Stoch
Posts: 0
Joined: Thu Jul 31, 2014 3:09 pm

Query Max Value

I followed the reference doc as suggested but could not see how to return the maximum value for a specified numeric.

I realize I can do this by doing a descending sort and then choosing the first record, but this seems inefficient. Is there a better way?

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

Query Max Value

Hello,

Please look up here http://docs.mongodb.org/manual/refere...

Adrian Stoch
Posts: 0
Joined: Thu Jul 31, 2014 3:09 pm

Query Max Value

Alena,

Thank you for the reference. But where and how would I apply the mongo query modifiers?

Am I doing that in the Database service somehow or adding the code to a label?

Is there additional documentation which explains how to apply this in Appery?

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

Query Max Value

Adrian,

Here is another way:

  1. You should sort data on necessary field.
  2. Set limit = 1
Adrian Stoch
Posts: 0
Joined: Thu Jul 31, 2014 3:09 pm

Query Max Value

Alena,

Thank you for the suggestion. I have implemented as described, but am struggling to get my LSV to update at the time the service is invoked.

This is the code:
code
//Reset the LSV to 0
localStorage.setItem('NextChatID',0);

// Execute the service to get the current last ID
GetChatID.execute();
var NextIDstr = localStorage.getItem('NextChatID');

//Increment the ID by 1 and update on screen
var NextID = parseInt(NextIDstr)+1;
Apperyio('NextIDLabel').text(NextID);

alert("wait");/code

Here is what happens:
Image

When the wait alert is shown, the ID Label still shows 1. The LSV is 0.
As soon as I move past the alert, the LSV is updated to the correct value. But this is after the function has completed and the Label still shows 1:
Image

Here is the service request parameters:
Image

Here is the mapping to the LSV in the service:
Image

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Query Max Value

Hello Adrian,

Everything is correct. Rest request is asynchronously (in parallel). I.e. you make a service call, the request comes, the result has not yet been received, and the execution of js code goes further.
You should use this code
code
var NextIDstr = localStorage.getItem('NextChatID');
//Increment the ID by 1 and update on screen
var NextID = parseInt(NextIDstr)+1;
Apperyio('NextIDLabel').text(NextID);/code

on success of event service

Adrian Stoch
Posts: 0
Joined: Thu Jul 31, 2014 3:09 pm

Query Max Value

Hi Evgene,

Thanks for the response. But I still can't get the latest ID result back in time. Is there a way to force the result before going to the next action?

Here is the sequence of actions from the button click:
Image

The 3 actions are:

  1. Get the last ID.

  2. Invoke the service to create the next record using the next ID.

  3. Navigate to the next page.

    I have to execute step 1 right before 2 to ensure that I get the most recent last ID. If I try and do this earlier it will certainly result in duplicates of the last ID when there are multiple users.

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Query Max Value

Hi Adrian,

Service invokes in background. Thus your code resume to execute without service response.

See explanation on screen shot: http://prntscr.com/4o53hw/direct

So here is true way to do your three actions:

  1. Button click - invoke "GetChatID" service.

  2. On "GetChatID" service "success" event - Invoke the service to create the next record using the next ID.

  3. On "success" event from prev service - navigate to page.

    That's called async javascript. So every time before you run next action you have to wait after prev async processing will done.

    Regards.

Return to “Issues”