gpt4 book ai didi

arrays - |MongoDB|如何排序和限制嵌入式数组?嵌入式数组分页

转载 作者:可可西里 更新时间:2023-11-01 09:24:48 26 4
gpt4 key购买 nike

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

我有一个帖子评论结构,其中帖子包含一组评论。我想得到一个评论列表,按 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
}

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

谁能帮帮我??

最佳答案

据我了解,您想按 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"}}}
)

关于arrays - |MongoDB|如何排序和限制嵌入式数组?嵌入式数组分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19692212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com