2

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 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

6 Comments

Entire document? :( that's sick. If it is true then documentdb seems wrong approach for forum database :(.
I have asked myself the same question and looks like replacedocument method is what is used to update. However, I don't know anything about the internal implementation of the method; maybe it is not replacing the entire document but the name looks so obvious.
Unfortunately it is replacing whole document. Patch functionality is not available. You can always create separated collection(or just other type of document) with answers, this way you can add and delete them without touching main document, you can always run a query to return all answers.
Thanks for your replies. I hope they'll add such functionality in release build (something like update in mongodb). Seems that there is really no other way so far. @plentysmart yeah, but in my case i would need separate doc for each answer or load/add/save_all action - with many answers that seems like bad idea. On the other hand creating hundrets of documents for single forum thread with answers seems not good idea either. I think ill stick with SQL Server for now :(
hmmm, I hope I missunderstood: What if I have an Order document with a list of order items and I need to update one specific order item. Do I need to replace all the Order document with the new one? Is there a doc about designing data for documentdb?
|
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.