Update offspring in nested tree mongoDB, node.js -
is there way update nested documents id or other field?
i use "full tree in single document" , don't know beforehand how deep nesting can go. need update, example, answer {id:'104'}. can via 'dot notation', since don't know level (depth) of nesting can't predict how long 'comment.answers.answers....answers.'
can go.
is there way directly find , update id:'104', or still need pass kind of depth mark?
{ title:'some title', comment: { id:'101' author:'joe', text:'some comment', answers: [ { id:'102' author:'joe', text:'first answer comment', answers: [ { id:'103' author:'done', text:'first answer first answer comment', answers:[] }, { id:'104' author:'bob', text:'second answer first answer comment', answers:[] } ] }, { }, { }, ] } }
in short there's no way query of sort. there few options:
you can create query long $or
statement, specifying each of possible nested locations document:
{ comment.id: 103, $or [ { comment.answers.id: 103 }, { comment.answers.answers.id: 103 }, { comment.answers.answers.answers.id: 103 } ] }
for more information $or operator see docs.
in truth, better , more sustainable solution use different schema, comments , answers stored in flat array , store information relationships between comments in comments.parent
field. example:
{ comment: [ { id: 1 } { id: 2, parent: 1 } ] }
for additional options , more in depth discussion of possible ways of modeling comment hierarchies, view storing comments use case in mongodb documentation.
there number of strategies in trees in mongodb might want consider.
Comments
Post a Comment