R
R
Roman2016-06-18 01:23:19
MongoDB
Roman, 2016-06-18 01:23:19

How to search deep into a structure?

there is an articles structure, comments are built into it, you need to find a specific comment and insert another comment into its children:

{  
   _id:"Some ID",
   text:"Some Article",
   comments:[  
      {  
         "_id":"some comment id1",
         "text":"some comment",
         "parent":"Some ID",
         "comments":[  
            {  
               "_id":"some comment id2",
               "text":"some comment",
               "parent":"some comment id1",
               "comments":[  
                  {  
                     ...
                  }
               ]
            }
         ]
      },
      {  
         "_id":"some comment id3",
         "text":"some comment",
         "parent":"Some ID",
         "comments":[  
            {  
               ...
            }
         ]
      }

nesting of comments can be any, I need to add a comment to another comment by its id, I don’t know at what level the parent comment is, how can I do this using a query in mongo? or is it easier and more correct to pull out the entire structure of comments into an object, walk through this tree, add a new element of the tree and update the entire tree in the mongo?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2016-06-20
@lega

If you need to access comments directly (by id), then you should move them to a separate collection, or to one "linear" level, so that it would be possible to do this. And with your structure, only write the entire block, or build a point query on this structure.
PS: Why do you need parent in a comment if it is already in some comment (already has ownership)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question