2
votes

Hyy , I have a collection where comments related to the blog are stored in multiple document as shown below.

 [
{
    "_id" : ObjectId("565f0f5d77f0c7bd11bbadd8"),
    "blog_id" : ObjectId("56587befdb7224110f007233"),
    "comments" : [
        {
            "user_id" : ObjectId("562fa014888806820e21e0df"),
            "user_full_name" : "Niroj Paudel",
            "comment" : "pradip is bhole baba",
            "_id" : ObjectId("565f0f5d77f0c7bd11bbadd9"),
            "dt" : ISODate("2015-12-02T15:33:49.578Z")
        },
        {
            "user_id" : ObjectId("562fa014888806820e21e0df"),
            "user_full_name" : "Niroj Paudel",
            "comment" : "honkog pokhara... he he ha ha",
            "_id" : ObjectId("565f1034fd07cbfc1129db0b"),
            "dt" : ISODate("2015-12-02T15:37:24.581Z")
        }
    ],
    "record_count" : 2,
    "__v" : 0
}


{
    "_id" : ObjectId("565efa37635f09900d21a339"),
    "blog_id" : ObjectId("56587befdb7224110f007233"),
    "comments" : [
        {
            "user_id" : ObjectId("562fa014888806820e21e0df"),
            "user_full_name" : "Niroj Paudel",
            "comment" : "wat a nice car wow",
            "_id" : ObjectId("565efa37635f09900d21a33a"),
            "dt" : ISODate("2015-12-02T14:03:35.289Z")
        },
        {
            "user_id" : ObjectId("562fa014888806820e21e0df"),
            "user_full_name" : "Niroj Paudel",
            "comment" : "love is life budikhola ma dives",
            "_id" : ObjectId("565efa76635f09900d21a33b"),
            "dt" : ISODate("2015-12-02T14:04:38.661Z")
        },
        {
            "user_id" : ObjectId("562fa014888806820e21e0df"),
            "user_full_name" : "Niroj Paudel",
            "comment" : "bholi ajaya ko bihe",
            "_id" : ObjectId("565efaa0635f09900d21a33c"),
            "dt" : ISODate("2015-12-02T14:05:20.847Z")
        },
        {
            "user_id" : ObjectId("562fa014888806820e21e0df"),
            "user_full_name" : "Niroj Paudel",
            "comment" : "manish is nice",
            "_id" : ObjectId("565efb17635f09900d21a33d"),
            "dt" : ISODate("2015-12-02T14:07:19.704Z")
        },
        {
            "user_id" : ObjectId("562fa014888806820e21e0df"),
            "user_full_name" : "Niroj Paudel",
            "comment" : "niroj is cool",
            "_id" : ObjectId("565efd53c22dddc80e8f461c"),
            "dt" : ISODate("2015-12-02T14:16:51.730Z")
        },
        {
            "user_id" : ObjectId("562fa014888806820e21e0df"),
            "user_full_name" : "Niroj Paudel",
            "comment" : "ramesh is cool",
            "_id" : ObjectId("565f0d376d82e24c11f6c0d1"),
            "dt" : ISODate("2015-12-02T15:24:39.010Z")
        }
    ],
    "record_count" : 6,
    "__v" : 0
}
]

Suppose user wants to update his comment; for this I would have the comments _id now my problem is that how can I detect in which document the comment exist based of the comment _id value of comments array field.

suppose I have a comment "_id" : "565f0f5d77f0c7bd11bbadd9" (first element of first document) then the result should give the parent document _id:"565f0f5d77f0c7bd11bbadd8" (first document id)

How can I do this..

Thank you in advance

2

2 Answers

0
votes

if possible, then please update your schema. Suppose you have collection of Blog. Then your code will be :

Blog.find({'comments._id' : '565f0f5d77f0c7bd11bbadd9'}).exec(function(err,blog){
    if(!err && blog){
        console.log("Blog id :"+blog._id);
    }
    else{
        console.log("Dont get your blog");
    }
});
0
votes

if your comments are directly stored on the blog document you could go with an aggregate method to collect the comment across all documents. note that this seems to work when you don't have any object references to be populated.

asuming all documents are based on a blog Schema and that you wish to recieve the id of the comment:

Blog.aggregate(
[{'$match':{'comments':{'$elemMatch:{'_id':'565f0f5d77f0c7bd11bbadd9'}}}},

{'$project':{comments:'$_id', user_id:1}},
{'$unwind':'$comments'}
{'$group':{_id:'$comments'}}
]).exec(function(err, results){
    if(results){
       console.log(results);
    }
});

I hope this is somewhat what you're looking for, you might find the following read usefull elementMatch and aggregation from the mongoDB docs.

cheers!