How to filter through possibly infinitely nested data NoSQL?

Viewed 23

I'm new to NoSQL so I might be wrong in my thinking process, but I am trying to figure out how to filter through possibly infinitely nested object (comment, replies to comment, replies to replies to comment). I am using MongoDB, but it probably applies to other NoSQL databases too.

This is the structure I wanted to use:

Post

{
 "name": "name",
 "comments": [
   {
     "id": "someid"
     "author": "author",
     "replies": [
       {
         
         "id": "someid",
         "author": "author",
         "replies": [
           {
             ...
           }
         ]
       },
       {
         "id": "someid",
         "author": "author",
         "replies": null
       }
     ]
   }
 ]
}

As you can see, replies can be infinitely nested. (well, unless i set the limit which doesn't sound that stupid)

But now if user wants to edit / delete comment, I have to filter through them and find the one and I can't find any better way than to loop through all of them, but that would be very slow with a lot of comments.

I was thinking to create ID for each comment that would somewhat help finding it (something inspired from hashmap, but not exactly). It could maybe include depth (how deep nested is comment) and then only filter through comments with at least that depth, but that little help would only increase performance slightly and only in specific cases, in worst case I would have to loop through all of them anyway. ID could also include indexes of comments and replies, but that would be limited since ID can't be infinite and replies can.

I couldn't find any MongoDB query for that.

Is there any solution / algorithm to do it more efficiently?

0 Answers
Related