如何批量更新User类中的所有对象(Parse.com,Android)?

问题描述 投票:0回答:3

我最初犯了一个错误,忘记在用户注册时初始化我的数组字段(设计),因此在我的 Parse 仪表板中,行显示为

(undefined)
,我希望它们是
[]
。我必须为大约 5000 个用户执行此操作。我尝试了以下代码:

var query = new Parse.Query("User");
query.equalTo("designs", "(undefined)");
query.each(function(obj) {
  obj.set("designs", "[]");
  return obj.save();
}).then(function() {
  // All objects updated.
}, function(err) {
  console.log(err);
});

我认为

(undefined)
应该是其他东西,但我不确定是什么,因为它可能不是字符串。我该如何解决这个问题?

更新:第二次尝试。是不是越来越近了?

    private void updateAllRows(final int skip) {

    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.setLimit(500);
    query.setSkip(skip);
    query.whereDoesNotExist("designs");
    query.findInBackground(new FindCallback<ParseUser>() {

        @Override
        public void done(List<ParseUser> results, ParseException e) {
            if (results.size() > 0) {

                // The query was successful.
                for (int i = 0; i < results.size(); i++) {
                    ParseUser user = results.get(i);
                    String[] designs = new String[0];
                    user.put("designs", Arrays.asList(designs));

                    try {
                        ParseUser.saveAll(results);
                        if (results.size() >= 500) {
                            updateAllRows(skip + 500); // make a recursion call with different skip value
                        }
                    } catch (ParseException e1) {
                        e1.printStackTrace();
                    }
                }

                Log.d(getClass().toString(), ": The batch job was successfully completed.");

            } else {
                // The query was unsuccessful.
                Log.d(getClass().toString(), ": The batch job was not successful.");
            }
        }
    });
}

我使用类的 onCreate 方法中的以下内容调用上述函数:

int skip = 0;
updateAllRows(skip);

这是在不使用云代码的情况下解决此问题的最佳方法吗?

错误:

08-08 12:38:43.034 24003-24053/? E/HttpOperation: [luq{checkandengageuser, getmobileexperiments}] Unexpected exception
                                                  java.io.IOException: 1 of 2 operations in the batch failed
                                                      at luj.a(PG:268)
                                                      at lth.o(PG:10642)
                                                      at ltc.a(PG:1402)
                                                      at lth.i(PG:154)
                                                      at ly.f(PG:51849)
                                                      at dsr.a(PG:1255)
                                                      at dsr.a(PG:20077)
                                                      at dsr.a(PG:706)
                                                      at dss.run(PG:1548)
                                                      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
                                                      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                      at java.lang.Thread.run(Thread.java:818)
java android parse-platform parse-server
3个回答
5
投票

JavaScript 版本 1. 您可以使用 Parse.Object.saveAll 在一次服务调用中保存多个对象 2. 您必须使用 limitskip 才能一次处理 500 个(或更少)个项目。 3. 您需要使用 query.doesNotExist("designs"); 才能获取 design 列中包含 undefined 的所有项目

function updateAllRows(skip) {

  var query = new Parse.Query(Parse.User);
  query.limit(500);
  query.skip(skip);
  // get recors where designs is not exist or is undefined      
  query.doesNotExist("designs");

  query.find().then(function(results) {

    // if we got results then change the designs property 
    if (results.length > 0) {

      for (var i = 0; i < results.length; i++) {
        var item = results[i];
        item.set("designs", []);
      }

      Parse.Object.saveAll(results).then(function() {
        // if we got 500 or more results then we know
        // that we have more results
        // otherwise we finish
        if (results.length >= 500) {
          updateAllRows(skip + 500); // make a recursion call with different skip value
        }

      }, function(err) {
        // error occured 
      });
    }
  }, function(error) {
    // error occured while trying to fetch data 
  });
};

Android Java版本

private void updateAllRows(final int skip){

    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.setLimit(500);
    query.setSkip(skip);
    query.whereDoesNotExist("designs");

    query.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(final List<ParseUser> users, ParseException e) {

            if (e == null){
                for (ParseUser user : users){
                    user.put("designs",new ArrayList<>());
                }

                // save all objects in one server call
                ParseObject.saveAllInBackground(users, new SaveCallback() {
                    @Override
                    public void done(ParseException e) {

                        if (e == null){
                            if (users.size() >= 500){
                                updateAllRows(skip + 500);
                            }
                        } else {
                            // error  while saving objects
                        }


                    }
                });
            }
        }
    });
}

}

顺便说一句!您还可以在云代码中创建云函数并从客户端触发该函数


0
投票

Javascript版本:

async updateAllRows(skip) {
    const query = new Parse.Query(Question);
    query.limit(500);
    query.skip(skip);
    query.doesNotExist("update");
    try {
      const results = await query.find();
      for (const result of results) {
        result.set('update', 'your update')
      }
      await Parse.Object.saveAll(results)
      if (results.length >= 500){
        updateAllRows(skip + 500).then().catch();
      }
    } catch (e) {
    }
  }

0
投票

我只是纠正了上面的答案,参数跳过保持在 0,因为数据已更新

function updateAllRows(skip) {
var query = new Parse.Query(Parse.User);
query.limit(500);
query.skip(skip);
// get recors where designs is not exist or is undefined    
query.doesNotExist("designs");
query.find().then(function(results) {

// if we got results then change the designs property 
if (results.length > 0) {

  for (var i = 0; i < results.length; i++) {
    var item = results[i];
    item.set("designs", []);
  }

  Parse.Object.saveAll(results).then(function() {
    // if we got 500 or more results then we know
    // that we have more results
    // otherwise we finish
    if (results.length >= 500) {
      updateAllRows(skip); // make a recursion call with different skip value
    }

  }, function(err) {
    // error occured 
  });
}
}, function(error) {
// error occured while trying to fetch data
});
};

模拟,有3850条数据可供更新

0 ->测试 1000 0 ->测试 1000 0 ->测试 1000 0 ->测试 850

© www.soinside.com 2019 - 2024. All rights reserved.