| MongoDB的|如何排序和限制嵌入式阵列?嵌入式阵列分页

时间:2013-10-30 19:31:05

标签: arrays mongodb sorting

我正在努力排序并限制mongodb中嵌入式数组的结果。这是场景:

我有一个评论后的结构,其中post包含一系列评论。我想获得一个注释列表,按createdAt排序并执行限制/偏移...有点像,给出一个帖子ID分页注释并将它们返回给我。 =]

......下面是一个结构样本:

{    "_id" : ObjectId("52707a234f2044b7f2d22083"),
     "comments" : [{
            "_id" : ObjectId("5270986b4f204f5dd51ada8a"),
            "comment" : "asdfasdf asdfasdfa ",
            "userid" : NumberLong(1),
            "likes" : [ ],
            "createdAt" : ISODate("2013-10-30T05:26:03.858Z")},
           {
            "_id" : ObjectId("527098714f204f5dd51ada8b"),
            "comment" : "asdfasdf asdfasdfa ",
            "userid" : NumberLong(1),
            "likes" : [ ],
            "createdAt" : ISODate("2013-10-30T05:26:09.425Z")
           }
    ],
    "createdAt" : ISODate("2013-10-30T03:16:51.745Z"),
    "likes" : [ ],
    "status" : "simbora!!",
    "userid" : NumberLong(1)
}

所以...我可以尝试这个查询:

db.post.aggregate([ 
{$match: {_id: new ObjectId("52707a234f2044b7f2d22083")}}, 
{$unwind: "$comments"}, 
{$sort: {"comments.createdAt": -1}},
{$limit: 2}
]);

它给了我这个:

"result" : [
        {
                "_class" : "models.documents.Post",
                "_id" : ObjectId("52707a234f2044b7f2d22083"),
                "comments" : {
                        "_id" : ObjectId("527098714f204f5dd51ada8b"),
                        "comment" : "asdfasdf asdfasdfa ",
                        "userid" : NumberLong(1),
                        "likes" : [ ],
                        "createdAt" : ISODate("2013-10-30T05:26:09.425Z")
                },
                "createdAt" : ISODate("2013-10-30T03:16:51.745Z"),
                "likes" : [ ],
                "status" : "simbora!!",
                "userid" : NumberLong(1)
        },
        {
                "_class" : "models.documents.Post",
                "_id" : ObjectId("52707a234f2044b7f2d22083"),
                "comments" : {
                        "_id" : ObjectId("5270986b4f204f5dd51ada8a"),
                        "comment" : "asdfasdf asdfasdfa ",
                        "userid" : NumberLong(1),
                        "likes" : [ ],
                        "createdAt" : ISODate("2013-10-30T05:26:03.858Z")
                },
                "createdAt" : ISODate("2013-10-30T03:16:51.745Z"),
                "likes" : [ ],
                "status" : "simbora!!",
                "userid" : NumberLong(1)
        }
],
"ok" : 1

请注意,注释不再是一个数组,而是一个对象...虽然它给了我有序和有限的集合,但它也为我提供了每个注释的父对象......这不太好。所以我尝试了这个:

db.post.aggregate([
{$match: {_id: new ObjectId("52707a234f2044b7f2d22083")}},
{$unwind: "$comments"},
{$sort: {"comments.createdAt": -1}},
{"$project": {"_id": 0, "comments": "$comments"}},
{"$group": {"_id": "$_id", "comments": {"$push": "$comments"}}},
]);

它给了我这个:

{
        "result" : [
                {
                        "_id" : null,
                        "comments" : [
                                {
                                        "_id" : ObjectId("527098714f204f5dd51ada8b"),
                                        "comment" : "asdfasdf asdfasdfa ",
                                        "userid" : NumberLong(1),
                                        "likes" : [ ],
                                        "createdAt" : ISODate("2013-10-30T05:26:09.425Z")
                                },
                                {
                                        "_id" : ObjectId("5270986b4f204f5dd51ada8a"),
                                        "comment" : "asdfasdf asdfasdfa ",
                                        "userid" : NumberLong(1),
                                        "likes" : [ ],
                                        "createdAt" : ISODate("2013-10-30T05:26:03.858Z")
                                },
                                {
                                        "_id" : ObjectId("527098694f204f5dd51ada89"),
                                        "comment" : "asdfasdf asdfasdfa ",
                                        "userid" : NumberLong(1),
                                        "likes" : [ ],
                                        "createdAt" : ISODate("2013-10-30T05:26:01.174Z")
                                },
                                {
                                        "_id" : ObjectId("527098674f204f5dd51ada88"),
                                        "comment" : "asdfasdf asdfasdfa ",
                                        "userid" : NumberLong(1),
                                        "likes" : [ ],
                                        "createdAt" : ISODate("2013-10-30T05:25:59.795Z")
                                },
                                {
                                        "_id" : ObjectId("527098644f204f5dd51ada87"),
                                        "comment" : "asdfasdf asdfasdfa ",
                                        "userid" : NumberLong(1),
                                        "likes" : [ ],
                                        "createdAt" : ISODate("2013-10-30T05:25:56.936Z")
                                },
                                {
                                        "_id" : ObjectId("527098604f204f5dd51ada86"),
                                        "comment" : "asdfasdf asdfasdfa ",
                                        "userid" : NumberLong(1),
                                        "likes" : [ ],
                                        "createdAt" : ISODate("2013-10-30T05:25:52.379Z")
                                },
                                {
                                        "_id" : ObjectId("52707a234f2044b7f2d22083"),
                                        "comment" : "asdfasdf asdfasdfa ",
                                        "userid" : NumberLong(1),
                                        "likes" : [ ],
                                        "createdAt" : ISODate("2013-10-30T05:24:36.539Z")
                                },
                                {
                                        "_id" : null,
                                        "comment" : "bla bla",
                                        "userid" : NumberLong(1),
                                        "likes" : [ ],
                                        "createdAt" : ISODate("2013-10-30T05:23:24.037Z")
                                }
                        ]
                }
        ],
        "ok" : 1
}

这也不太好,因为如果我应用限制并跳过运算符,它将通过发布限制,而不是评论......

任何人都可以帮帮我吗?

1 个答案:

答案 0 :(得分:5)

如果我理解,你想找到一个id的帖子,只返回最后两条评论。

你不是那么远,解决方案是你的两个试验的组合:

db.posts.aggregate( 
  {$match: {_id: new ObjectId("52707a234f2044b7f2d22083")}}, 
  {$unwind: "$comments"}, 
  {$sort: {"comments.createdAt": -1}},
  {$limit: 2},
  {"$group": {"_id": "$_id", "comments": {"$push": "$comments"}}}
)