Verifying uniqueness on a batch save with a huge a

2019-08-28 23:42发布

问题:

I'm doing a parse batch save requestas follows:

Parse.Object.saveAll(nameGenderArrayToSave)

nameGenderArrayToSave is an array with thousands of objects to save. I'm also interested in guarante the uniqueness of my data. So I have a beforeSave hook that do it for me:

Parse.Cloud.beforeSave("NameGender", function(request, response) {
    if (!request.object.isNew()) {
      // Let existing object updates go through
      response.success();
    }
    var query = new Parse.Query("NameGender");
    // Add query filters to check for uniqueness
    query.equalTo("name", request.object.get("name"));
    query.first().then(function(existingObject) {
      if (existingObject) {
        // Update existing object
        if (request.object.get("gender") != "U"){
            existingObject.set("gender", request.object.get("gender"));
        }
        return existingObject.save();
      } else {
        // Pass a flag that this is not an existing object
        return Parse.Promise.as(false);
      }
    }).then(function(existingObject) {
      if (existingObject) {
        // Existing object, stop initial save
        response.error("Existing object");
      } else {
        // New object, let the save go through
        response.success();
      }
    }, function (error) {
      response.error(error);
    });
});

The code above is running as expected but i'm with a performance problem since i'm triggering a find for each object i'm saving and so i'm reaching Parse request/sec limit. Could anyone help me here finding a way to solve this?