有条件地更新多行 [英] Update Multiple Rows Conditional

查看:97
本文介绍了有条件地更新多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道使用Parse.com REST API根据给定条件更新多行的方法吗?

想象一下一个带有多个位置的位置"类.每个项目都有一个状态"列,其中包含一个枚举字符串值,例如确定"或错误".

我想做一些可以在MongoDb中完成的事情

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

字词:

查找与我的查询匹配的所有位置,然后将状态更新为 好".

Parse.com REST API批处理操作文档中没有关于它的任何文章,我怀疑Parse.com是否支持这一点. Parse.com支持批量操作,但显然不支持重新绑定到条件.

我不是要逐项查找所有项目,然后再更新它们,因为这花费的时间太长了,所以我们谈论的是上万个项目.

解决方案

无法一步一步完成. saveAll函数是您所寻找的最接近的操作. JS API参考

这里是描述:

saveAll(list,options)
保存Parse.Object的给定列表.如果有的话 遇到错误,停止并调用错误处理程序.

示例用法:

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.
  },
});

通过此操作,您仍然必须查询对象,遍历它们并更新值,然后在已更新对象列表上调用saveAll.

saveAll操作存在一些混乱-特别是它使用了多少个API请求.不确定地是由于以下事实:解析已更改了他们对API请求的计费方式,而此操作曾经一度不再适用.

通过此链接:

saveAll尝试执行尽可能少的API调用.通常会打给 saveAll仅在一个API请求中产生结果.但是,如果有 集合中的对象与另一个未保存的对象有关系,即 对象必须首先保存,从而导致更多的API请求.

这不再成立 . 2014年4月,Parse将其定价模型更改为每秒请求指标,但那年晚些时候,Parse 更改了对批处理API请求的计数方式自开发人员开始利用批处理操作.

此处是此事件的官方Parse声明的摘录:

我们注意到一些开发人员正在滥用这项豁免, 严重降低了其他应用在共享上的性能 集群.我们进行了进一步调查,发现累积影响 预测资源与实际需求之间的不匹配,甚至 在善意的开发人员中,导致开发中的不稳定 更广泛的系统.

我们取消了豁免,并打算继续采用这种模式 向前.也就是说,我们完全理解这将需要 对于依赖先前模型的开发人员进行了更改.

今天使用saveAll操作将导致列表中的每个对象1个API请求,从而为每个单独的对象有效地调用save.

目前,无法避免必须对每个已修改对象调用save的方法.希望这是Parse将来会考虑添加的内容.

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.

解决方案

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.

这篇关于有条件地更新多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆