我已经使用了MongoDB聚合,当我尝试使用排序和限制时,我得到重复的记录。用mongodb汇总数据重复
正如你可以看到下面,我有3个文件,并从该document1
和document2
具有相同的标题和document3
有不同的视频节目。
我想按标题对文档进行分组,并按desc顺序对序列剧集中的分组文档进行排序。
document1
和document2
将在同一组,将获得独特的书2,然后现在我想这个document2
和document3
与video_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"
}
]
}
Expexcted输出:
我想要的结果与document3
然后document2
因为docuemnt1-2是组合在一起,产生一个潮头document2
和docuemnt2
有最新的插曲。 我需要比较document2
和document3
视频查看次数:
/* 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){});
什么是您的示例文档,样本中您的预期输出是什么?你能用这两件事来更新你的问题吗? – chridam
是的,当然,我已更新我的问题,请现在检查 –
汇总这些样本文档的预期输出是什么? – chridam