4

我在尝试对 mongodb 中嵌入数组的结果进行排序和限制时遇到了最困难的情况。这是场景:

我有一个 post-comment 结构,其中 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

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

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
}

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

谁能帮我吗??

4

1 回答 1

8

如果我理解,您想通过 id 查找帖子并仅返回最后 2 条评论。

您还没有到目前为止,解决方案是您的 2 次试验的组合:

db.posts.aggregate( 
  {$match: {_id: new ObjectId("52707a234f2044b7f2d22083")}}, 
  {$unwind: "$comments"}, 
  {$sort: {"comments.createdAt": -1}},
  {$limit: 2},
  {"$group": {"_id": "$_id", "comments": {"$push": "$comments"}}}
)
于 2013-10-30T19:57:43.837 回答