MongoDB update deeply nested array -



MongoDB update deeply nested array -

consider next document:

{ "comments": [ { "id" : 1, "userid": "john", "comment": "lorem ipsum...", "responses": [ { "id" : 2, "userid": "smith", "comment": "lorem ipsum...", "responses": [ { "id" : 3, "userid": "random", "comment": "lorem ipsum...", "responses": [] // want force comment array }, { "id" : 4, "userid": "guy", "comment": "lorem ipsum..." } ] }, { "id" : 5, "userid": "adam", "comment": "lorem ipsum..." } ] } ] }

is there way force document responses array? in case user wants comment on level-3 comment , want force comment array. send array positions user , server when commenting i'm pretty sure that's unreliable. if delete happen in between (i guess(?)) positions alter in array, it's no-go.

if know sequence of indexes downwards level, yes:

> db.test.update({ ... }, { "$push" : { "comments.0.responses.0.responses.0.responses" : "this response" } })

the 0's indexes. however, multiply nested construction bad info modeling choice. should consider other options. perhaps docs on modeling tree structures helpful?

mongodb

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -