Prashant Pandey
Prashant Pandey

Reputation: 4662

Querying Nested Documents in Couchbase

I am writing a Reddit like comment store using Couchbase. For each comment, I am storing its parentId and a list of its childrenIds. Each top-level comment on a web page will have its parentId as null.

I want to retrieve a comment block efficiently. By a comment block, I mean a top-level comment along with all its children comments. So the first step in this can be to write a map function that emits the ids of all top-level comments.

How do I go about fetching the entire tree once I have the root. A very naive approach would be to find the children, and query them recursively. But this defeats the purpose of not using a relational database for this project (since I am dealing with highly nested data and relational databases are terrible at storing them).

Can someone guide me on this?

Upvotes: 1

Views: 55

Answers (1)

Johan Larson
Johan Larson

Reputation: 1890

OK, so each top-level comment has a tree of sub-comments below it. I think you could safely put the entire tree of sub-comments in the document of the top-level comment in most cases. The default limit on document sizes is 20 MB, which is a heck of a lot for text.

The question then is what to do with comments that inspire a LOT of sub-comments. I suggest you start spilling parts of the sub-comment tree to other documents when that happens, so strictly speaking there can be a tree of sub-comment trees, although typically you only need the one document. Design things so these subsidiary documents only get fetched on demand, and you never have to fetch absolutely the entire sub-comment tree.

Upvotes: 1

Related Questions