Cloud Datastore 插入和更新插入问题
Cloud Datastore Insert and Upsert Problems
我在实体键下插入额外的 属性 时遇到一个奇怪的问题。
我试图在不修改现有数据的情况下再添加一个 属性,但是,我的以下代码创建了新的 属性 并删除了现有数据(显示空白)
根据 GCP Doc,我尝试使用 Insert 和 upsert not issue not got fixed
server.post('/submit', (req, res,) => {
const OutTime = new Date();
const key = datastore.key([VData, 5717023518621696])
const entity = {
key:key,
method: 'insert',
data: { content: OutTime },
}
datastore.insert(entity).then(() => {
// Task inserted successfully.
});
// [END datastore_upsert]
});
这是预期的,您不能 add/modify 只是现有实体的 属性 - 整个实体需要重写。因此,您需要 get
实体,将新的 属性 添加到其中,然后 update
/upsert
将其返回到数据存储区。
来自您引用的文档中的 Updating an entity(强调我的):
The provided data overwrites the existing entity. The entire object must be sent to Cloud Datastore. If the entity does not exist, the
update will fail. If you want to update-or-create an entity, use
upsert
as described previously. Using a transaction allows you
to perform the get
and update
operations in a single atomical
transaction.
来自 Creating an entity(强调我的):
You can save the entity to Cloud Datastore using upsert
(which
will overwrite an entity if it already exists in Cloud Datastore)
我在实体键下插入额外的 属性 时遇到一个奇怪的问题。
我试图在不修改现有数据的情况下再添加一个 属性,但是,我的以下代码创建了新的 属性 并删除了现有数据(显示空白) 根据 GCP Doc,我尝试使用 Insert 和 upsert not issue not got fixed
server.post('/submit', (req, res,) => {
const OutTime = new Date();
const key = datastore.key([VData, 5717023518621696])
const entity = {
key:key,
method: 'insert',
data: { content: OutTime },
}
datastore.insert(entity).then(() => {
// Task inserted successfully.
});
// [END datastore_upsert]
});
这是预期的,您不能 add/modify 只是现有实体的 属性 - 整个实体需要重写。因此,您需要 get
实体,将新的 属性 添加到其中,然后 update
/upsert
将其返回到数据存储区。
来自您引用的文档中的 Updating an entity(强调我的):
The provided data overwrites the existing entity. The entire object must be sent to Cloud Datastore. If the entity does not exist, the update will fail. If you want to update-or-create an entity, use
upsert
as described previously. Using a transaction allows you to perform theget
andupdate
operations in a single atomical transaction.
来自 Creating an entity(强调我的):
You can save the entity to Cloud Datastore using
upsert
(which will overwrite an entity if it already exists in Cloud Datastore)