1
votes

My db save user location by country:

  1. name | surename | NY
  2. name | surename | London
  3. name | surename | France
  4. name | surename | NY
  5. name | surename | France
  6. name | surename | NY

I'm trying to get the next list by some query:

["NY","London","France"] OR [{"NY":3},{"London":1},{"France":2}]

Is there any aggregations query same as Elasticsearch?

I want to get only Country list of all my users

2

2 Answers

0
votes

Unfortunately, there is no functionality for aggregation queries with Parse.

If you want to get a list of all the countries for all your users, you will have to query for all users first and then construct your own aggregation using the results.

0
votes

SOLVED

I made aggregation for primitives, good for all platforms

just copy paste - good up-to 10,000 results:

Cloud Code:

    Array.prototype.contains = function(k) {
      for(var i=0; i < this.length; i++){
        if(this[i] === k){
          return true;
        }
      }
      return false;
    }

    skip = 0;
    limit = 1000;
    var res = {
        time:null,
        objects:new Array()
    }
    var regularList = 0;
    var countList = 1;
    var tmpArray = new Array();
    var start;
    Parse.Cloud.define("aggregation", function(request, response) {
        start = new Date().getTime();
        pagingQuery(request.params.listType,request.params.className,request.params.columnToAggregate,response);
    });

    function pagingQuery(listType,className,columnToAggregate,response){
        var object = Parse.Object.extend(className);
        var query = new Parse.Query(object);
        query.limit(limit);
        query.skip(skip * limit)
        query.find({
          success: function(results) {
            for(var i=0; i<results.length;i++){
                    switch(listType){
                        case regularList:
                            if (!tmpArray.contains(results[i].get(columnToAggregate))) {
                                tmpArray.push(results[i].get(columnToAggregate));
                            }
                            break;
                        case countList:
                            var obj = tmpArray.filter(function ( obj ) {
                                return obj.name === results[i].get(columnToAggregate);
                            })[0];
                            if(obj == null){
                                var agg = {
                                    name:results[i].get(columnToAggregate),
                                    count:1
                                }
                                tmpArray.push(agg);
                            }else{
                                obj.count++;
                            }

                            break;

                }
            }           
            if(results.length == limit){
                skip++;
                pagingQuery(type,className,columnToAggregate,response);
            }else{
                var end = new Date().getTime();
                var t = end - start;
                res["time"] = t;
                res["objects"] = tmpArray;
                response.success(res);
            }
          },
          error: function(error) {
                response.error("Error: " + error.code + " " + error.message);
          }
        });
    }

now you can easily make any aggregation you want:

0 - regular -- ["NY","London","France"]

1 - count -- [{name:"NY",count:3},{name:"London",count:1},{name:"France",count:2}]

in my case i'm using Javascript SDK but it is the same for all platforms (Android, IOS ...): you need to supply your Class name & column to aggregate:

Client:

var regularList = 0;
var countList = 1;  
params = {
    className: 'YourClass',
    columnToAggregate: 'City',
    listType: countList
};
Parse.Cloud.run('aggregation', params, {
     success: function(result) {
        console.log(result);
     },
     error: function(error) {
        console.log(error);
      }
});

also you get 'time' in any query, so you can know how many time query takes in milliseconds, as in ElasticSearch