0

Hello I am fairly new to mongodb, and honesty its still a bid confusing for me since I am used to mysql, and also less knowledgeable with json.

Here is my document from the collection Discussion

{
        "_id" : ObjectId("5188c93f0361ca6dc33e3a30"),
        "admin" : [ ],
        "created" : "2013-04-30 19:10:21",
        "description" : "guitar theory",
        "members" : [ ],
        "modified" : "2013-04-30 19:10:21",
        "name" : "Arpeggios",
        "posts" : [
                {
                        "post_id" : "1",
                        "user_id" : "1",
                        "name" : "Test",
                        "slug" : "xxx",
                        "comment" : "xxx",
                        "created" : "xxx",
                        "modified" : "xxx",
                        "comments" : [ ],
                        "attachments" : [ ]
                },
                {
                        "post_id" : "2",
                        "user_id" : "1",
                        "name" : "Test",
                        "slug" : "xxx",
                        "comment" : "xxx",
                        "created" : "xxx",
                        "modified" : "xxx",
                        "comments" : [ ],
                        "attachments" : [ ]
                }
        ],
        "profile_pic" : "adasdad",
        "settings" : [ ],
        "slug" : "arpeggio"
}

my goal is to push an element on the array comments which has post_id = 1, to get the picture this is the result I want:

{
        "_id" : ObjectId("5188c93f0361ca6dc33e3a30"),
        "admin" : [ ],
        "created" : "2013-04-30 19:10:21",
        "description" : "guitar theory",
        "members" : [ ],
        "modified" : "2013-04-30 19:10:21",
        "name" : "Arpeggios",
        "posts" : [
                {
                        "post_id" : "1",
                        "user_id" : "1",
                        "name" : "Test",
                        "slug" : "xxx",
                        "comment" : "xxx",
                        "created" : "xxx",
                        "modified" : "xxx",
                        "comments" : [ 
                        {"comment_id":"xxx", "user_id":"xxx", "name":"xxx","comment":"xxx", "created":"xxx", "modified":"xxx"},
                        {"comment_id":"xxx", "user_id":"xxx", "name":"xxx","comment":"xxx", "created":"xxx", "modified":"xxx"}
                        ],
                        "attachments" : [ ]
                },
                {
                        "post_id" : "2",
                        "user_id" : "1",
                        "name" : "Test",
                        "slug" : "xxx",
                        "comment" : "xxx",
                        "created" : "xxx",
                        "modified" : "xxx",
                        "comments" : [ ],
                        "attachments" : [ ]
                }
        ],
        "profile_pic" : "adasdad",
        "settings" : [ ],
        "slug" : "arpeggio"
} 

I thoroughly already researched for hours already, and this is what I came up which is just a failure and does not work:

db.discussions.update(
                {_id:ObjectId("5188c93f0361ca6dc33e3a30"), posts:{post_id:1}}, 
                {$push:
                    {"posts:
                        {comments:
                            {"comment_id":"xxx", "user_id":"xxx", "name":"xxx","comment":"xxx", "created":"xxx", "modified":"xxx"}
                        }
                    } 
                }
                )

Please guys I'm desperately asking for help. I want to learn about this.

2
  • 1
    Change the query predicate to be "posts.post_id":1 and the $push part to push to "posts.$.comments":{new comment} This is using positional operator: docs.mongodb.org/manual/reference/operator/positional however, you really shouldn't use this schema - your document growth is unbounded and that's not good for performance. Commented May 7, 2013 at 23:20
  • i already had tried that, but I will retry again. About your opinion though I will pass it to my work mate. thank you Commented May 7, 2013 at 23:40

1 Answer 1

1

There are several things going on.

First you want to use the $ positional operator.

Second of all, you mention your collection name is Discussion but your update uses "discussions" - make sure you use the same name as the actual collection name.

Third, your post_id in the listed document is "1" but you are trying to match 1. String "1" will not be equal to number 1. Be careful of your types.

This syntax (note I also corrected for unbalanced quotes) should do it:

db.discussion.update(
             {_id:ObjectId("5188c93f0361ca6dc33e3a30"), "posts.post_id":"1"},
             {$push:
                  {"posts.$.comments":
                         {"comment_id":"xxx", "user_id":"xxx",
                          "name":"xxx","comment":"xxx", "created":"xxx",
                           "modified":"xxx"}
                     }
                 }
              )

I'm going to reiterate my dislike of this schema - you are not bounding your document growth and that will cause performance problems, not to mention making it more complex than you need (and larger too).

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

1 Comment

on my node.js I declared it as Discussion, I am aware of that but thanks for the tip. Anyway before I posted my problem here on stackoverflow I already tried that, just didn't used it properly. My mistake was not putting "" on the 1. Which is a new lesson to me. Thanks!

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.