2
votes

I'm developing a simple question-answers forum. I want to use Azure DocumentDB.

How can i add an answer to existing question document in DocumentDB?

In my case i've done a list of answers of the same type as question:

public List<Announcement>  { get; set; }

My document object looks as follows:

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; }

}

I don't see any update methods in DocumentClient :/.

2

2 Answers

5
votes

For me, you need to use the ReplaceDocumentAsync method in order to update your document.

As far as I can tell you need to be replacing the entire document; because when you change something you update the entire document not a part of it.

You can check the documentation about the method here

2
votes

At the moment you can't update part of the document, you have to update the whole document. Updating part of the document is a frequently requested feature and you can vote for Microsoft to add it in the future. Do so on Azure's Uservoice: https://feedback.azure.com/forums/263030-documentdb/suggestions/6693091-be-able-to-do-partial-updates-on-document


In any case, you should probably rethink whether you want to embed the answers into the question itself.

According to the article Modeling data in DocumentDB, you should embed if

  • There are contains relationships between entities.

  • There are one-to-few relationships between entities.

  • There is embedded data that changes infrequently.

  • There is embedded data won't grow without bound.

  • There is embedded data that is integral to data in a document.

If you look at the bullet points in bold, it looks like having the answers inside the question might not be a good idea as you can potentially have thousands of answers, and the question document will change very frequently.

Instead you can either have

  • each answer defined as a document with a reference to the question, so you just add an answer document when someone adds an answer. That's the easiest option.
  • or you can define documents made up of 100 answers for example so it won't grow too much either. But then that takes a bit more planning to manage.