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?
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?
Hello Christine!
The following reference could be helpful http://docs.appery.io/documentation/b.... Let us know about the result.
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?
Hello,
Please look up here http://docs.mongodb.org/manual/refere...
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?
Adrian,
Here is another way:
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
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:
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
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:
The 3 actions are:
Get the last ID.
Invoke the service to create the next record using the next ID.
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.
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:
Button click - invoke "GetChatID" service.
On "GetChatID" service "success" event - Invoke the service to create the next record using the next ID.
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.