Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

node.js - Returning inner array elements from multiple document in sorted form

hyy there, my collection goes like this and I am trying to get all comments list of particular blog_id in sorted order of date.

    [
{
    "_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
}
]

I want to list all comments of particular blog_id in descending order of date like below


    

[

        {
            "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")
        }
                {
            "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")
        }
]

    

how Can I do this in mongooes or mongodb

I had tried this

BlogComments.aggregate([

            {$match:{blog_id:blogId}},
            {
                $group:{
                    _id:statusId,comment_count:{$sum:"$record_count"},comments:{$push:"$comments"}
                 }
            },
            { "$unwind": "$comments" },
            { "$unwind": "$comments" },


            { "$group": {
                "_id": statusId,
                "comment_count": { "$first": "$comment_count" },
                "comments": { "$addToSet": "$comments" }
            }},
            { $sort: {'comments._id': 1}}


        ]);

This query gives comment and all associated comments from the multiple document but comments are in random order. $sort is not properly working for me

Thank you in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Jorin,

Try this:

db.comments.aggregate(
[
  {$match: {blog_id: ObjectId("56587befdb7224110f007233")}}, //match blogid
  {"$unwind": "$comments"}, //unwind the comments array
  {$sort: {"comments.dt": 1}}, //sort the comment documents by date

  {
    "$group": {
        "_id": '$blog_id', //group comment documents by blog_id
        "comments": {
            "$push": "$comments" //push comments of same blog_id into an array
        }
    }
}])

Based upon your sample data the output will be:

{
    "_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")
        },
        {
            "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")
        }
    ]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...