Update Multiple Rows Conditional

2019-08-08 03:12发布

Does anybody know a way of updating multiple rows according to a given condition using the Parse.com REST API?

Imagine a class "Location" with several locations. Each item has a column "status" that holds an enum string value like "ok" or "error".

I would like to do something that can be done in a MongoDb somehow like this

db().find(query).update({status: "ok"})

In Words:

"Find all locations that match my query and then update the status to Ok".

There's nothing written about it in the Parse.com REST API Batch Operations Documentation and I doubt that Parse.com supports this. Parse.com supports batch operations, but obviously not if they're bound to a condition.

I'm not looking for the find-all-items-and-then-update-them-item-by-item-way, as this takes way too long, we're talking about tenthousands items.

1条回答
Root(大扎)
2楼-- · 2019-08-08 03:47

There is no way to do that in one step. The closest operation to what you are looking for is the saveAll function. JS API Reference

Here is the description:

saveAll( list, options )
Saves the given list of Parse.Object. If any error is encountered, stops and calls the error handler.

Example usage:

Parse.Object.saveAll([object1, object2, ...], {
  success: function(list) {
    // All the objects were saved.
  },
  error: function(error) {
    // An error occurred while saving one of the objects.
  },
});

With this operation, you will still have to query for the objects, iterate through them and update the values, then call saveAll on the list of updated objects.

There has been a bit of confusion around the saveAll operation -- in particular, how many API requests it uses. This uncertainly is due to the fact that Parse has changed how they bill API requests and what was once true for this operation is no longer.

Per this link:

saveAll attempts to do as few API calls as possible. Usually a call to saveAll results in only one API request. However, if any of the objects in the set has a relation to another unsaved object, that object will have to be saved first, resulting in more API requests.

This is no longer true. In April 2014, Parse changed their pricing model to the requests per second metric, but later that year Parse also changed how batch API requests were counted since developers started to exploit batch operations.

Here is an excerpt of an official Parse statement on the matter:

we noticed some developers were abusing this exemption and seriously degrading performance for the other apps on their shared clusters. We investigated further and found that the cumulative impact of the mismatch between predicted resources and actual demands, even among well meaning developers, was contributing to instability in the broader system.

We removed the exemption and intend to continue with this model going forward. That said, we completely understand that this will require changes for the developers that relied on the previous model.

Using the saveAll operation today will result in 1 API request per object in the list, effectively calling save for each individual object.

Presently, there is no way to avoid having to call save on each of the modified objects. Hopefully this is something that Parse will consider adding in the future.

查看更多
登录 后发表回答