删除parse-server中的重复条目

时间:2017-02-02 08:42:54

标签: javascript cloud-code parse-server

解析服务器不支持groupBy查询。因此,我没有调整代码来处理重复的条目,而是决定创建一个Job来清理数据。

enter image description here

我使用underscore创建了一个云功能但效果并不理想。它也删除了不重复的条目。

如果存在具有相同post_iduser_id

的其他条目,我想删除条目
    Parse.Cloud.job("removeDuplicateItems", function(request, status) {
     var _ = require("underscore");
     var hashTable = {};

      function hashKeyForTestItem(testItem) {
        var fields = ["user_id", "post_id"];
        var hashKey = "";
        _.each(fields, function (field) {
            hashKey += testItem.get(field) + "/" ;
        });
        return hashKey;
      }

      var testItemsQuery = new Parse.Query("Post_shares");
      testItemsQuery.each(function (testItem) {
        var key = hashKeyForTestItem(testItem);

        if (key in hashTable) { // this item was seen before, so destroy this
            return testItem.destroy();
        } else { // it is not in the hashTable, so keep it
            hashTable[key] = 1;
        }

      }).then(function() {
        status.success("removal completed successfully.");
      }, function(error) {
        status.error("Uh oh, something went wrong.");
      });
    }); 

有更好的方法吗?

0 个答案:

没有答案
相关问题