Question

Hello I have a collection:

{
    "_id" : ObjectId("508d27069cc1ae293b36928d"),
    "title" : "This is the title",
    "body" : "This is the body text.",
    "created_date" : ISODate("2012-10-28T12:41:39.110Z"),
    "comments" : [
        {
            "subject" : "This is coment 1",
            "body" : "This is the body of comment 1.",
            "author_id" : ObjectId("508d345f9cc1ae293b369296"),
            "created_date" : ISODate("2012-10-28T13:34:23.929Z")
        },
        {
            "subject" : "This is coment 2",
            "body" : "This is the body of comment 2.",
            "author_id" : ObjectId("508d34739cc1ae293b369297"),
            "created_date" : ISODate("2012-10-28T13:34:43.192Z")
        },
        {
            "subject" : "This is coment 3",
            "body" : "This is the body of comment 3.",
            "author_id" : ObjectId("508d34839cc1ae293b369298"),
            "created_date" : ISODate("2012-10-28T13:34:59.336Z")
        }
    ]
}

So, on one page at dashboard I want to see all comments, how I can do that? How I can get all comments for each post in a single collection, how I can identify for each comment (for editing or removing)?

UPD1:

This is a doc from the posts collection. I want to get something like this:

[
    ...,
    {
         "_generated_id_for_identify": [What the data?],
         "subject" : "This is coment 1",
         "body" : "This is the body of comment 1.",
         "author_id" : ObjectId("508d345f9cc1ae293b369296"),
         "created_date" : ISODate("2012-10-28T13:34:23.929Z")
    },
    {
         "_generated_id_for_identify": [What the data?],
         "subject" : "This is coment 2",
         "body" : "This is the body of comment 2.",
         "author_id" : ObjectId("508d34739cc1ae293b369297"),
         "created_date" : ISODate("2012-10-28T13:34:43.192Z")
    },
    ...,
    {
         "_generated_id_for_identify": [What the data?],
         "subject" : "This is coment N",
         "body" : "This is the body of comment N.",
         "author_id" : ObjectId("508d34839cc1ae293b369298"),
         "created_date" : ISODate("2012-10-28T13:34:59.336Z")
    },
    ...
]
Was it helpful?

Solution

with aggregate in posts collection and matching by "_id" and then using unwind in comments and proyection you can obtain a new collection of comments in the results key.

db.posts.aggregate([
                   {$match:{_id:ObjectId("508d27069cc1ae293b36928d")}}, 
                   {$unwind:"$comments"}, 
                   {$project:{
                              "_id":{id:"$_id",dt:"$comments.created_date"},
                            subject:"$comments.subject",
                               body:"$comments.body",
                         "autor_id":"$comments.author_id",
                     "created_date":"$comments.created_date"}} ]).result

but with a simple query matching the posts._id you obtain the post.comments

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top