以排序形式从多个文档返回内部数组元素

时间:2015-12-02 16:37:58

标签: node.js mongodb mongoose

hyy那里,我的收藏是这样的,我试图以排序的日期顺序获取特定blog_id的所有评论列表。

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

我想按日期的降序列出特定blog_id的所有评论,如下所示


    

[

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

    

我如何在mongooes或mongodb

中执行此操作

我试过这个

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}}


        ]);

此查询提供多个文档中的注释和所有相关注释,但注释是随机排列的。 $ sort不适合我

提前谢谢。

2 个答案:

答案 0 :(得分:2)

Jorin,

试试这个:

defer = "defer"

根据您的样本数据,输出将为:

char** fileName;
int Number_Of_Strings, String_Length, i;
fileName = malloc(Number_Of_Strings * sizeof(char*));

for ( i = 0; i < Number_Of_String; i++)   // For each string
    fileName = malloc(String_Length * sizeof(char));

答案 1 :(得分:0)

要执行此操作,您可以使用方法findById,然后使用mongoose方法sort对注释进行排序。

举个例子:

Blog.find({blog_id : YOUR_BLOG_ID}, 'comments')
.sort('dt')
.exec(function(err, docs){
 console.log(docs); // do whatever you want with your docs
});

这样的事情应该可以胜任。