How to store many lines in DocumentDb
I have an entity on my system that will have many comments added against it in a short amount of time.
Would I be correct in saying that if I read the document and then change something in it, the entire object is saved back to storage?
If that's correct, then loading and saving a 5000 comment object so that I can add a comment seems too big?
Should I store each comment as a document and then browse the document collection for a specific key? I will also need to quickly find the comment and change it.
source to share
One massive document
If you want to keep all comments in one document, you run into several problems:
- Parallel writes - you will need to do a lot of reads and resends (non-current Etag)
- Loading a large document to add a comment
- Loading a large document to edit a comment
Pros:
- You can delete the main object with all documents in one operation.
Multiple collections
The best option, which I think, is to split the comment and main objects into two collections and bind each comment to the main object by id. This way you can add and edit one comment, and also retrieve all documents on demand. This way, you can run much more complex queries on a collection of comments, for example, get all the user's comments, etc.
Minuses:
- Removing the main object with all comments will be quite expensive (you need to remove the comment one by one)
Although you should be aware that the OrderBy clause is not supported , so you might have a problem getting for example the last 10 comments, etc.
Possible alternative
Not sure what other operations / queries you should support. But if its the only CRUD, getting the list of comments for the main entity and potentially getting the last x comments, you might consider using an Azure table where:
- Line string - Time ID (Unix epoch) + GUID to avoid conflicts.
- Section Key - Main Object ID
This way you can quickly get and update the comment object and also query it by time
source to share