Page 1 of 3

How can I create a group in my query service and set a limit on the values returned?

Posted: Sun Sep 01, 2013 11:48 pm
by yramess

I have a Collection that has these values (example):

groupname, value, userid, _updatedAt
A, 35, 222, Today
B, 54, 222, Today
A, 44, 255, Yesterday
A, 46, 222, Yesterday
B, 10, 222, Two days ago

I am already running a where query to only receive the rows for the connected user and I am sorting the data to get the latest values.

What I need to receive back for user A is this exactly:

A, 35, 222, Today
B, 54, 222, Today

So only the latest value entered for a "group" for a specified user.

I am still new to this so I am using the Query Service. My request probably needs Javascript but I would need help on how to achieve this with the current query result that I get (where on userid and sort on date).

Thanks


How can I create a group in my query service and set a limit on the values returned?

Posted: Mon Sep 02, 2013 1:01 am
by maxkatz

One option is to add a date to the query


How can I create a group in my query service and set a limit on the values returned?

Posted: Mon Sep 02, 2013 11:46 am
by yramess

It isn't that simple because I do not know the date. Those values will be created as my users send data to this collection.

I am looking to retrieve the latest group item value based on its user id. Which is why I still think this is a group query and I need help on how to do this.


How can I create a group in my query service and set a limit on the values returned?

Posted: Mon Sep 02, 2013 12:35 pm
by yramess

Let me ask you another question/solution if groups in queries are still not available.

I am sending the result query to a dataset that shows all the information that I received (group name, time and date). Considering I wouldn't want to show the same group more than once in my Data List (grid) and select only the more recent date, is there a way to sort through the result in the "Add JS" box with a foreach model?

Image

Image


How can I create a group in my query service and set a limit on the values returned?

Posted: Mon Sep 02, 2013 2:03 pm
by Serhii Kulibaba

You can use request parameters from json by executing you REST Service from code:
pre
service_name.execute({
success: function( PlainObject data, String textStatus, jqXHR jqXHR ) {
//Success handler here
},
error: function( jqXHR jqXHR, String textStatus, String errorThrown ) {
//Error handler here
},
complete: function( jqXHR jqXHR, String textStatus ) {
//Complete handler here
},
data: { object with request params },
headers: { object with request header params }
});
/pre

You can replace any parameter and handler to yours. (They are not required)
Thus you can control request at runtime.

Example for you: (run this JS, when you want get grouped data from database)
pre
var date = new Date();
var dateNow = date.getFullYear() + "-" + date.getMonth() + "-" + date.getDate();

selectTest.execute({data: {"where": '{"_updatedAt":{"$exists":"dateNow"}}',
"database_id": "516fb7c3e4b0e0d8f16ccf9a",
"X-Appery-Session-Token": "90f81cf4-f8c5-4131-9ef2-466454352aeb"}}
)
/pre
where database_id and X-Appery-Session-Token - yours.
You can change "where" param, as you want - to get current data.


How can I create a group in my query service and set a limit on the values returned?

Posted: Tue Sep 03, 2013 12:58 am
by yramess

I am not trying to get the current date data. I want to retrieve the latest (so not necessarely today, but simply the most recent value that matches a column value).

So to review what I have in a collection is:

groupname, value, userid, _updatedAt
A, 35, 222, June 22nd 2013
B, 54, 222, June 24th 2013
A, 44, 255, December 11th 2012
A, 46, 222, November 23rd 2012
B, 10, 222, July 14th 2013

What I need to receive back for userid 222 is this exactly:

A, 35, 222, June 22nd 2013
B, 10, 222, July 14th 2013

So any idea on how to get this done?


How can I create a group in my query service and set a limit on the values returned?

Posted: Tue Sep 03, 2013 5:40 am
by Serhii Kulibaba

So you should just add next request param:
"sort" = "-_updatedAt" (where "-" means sort by date descending)
"limit" = "2" (amount of results that you need)


How can I create a group in my query service and set a limit on the values returned?

Posted: Tue Sep 03, 2013 12:07 pm
by yramess

I feel like you are trying to give me a solution without reading my full question from the start and it is deceiving.

Please, if it is not clear, ask me for more details but this is the 3rd time that I get a solution that is nowhere close to what I am asking.

To simplify it even more for you:

I have animals. An animal has a number of cookies. I am updating the number of cookies with the app. I want the latest number of cookies for each animal that I own.

Full collection:

animal | cookies | owner | updatedAt
cat | 5 | sofia | July 14th 2013
cat | 6 | sofia | July 13th 2013
dog | 3 | sofia | July 14th 2013
dog | 4 | sofia | July 13th 2013
dog | 6 | sofia | July 11th 2013
bird | 1 | sofia | July 10th 2013
bird | 2 | sofia | July 9th 2013

Expected returned collection in my app:

animal | cookies | owner | updatedAt
cat | 5 | sofia | July 14th 2013
dog | 3 | sofia | July 14th 2013
bird | 1 | sofia | July 10th 2013

Solution by Sergiy:
If I use limit 3 (1 per animal right) on this table by sorting by date, I will never see the result of the bird because the updated date is too far. It is not a solution.

Solution by Max and Sergiy:
If I use a REST service with today's date, I will never see any result because the data hasn't been updated today.


How can I create a group in my query service and set a limit on the values returned?

Posted: Tue Sep 03, 2013 4:06 pm
by maxkatz

We need to check if it's possible to do this with one query. It would be possible in a relational database but we use MongoDB (NoSQL).

An alternative, first sort the collection as shown in "Full collection" and then using JavaScript search and get the values that you need. You will end up with "Expected collection".

Hope this helps...


How can I create a group in my query service and set a limit on the values returned?

Posted: Tue Sep 03, 2013 4:47 pm
by yramess

Hi Max! I also think this will need to be done in Javascript, I am just unsure how? I have tried adding javascript to the response (which I have assigned to my list) but I have no idea how to go through the value so that it can skip data by using a "continue;" model.

Can you help me with this?