2

I have seen the tutorial but its not clear to me how to handle the following problem and optimize my query. I am doing a query inside a query for each item, and of course this is not fine since I am sure it can be done with one query.

I have the following objects:

User - a User logged in using FB logging

Publication - a publication created by the user (has a field called createdBy with a pointer to the user

Parse.Installation - the Installation object to create a push, has a reference to the user.

Right now what I am doing is a query by date, looking for expired publications as follows:

var Publication = Parse.Object.extend("Publication");
var query = new Parse.Query(Publication);
//calculate the date here......
query.lessThan("createdAt", expirationDate);
query.find({
success: function(results) {

//here I have all the publications.
//Now I get the user
var aPub = results[i];
var user = aPub.get('createdBy');
var query2 = new Parse.Query(Parse.Installation);
query2.equalTo('user', user);

//and now I send the push for EACH user
Parse.Push.send({
where: query2,
data: {....
...
...

I am sure this can be done with one query but I don't understand how.

Thanks.

1 Answer 1

3

For the second part of your query, this is still using several queries but only one API call.

var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo("objectId",  aPub.get('createdBy')id;

// Find devices associated with these users
var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.matchesQuery('user', userQuery);

// Send push notification to query
Parse.Push.send({
    where: pushQuery,
    data: {}
}, {
    success: function() {
        // Push was successful
    },
    error: function(error) {
        // Handle error
    }
});`

in addition, you don't need to use find(). you can look into these methods:

containedIn()

matchesQuery()

matchesKeyInQuery()

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but where do I set the query.lessThan("createdAt", expirationDate); since the Query doesnt include the Publication Object ?
You should set this in the query that gets you the list of publication still. Then, use the matchesKeyInQuery() method to build the relational query to the users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.