c# - Azure DocumentDB - 将对象插入文档数组?

标签 c# azure azure-cosmosdb

我正在开发一个简单的问答论坛。我想使用 Azure DocumentDB。

如何向 DocumentDB 中的现有问题文档添加答案?

就我而言,我已经制作了与问题相同类型的答案列表:

public List<Announcement>  { get; set; }

我的文档对象如下所示:

public class Announcement
{
    [JsonProperty(PropertyName = "id")]
    public string ID { get; set; }

    [JsonProperty(PropertyName = "userid")]
    public int UserID { get; set; }

    [JsonProperty(PropertyName = "username")]
    public string UserName { get; set; }

    [JsonProperty(PropertyName = "useripaddress")]
    public string UserIPAddress { get; set; }

    [JsonProperty(PropertyName = "body")]
    public string Body { get; set; }

    [JsonProperty(PropertyName = "tags")]
    public List<string> Tags { get; set; }

    [JsonProperty(PropertyName = "datecreate")]
    public DateTime DateCreate { get; set; }

    [JsonProperty(PropertyName = "isedited")]
    public bool IsEdited { get; set; }

    [JsonProperty(PropertyName = "dateedit")]
    public DateTime DateEdit { get; set; }

    [JsonProperty(PropertyName = "imageaddonid")]
    public string ImageAddonId { get; set; }

    [JsonProperty(PropertyName = "imageaddonsource")]
    public string ImageAddonSource { get; set; }

    [JsonProperty(PropertyName = "answers")]
    public List<Announcement>  { get; set; }

}

我在 DocumentClient 中没有看到任何更新方法:/。

最佳答案

对我来说,您需要使用 ReplaceDocumentAsync 方法来更新文档。

据我所知,您需要替换整个文档;因为当您更改某些内容时,您会更新整个文档而不是其中的一部分。

可以查看该方法的文档 here

关于c# - Azure DocumentDB - 将对象插入文档数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26960916/

相关文章:

c# - 带有显示错误的 Internet 代理服务器的 Windows WCF 客户端服务器违反了协议(protocol)。部分=响应状态行

powershell - 使用 Powershell 2 部署 Azure 项目时出现问题

mongodb - Azure Cosmos DB 的 MongoDB API 中的变更流支持有多可靠?

c# - 使用 cookie 的 ASP.NET web api "Remember Me"功能

c# - Xamarin ios 可达性不起作用

c# - Office 365 统一 api(预览版)组文件仅在某些租户中返回 500 内部服务器错误

azure - 具有 LetsEncrypt 的 AKS 以及针对不同容器的多个证书

c# - Entity Framework Core 和 Cosmos DB。使用 LINQ 表达式读取实体

azure - 在 Cosmos Db 中执行包含 Node 模块的 UDF(用户定义函数)

c# - XDocument 文本节点换行