Reputation: 179
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.
Upvotes: 0
Views: 177
Reputation: 42352
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).
Upvotes: 1