How to get count of relation field in parse

1110

GameScore object have one Relation field named Badges.
How I can get count of all objects in this relation in query:

var GameScore = Parse.Object.extend("GameScore");
var query = new Parse.Query(GameScore);
query.equalTo("playerName", "Dan Stemkoski");
query.find({
  success: function(results) {
    alert("Successfully retrieved " + results.length + " scores.");
    // Do something with the returned Parse.Object values
    for (var i = 0; i < results.length; i++) {
      var object = results[i];
      alert(object.id + ' - ' + object.get('playerName'));
    }
  },
  error: function(error) {
    alert("Error: " + error.code + " " + error.message);
  }
});

I need something like:

object.Badges.count

or

object.Badges.length
paolobueno

A Parse.Relation object is actually a query description that will return the objects in that relation, so for this case you'd need to run another query for each GameScore:

query.find().then(function (gameScores) {
    alert("Successfully retrieved " + gameScores.length + " scores.");
    var countPromises = gameScores.map(function (gs) {
        // the following executes a count() query, which is severely penalized by Parse
        return gs.get('Badges').query().count()
            .then(function (count) {
                // this extra step is to add the retrieved count as an extra property to the GameSccore object,
                // instead of returning only the counts
                gs.count = count; 
                return gs;
            });
    });
    return Parse.Promise.when(countPromises);
}).then(function () {
    var gameScoresWithBadgeCount = Array.prototype.slice.call(arguments);
}).fail(function(error) {
    alert("Error: " + error.code + " " + error.message);
});

This causes a lot of extra round trips (I assume you're on a browser environment because of alert()), and calls count() queries which are additionally limited by Parse.

What I can recommend you is to keep a count cache as an extra field on the GameScore class, and update it accordingly through CloudCode hooks. Alternatively, you can try to avoid the Relation and make the equivalent using an Array field if possible, through which you can always include the related Badges if needed or get their count without querying for them at all!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

count query - how to get relation instead of hash

From Dev

Laravel get relation count where a field in the database is set to a certain value

From Dev

How to get user from relation using Parse Javascript for android app

From Dev

Yii2 - How to get field value from relation

From Dev

Get relation count Laravel JSON

From Dev

How to get an extra count field with Django ORM?

From Dev

How to get the count of objects in ManyToMany field in template

From Dev

How to get all max count with a field

From Dev

How to get all max count with a field

From Dev

how can get count field in crirteria?

From Dev

How to get an extra count field with Django ORM?

From Dev

How to get the count of objects in ManyToMany field in template

From Dev

Laravel: Order query by count id field on relation

From Dev

How to use SPARQL query to get field and field count at the same time?

From Dev

How to delete a relation of a object in Parse?

From Dev

How to get COUNT and INNER JOIN related with concated value field in mysql

From Dev

How to get count from first table field when joining

From Dev

How to get COUNT and INNER JOIN related with concated value field in mysql

From Dev

How can I get a total count of an int field in a List?

From Dev

How to get count of particular field using case in MySQL

From Dev

Python sqlalchemy: How do I use the relation to get count data from many to many

From Dev

How to use Relation fields in parse.com

From Dev

How to Count Number of in in a field

From Dev

how to fetch count of items in relation table in mysql

From Dev

How to find MAX of COUNT Result for Relation

From Dev

How to get property on relation in blade

From Dev

get Parse.com Relation user to Listview (from an Object)?

From Dev

How to parse the field value in logstash?

From Dev

Laravel: get models if count of relation has some condition

Related Related

  1. 1

    count query - how to get relation instead of hash

  2. 2

    Laravel get relation count where a field in the database is set to a certain value

  3. 3

    How to get user from relation using Parse Javascript for android app

  4. 4

    Yii2 - How to get field value from relation

  5. 5

    Get relation count Laravel JSON

  6. 6

    How to get an extra count field with Django ORM?

  7. 7

    How to get the count of objects in ManyToMany field in template

  8. 8

    How to get all max count with a field

  9. 9

    How to get all max count with a field

  10. 10

    how can get count field in crirteria?

  11. 11

    How to get an extra count field with Django ORM?

  12. 12

    How to get the count of objects in ManyToMany field in template

  13. 13

    Laravel: Order query by count id field on relation

  14. 14

    How to use SPARQL query to get field and field count at the same time?

  15. 15

    How to delete a relation of a object in Parse?

  16. 16

    How to get COUNT and INNER JOIN related with concated value field in mysql

  17. 17

    How to get count from first table field when joining

  18. 18

    How to get COUNT and INNER JOIN related with concated value field in mysql

  19. 19

    How can I get a total count of an int field in a List?

  20. 20

    How to get count of particular field using case in MySQL

  21. 21

    Python sqlalchemy: How do I use the relation to get count data from many to many

  22. 22

    How to use Relation fields in parse.com

  23. 23

    How to Count Number of in in a field

  24. 24

    how to fetch count of items in relation table in mysql

  25. 25

    How to find MAX of COUNT Result for Relation

  26. 26

    How to get property on relation in blade

  27. 27

    get Parse.com Relation user to Listview (from an Object)?

  28. 28

    How to parse the field value in logstash?

  29. 29

    Laravel: get models if count of relation has some condition

HotTag

Archive