现有 Cosmos DB 文档需要使用新属性进行更改/更新,其他集合的现有文档也需要使用相同的新属性及其值进行更新。
是否有任何推荐的方法或工具可用于更新 Cosmos DB 上的现有文档,或者使用 Cosmos DB SDK 编写自定义 C# 应用程序/PowerShell 脚本是唯一的选择吗?
示例:
现有用户
文档
{
id:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b3e382e397a0b262a222765282426" rel="noreferrer noopener nofollow">[email protected]</a>,
name: "abc",
country: "xyz"
}
更新了用户
文档
{
id:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a5f594f581b6a474b434604494547" rel="noreferrer noopener nofollow">[email protected]</a>,
name: "abc",
country: "xyz",
guid:"4334fdfsfewr" //new field
}
用户
现有订单
文档
{
id:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4d1d7c1d695e4c9c5cdc88ac7cbc9" rel="noreferrer noopener nofollow">[email protected]</a>,
user: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="304543554201705d51595c1e535f5d" rel="noreferrer noopener nofollow">[email protected]</a>",
date: "09/28/2020",
amt: "$45"
}
更新了用户
的order
文档
{
id:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="760305130447361b171f1a5815191b" rel="noreferrer noopener nofollow">[email protected]</a>,
user: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbcec8dec98afbd6dad2d795d8d4d6" rel="noreferrer noopener nofollow">[email protected]</a>",
userid: "4334fdfsfewr", // new field but with same value as in user model
date: "09/28/2020",
amt: "$45"
}
最佳答案
我可能会选择:
- 通过脚本更新用户文档
- 拥有带有 Cosmosdb 触发器的 Azure Function,可以监听用户文档的更改并相应地更新订单
[更新] 无论您最喜欢哪种类型的脚本:PS、C#、Azure Functions... 现在,您的意思是它们需要“同时”使用新属性进行更改?我不确定这是否可能。如果你想要这样的效果那么我想你最好的选择是:
- 为用户创建新的集合/容器
- 拥有一个 Azure 函数,用于监听现有用户容器的更改源(因此,使用 StartFromBeginning 选项)
- 更新您的文档以添加新字段并将其存储在新创建的容器中
- 完成后,将您的应用切换为使用新容器
您可以选择如何更改其他集合(订单):使用旧用户或新用户容器中的changeFeed和Azure Functions。
PS。 是的,无论我采用什么流程,它仍然是带有 Cosmos DB 触发器的 Azure Functions。关于Azure Cosmos DB - 使用附加字段更新现有文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64103393/