'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);


Solution 1:[1]

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);

Solution 2:[2]

There are a couple of ways you could do this.

I'm working on a project now where I have cells composed of users.

I currently have an afterSave trigger that does this:

   const count = await cell.relation("members").query().count();
   cell.put("memberCount",count);

This works pretty well.

There are other ways that I've considered in theory, but I've not used them yet.

The right way would be to hack the ability to use select with dot notation to grab a virtual field called relatedItems.length in the query, but that would probably only work for me because I use PostGres ... mongo seems to be extremely limited in its ability to do this sort of thing, which is why I would never make a database out of blobs of json in the first place.

You could do a similar thing with an afterFind trigger. I'm experimenting with that now. I'm not sure if it will confuse parse to get an attribute back which does not exist in its schema, but I'll find out, by the end of today. I have found that if I jam an artificial attribute into the objects in the trigger, they are returned along with the other data. What I'm not sure about is whether Parse will decide that the object is dirty, or, worse, decide that I'm creating a new attribute and store it to the database ... which could be filtered out with a beforeSave trigger, but not until after the data had all been sent to the cloud.

There is also a place where i had to do several queries from several tables, and would have ended up with a lot of redundant data. So I wrote a cloud function which did the queries, and then returned a couple of lists of objects, and a few lists of objectId strings which served as indexes. This worked pretty well for me. And tracking the last load time and sending it back when I needed up update my data allowed me to limit myself to objects which had changed since my last query.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Nobody Tells The Truth