4

Consider the following 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": [] // I want to push a comment into this array
                        },
                        {
                            "id" : 4,
                            "userid": "guy",
                            "comment": "lorem ipsum..."
                        }
                    ]
                },
                {
                    "id" : 5,
                    "userid": "adam",
                    "comment": "lorem ipsum..."
                }
            ]
        }
    ]
}

Is there ANY way to push a document into that responses array? So in this case a user wants to comment on a level-3 comment and I want to push that comment into the array. I could send the array positions to the user and back to the server when commenting but I'm pretty sure that's unreliable. Also if a delete would happen in between then (I guess(?)) the positions would change in the array, so it's really a no-go.

7
  • Anytime I need to update/edit a document I use Robomongo. Its quick and easy Commented Nov 4, 2014 at 15:19
  • Well that doesn't really solve my problem. Commented Nov 4, 2014 at 15:56
  • Do you need to update the document by writing a command? Commented Nov 4, 2014 at 15:58
  • I need to update the document automatically on the backend in Go. Commented Nov 4, 2014 at 16:00
  • do all comments have globally unique IDs, regardless of where they sit in the nesting level? Commented Nov 4, 2014 at 17:34

1 Answer 1

1

If you know the sequence of indexes to get down to that level, then yes:

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

The 0's are the indexes. However, a multiply nested structure like this is almost certainly a bad data modeling choice. You should consider other options. Perhaps the docs on modeling tree structures will be helpful?

Sign up to request clarification or add additional context in comments.

1 Comment

Since it's a web app I wouldn't rely on the indexes, anything could happen. I'm not sure though, how should I remodel it. Only thing I need is to be able to sort the comments by the date field (it's not in the original question) in every level. I'm guessing I should use a single array and push all comments in that with a parent ID and do the rest on the application level. But I'm open for any other (better?) suggestions if you have one. (Or use aggregation framework, but would that be better performance wise?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.