Is there a builtin / simple way to get distinct rows?
I need to combine it with the most recent row per distinct item, too.
Is there a builtin / simple way to get distinct rows?
I need to combine it with the most recent row per distinct item, too.
In the Javascript documentation, distinct
queries is listed & explained.
It is not in the iOS SDK. And in my project I have tried the code completion in Xcode but .distinct
does not exist.
Pointers for how to implement this? Or will it require some post-processing?
Mobile SDKs doesn’t have all abilities of Js SDK. You can create a cloud code and use js SDK to run your query then send it to device.
Good to know. I had saw some suggestions along similar lines to use Cloud Code.
Generally, what performance differences / implications are involved here?
I was thinking of using an “event” style table, where as something happens, a new row is added for that user. This query would then choose the latest (createdAt) row per user (the distinct part). Would a sensible alternative be to simply have 1 row per user, which is updated (updatedAt) whenever the event occurs? I wouldn’t have the history of all events that occurred though.
The SDKs are wrappers of the REST API. And it’s difficult for an open source project to keep all the SDKs in Sync. The JS SDK implementation is the most complete wrapper of the REST API once they have the same core language.
For the REST API you can use distinct in queries https://docs.parseplatform.org/rest/guide/#distinct-queries.
For your specific case you will need to get the $max of createdAt https://docs.parseplatform.org/rest/guide/#aggregate-queries
So you can do a manual NSURLConnection
to the Parse Server with credentials and get the value.
Wow - I had not realised this and sounds like a game-changer… So I can just make URL requests against the APIs? This would provide feature parity, effectively, with the latest updates, etc? (based upon the API is the priority product).
Would this return PFObjects into the Xcode project? Or JSON?
I am only learning the Swift implementation of the SDK, and only spent the last few days reading about Javascript, while trying to overcome the behaviour I am looking for and limited knowledge.
My best guess is I can “chain” the distinct
and max
query methods. This would be 2 calls / queries to the DB, so would this not be better as Cloud Code to minimise traffic?
And would this be the best “pattern” for this style of regular updates to the app? I would have thought a simple “select * from class” would minimise effort (processing, network and data flows) at both server and client…?
@rhuanbarreto, I have tried combining my 2 queries:
Distinct - to get an array of unique players
Pass output of above into a query.containedIn(), followed by query.aggregate
This returns ONLY the single most latest item from the 2nd query, not the latest item PER player from the 1st query…
Code is:
Parse.Cloud.define("getLeaderboard", (request) => {
var query = new Parse.Query("Leaderboard");
query.distinct("playerName")
.then(function(results) {
console.log("Distinct: " + results);
var pipeline = [{ group: { objectId: null, latest: { $max: '$updatedAt'}}}];
var queryLatest = new Parse.Query("Leaderboard");
queryLatest.containedIn("playerName", results);
queryLatest.aggregate(pipeline)
.then(function(results) {
console.log("Max: " + results);
})
.catch(function(error) {
console.log("Error: " + error);
});
})
.catch(function(error) {
// There was an error.
console.log(error);
});
});
Returns:
Distinct: Player 1,Player 2,Player 3,Player 4,Player 5,
verbose: REQUEST for [GET] /parse/aggregate/Leaderboard: {,
“pipeline”: [,
{,
“group”: {,
“objectId”: null,
“latest”: {,
“$max”: “$updatedAt”,,
},
},
},
],
} {“method”:“GET”,“url”:“/parse/aggregate/Leaderboard”,“headers”:{“user-agent”:“node-XMLHttpRequest, Parse/js2.12.0 (NodeJS 12.16.1)”,“accept”:“/”,“content-type”:“text/plain”,“host”:“my-parse-host.com:1337”,“content-length”:“241”,“connection”:“close”},“body”:{“pipeline”:[{“group”:{“objectId”:null,“latest”:{“$max”:“$updatedAt”}}}]}},
verbose: RESPONSE from [GET] /parse/aggregate/Leaderboard: {,
“response”: {,
“results”: [,
{,
“latest”: {,
“__type”: “Date”,
“iso”: “2020-07-07T21:28:56.821Z”,
},
“objectId”: null,
},
],
},
} {“result”:{“response”:{“results”:[{“latest”:{“__type”:“Date”,“iso”:“2020-07-07T21:28:56.821Z”},“objectId”:null}]}}},
Max: [object Object],
First you must understand the query you are trying to do. If you want to use any aggregation like max, you need to have something to group by. The same as normal SQL.
Most of parameters you use in Parse SDK are queries that you can normally run in a mongoDB shell.
I’m not an iOS developer… I can’t help with this.