Google Cloud Datastore 是否支持像 Mongodb $set 这样的部分更新?

Does Google Cloud Datastore support partial update like Mongodb $set?

我正在使用 Node.js,不想每次更新都覆盖整个实体,大多数时候只需要更新特定属性,例如计数器。

文档中没有关于如何执行此操作的明显参考。

Node.js 版本 Google Cloud Datastore 是否支持像 Mongodb $set 这样的部分更新?

除了 put() 和 putmulti() 之外,没有 DataStore API 可以更新实体,因此几乎不可能。

稍微阅读一下 from here 就知道原因了。

Internally, App Engine stores entities in protocol buffers, efficient mechanisms for serializing structured data; see the open source project page for more details. Instead of storing each entity property as an individual column in the corresponding Bigtable row, a single column is used which contains a binary-encoded protocol buffer containing the names and values for every property of a given entity.

简而言之,Datastore 中的实体行是底层存储方案中的单个二进制值,这就是为什么没有 API 进行部分更新的原因。

没有直接 API 只更新实体的一个 属性。

但是,您可以使用 Datastore transactions,而不是获取实体、修改它和更改它(使用两个不同的数据存储区 API 调用)。这是以原子方式实现此目的的推荐方法。

A transaction is a set of Google Cloud Datastore operations on one or more entities. Each transaction is guaranteed to be atomic, which means that transactions are never partially applied. Either all of the operations in the transaction are applied, or none of them are applied.

example in the documentation 正在解释如何使用事务“用相对于其当前值的新 属性 值更新实体:。这正是您要查找的内容。