gpt4 book ai didi

mongodb - 使用 mongodb 聚合进行数据复制

转载 作者:可可西里 更新时间:2023-11-01 10:50:53 24 4
gpt4 key购买 nike

我使用了 MongoDB 聚合,当我尝试使用排序和限制时,我得到了重复的记录。

正如您在下面看到的,我有 3 个文档,其中 document1document2 具有相同的标题,而 document3 具有不同的视频标题.

我想按标题对文档进行分组,并按连续剧集按降序对分组的文档进行排序。

document1document2 将在同一组中,并将获得唯一的 document2 然后现在我想对这个 document2document3video_view_count

/* document1 */
{
"_id" : ObjectId("580afd565706467c1bbabd70"),
"serial_episode" : "5",
"video_view_count" : 50.0,
"video_data" : [
{
"video_categories" : [
"Sport"
],
"video_title" : "Zwyci??zca",
"language_id" : "578f1ec6e494f9400b21fec4"
},
{
"video_featured_text" : "",
"video_categories" : [
"Sport"
],
"video_title" : "Zwyci??zca",
"language_id" : "578f1ec6e494f9400b21fec3"
}
]
}

/* document2 */
{
"_id" : ObjectId("580afd565706467c1bbabd71"),
"serial_episode" : "6",
"video_view_count" : 10.0,
"video_data" : [
{
"video_categories" : [
"Sport"
],
"video_title" : "Zwyci??zca",
"language_id" : "578f1ec6e494f9400b21fec4"
},
{
"video_featured_text" : "",
"video_categories" : [
"Sport"
],
"video_title" : "Zwyci??zca",
"language_id" : "578f1ec6e494f9400b21fec3"
}
]
}

/* document3 */
{
"_id" : ObjectId("580afd565706467c1bbabd72"),
"serial_episode" : "",
"video_view_count" : 11.0,
"video_data" : [
{
"video_categories" : [
"Sport"
],
"video_title" : "Zwyci??zca123",
"language_id" : "578f1ec6e494f9400b21fec4"
},
{
"video_featured_text" : "",
"video_categories" : [
"Sport"
],
"video_title" : "Zwyci??zca123",
"language_id" : "578f1ec6e494f9400b21fec3"
}
]
}

预期输出:

我想要一个 document3 然后是 document2 的结果,因为 docuemnt1-2 组合在一起产生一个唯一的 document2docuemnt2 有最新一集。我需要比较 document2document3 的视频观看次数:

/* document3 */
{
"_id": ObjectId("580afd565706467c1bbabd72"),
"serial_episode": "",
"video_view_count": 11,
"video_data": [
{
"video_categories": [
"Sport"
],
"video_title": "Zwyci??zca123",
"language_id": "578f1ec6e494f9400b21fec4"
},
{
"video_featured_text": "",
"video_categories": [
"Sport"
],
"video_title": "Zwyci??zca123",
"language_id": "578f1ec6e494f9400b21fec3"
}
]
},
/* document3 */
{
"_id": ObjectId("580afd565706467c1bbabd71"),
"serial_episode": "6",
"video_view_count": 10,
"video_data": [
{
"video_categories": [
"Sport"
],
"video_title": "Zwyci??zca",
"language_id": "578f1ec6e494f9400b21fec4"
},
{
"video_featured_text": "",
"video_categories": [
"Sport"
],
"video_title": "Zwyci??zca",
"language_id": "578f1ec6e494f9400b21fec3"
}
]
}

当前聚合操作:

// Grouping that I have implemented 
var group = {
"$group": {
"_id":" $video_data.video_title",
"video_rating" : { $first:" $video_rating" },
"serial_episode" : { $first: "$serial_episode" },
"video_view_count": { $first: "$video_view_count" },
}
};

// Aggregate function to get that videos
videos.aggregate([
{ $match: { "video_data.video_categories": query.category_name } },
{ $unwind: "$video_data" },
{ $sort: { video_view_count: -1 } },
{ $sort:{ serial_episode: -1 } },
group,
{ $sort:{ video_view_count: -1 } },
{ $skip: skipData },
{ $limit: 10 }
], function(error, output){});

最佳答案

你为什么要解除它..?删除 unwind,它将文档与数组长度相乘..

关于mongodb - 使用 mongodb 聚合进行数据复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40191030/

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