With that data structure, you will have multiple (nested) queries, but done in CloudCode it should still be plenty fast when you use proper limits and promises.
Try it in curl here:
curl -X POST -H "X-Parse-Application-Id: 1ClVjWioWtW4uz8Nt7zYiXHkoNYQao6Z1rdXd8Gt" -H "X-Parse-REST-API-Key: VlYbFT8uhPpsYeRW5ezIn1A8bWa5N9OW9riqs4ji" -H "Content-Type: application/json" -d '{}' https://api.parse.com/1/functions/collectionSummary
Cloud Code:
var _ = require('underscore');
Parse.Cloud.define("collectionSummary", function(request, response) {
var collections = [];
var collectionQuery = new Parse.Query("Collection");
collectionQuery.find({
success: function(results) {
var promises = [];
_.each(results, function(collection) {
var name = collection.get("name");
var imageQuery = new Parse.Query("Image");
imageQuery.equalTo("collection", collection);
imageQuery.ascending("position");
imageQuery.limit(5);
promises.push(imageQuery.find({
success: function(images) {
if (images.length > 0) {
collections.push({ "collection":collection, "images":images });
}
},
error: function() {
response.error("Could not read images.");
}
}));
});
Parse.Promise.when(promises).then(function() {
response.success(collections);
});
},
error: function() {
response.error("Could not read collections.");
}
});
});
Output:
{
"result": [
{
"collection": {
"__type": "Object",
"className": "Collection",
"createdAt": "2015-05-11T21:52:28.406Z",
"name": "Apple",
"objectId": "cpTsJBNc9q",
"updatedAt": "2015-05-11T21:52:28.406Z"
},
"images": [
{
"__type": "Object",
"className": "Image",
"collection": {
"__type": "Pointer",
"className": "Collection",
"objectId": "cpTsJBNc9q"
},
"createdAt": "2015-05-11T22:12:35.152Z",
"objectId": "0AmtlLrlHT",
"position": 0,
"updatedAt": "2015-05-11T22:12:49.463Z",
"url": "test0"
},
{
"__type": "Object",
"className": "Image",
"collection": {
"__type": "Pointer",
"className": "Collection",
"objectId": "cpTsJBNc9q"
},
"createdAt": "2015-05-11T22:12:58.185Z",
"objectId": "WbomPr3hUK",
"position": 1,
"updatedAt": "2015-05-11T22:13:05.523Z",
"url": "test1"
},
{
"__type": "Object",
"className": "Image",
"collection": {
"__type": "Pointer",
"className": "Collection",
"objectId": "cpTsJBNc9q"
},
"createdAt": "2015-05-11T22:19:02.836Z",
"objectId": "ASLInM7Hu5",
"position": 15,
"updatedAt": "2015-05-11T22:19:08.990Z",
"url": "test8"
},
{
"__type": "Object",
"className": "Image",
"collection": {
"__type": "Pointer",
"className": "Collection",
"objectId": "cpTsJBNc9q"
},
"createdAt": "2015-05-11T22:19:14.719Z",
"objectId": "VkCF98Ts0N",
"position": 20,
"updatedAt": "2015-05-11T22:19:20.699Z",
"url": "test9"
},
{
"__type": "Object",
"className": "Image",
"collection": {
"__type": "Pointer",
"className": "Collection",
"objectId": "cpTsJBNc9q"
},
"createdAt": "2015-05-11T22:19:27.032Z",
"objectId": "I2mEG20bfp",
"position": 40,
"updatedAt": "2015-05-11T22:19:37.554Z",
"url": "test10"
}
]
},
{
"collection": {
"__type": "Object",
"className": "Collection",
"createdAt": "2015-05-11T21:52:35.297Z",
"name": "Banana",
"objectId": "zxPfnWlm1T",
"updatedAt": "2015-05-11T21:52:35.297Z"
},
"images": [
{
"__type": "Object",
"className": "Image",
"collection": {
"__type": "Pointer",
"className": "Collection",
"objectId": "zxPfnWlm1T"
},
"createdAt": "2015-05-11T22:16:05.924Z",
"objectId": "vK9O7b5vR6",
"position": 0,
"updatedAt": "2015-05-11T22:16:12.607Z",
"url": "test2"
},
{
"__type": "Object",
"className": "Image",
"collection": {
"__type": "Pointer",
"className": "Collection",
"objectId": "zxPfnWlm1T"
},
"createdAt": "2015-05-11T22:16:26.676Z",
"objectId": "oMEMgwauEi",
"position": 1,
"updatedAt": "2015-05-11T22:16:30.750Z",
"url": "test3"
},
{
"__type": "Object",
"className": "Image",
"collection": {
"__type": "Pointer",
"className": "Collection",
"objectId": "zxPfnWlm1T"
},
"createdAt": "2015-05-11T22:16:35.378Z",
"objectId": "0sAbNK1LbN",
"position": 2,
"updatedAt": "2015-05-11T22:16:39.475Z",
"url": "test4"
},
{
"__type": "Object",
"className": "Image",
"collection": {
"__type": "Pointer",
"className": "Collection",
"objectId": "zxPfnWlm1T"
},
"createdAt": "2015-05-11T22:16:42.984Z",
"objectId": "QVZJmHQJ6E",
"position": 3,
"updatedAt": "2015-05-11T22:16:52.405Z",
"url": "test5"
},
{
"__type": "Object",
"className": "Image",
"collection": {
"__type": "Pointer",
"className": "Collection",
"objectId": "zxPfnWlm1T"
},
"createdAt": "2015-05-11T22:16:56.827Z",
"objectId": "NGS39LCAJL",
"position": 4,
"updatedAt": "2015-05-11T22:17:01.401Z",
"url": "test6"
}
]
}
]
}
position= 1, or is position unique and you're just looking for the lowest number? Second, is the number ofCollectionobjects around 3 total, or 3 per user? - Ryan Kreagerpositionis relative, so each collection will have an item whereposition= 1. Secondly, the number ofCollectionobjects are total, not per user, though as you've probably gathered it's just an arbitrary figure, the real number would likely be a little higher than that - Robin Pyon