Question

I have classes named _User & Photo & PhotoJourney. I want to send a Photo to a random user based on some action. In the PhotoJourney class I have a field photo (Pointer to Photo class) and photoSentTo (Pointer to _User class).

My requirement is to get all users excluding those whose photo has been sent.

var PhotoObj = Parse.Object.extend("Photo");
var photo = new PhotoObj();
photo.id = request.params.photoId;

var Query1 = new Parse.Query("User");

var Query2 = new Parse.Query("PhotoJourney");
Query2.select("photoSentTo");
Query2.equalTo("photoID", photo);

Query1.doesNotMatchKeyInQuery("objectId", "photoSentTo", Query2);
Query1.find({
    success: function(results)
    {
    },
    error : function() 
    {
    }
});

If I execute Query2.find(), it gives me the correct result. i.e. a list of users that I want to exclude from _User table, but I am not sure how to do that and which parameters to pass on to doesNotMatchKeyInQuery("","","").

I am quite new to cloud code development. Please correct me if I am doing it wrong.

Thanks in advance.

Was it helpful?

Solution

Sorry, I actually don't think you can solve this with queries only. I think you need to do it like this:

  1. Get a list of all PhotoJourney objects where photo matches the current photo.
  2. Create a list of user objects by retrieving the photoSentTo object in all PhotoJourney objects (excludeUsers)
  3. Query for all users (allUsers)
  4. As you now have two lists of user objects, filter out the objects from allUsers that exist in excludeUsers

The resulting list is the list you can use for getting a random user.

This is quite a heavy operation if you have lots of users. You should create a cloud code function that does this job on the server and only returns the random user object.

As an alternative, you could create a new field on the Photo object (or create a join table for it) that contains an array of pointers to all the users that have received the Photo. Then, when querying for users, you can exclude users that exist in this list. Or the array could contain only the objectIDs, and you can use that as a constraint in your User query:

userQuery.notContainedIn("objectId", photo.get("sentToUsers")); // Array of objectIds of receivers
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top