Skip to content
Advertisement

Parse.com relations count

I want to query object from Parse DB through javascript, that has only 1 of some specific relation object. How can this criteria be achieved? So I tried something like this, the equalTo() acts as a “contains” and it’s not what I’m looking for, my code so far, which doesn’t work:

var query = new Parse.Query("Item");
query.equalTo("relatedItems", someItem);
query.lessThan("relatedItems", 2);

Advertisement

Answer

It seems Parse do not provide a easy way to do this.

Without any other fields, if you know all the items then you could do the following:

var innerQuery = new Parse.Query('Item');
innerQuery.containedIn('relatedItems', [all items except someItem]);
var query = new Parse.Query('Item');
query.equalTo('relatedItems', someItem);
query.doesNotMatchKeyInQuery('objectId', 'objectId', innerQuery);    
...

Otherwise, you might need to get all records and do filtering.

Update

Because of the data type relation, there are no ways to include the relation content into the results, you need to do another query to get the relation content.

The workaround might add a itemCount column and keep it updated whenever the item relation is modified and do:

query.equalTo('relatedItems', someItem);
query.equalTo('itemCount', 1);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement