使用mongodb聚合实现数据复制
我使用了MongoDB聚合,当我尝试使用排序和限制时,会得到重复的记录 如下所示,我有3个文档,其中使用mongodb聚合实现数据复制,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我使用了MongoDB聚合,当我尝试使用排序和限制时,会得到重复的记录 如下所示,我有3个文档,其中document1和document2具有相同的标题,而document3具有不同的视频标题 我想按标题对文档进行分组,并按描述顺序按系列事件对分组的文档进行排序 document1和document2将在同一个组中,并将获得唯一的document2,然后现在我想用视频查看次数对document2和document3进行排序 /* document1 */ { "_id" : Object
document1
和document2
具有相同的标题,而document3
具有不同的视频标题
我想按标题对文档进行分组,并按描述顺序按系列事件对分组的文档进行排序
document1
和document2
将在同一个组中,并将获得唯一的document2,然后现在我想用视频查看次数对document2
和document3
进行排序
/* 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输出:
// 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){});
我想要一个带有document3
的结果,然后是document2
,因为docuemnt1-2被组合在一起生成一个uniqedocument2
,而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){});
你为什么要解开它。。?
删除展开,它会将文档与数组长度相乘。我已展开,因为我在对象数组中有数据。像{id:1234655,video\u view\u count:0 video\u data:[{video\u title:“Hello”},{video\u title:“HI”}我想问题在于你的数据。。在文档1中没有“视频标题”字段使用$EXIST您能告诉我您从查询中得到了什么吗请立即检查我的问题数据。忘记发布有问题的视频标题,对此很抱歉。由于我使用了分页限制,所以我得到了重复数据的输出。因此,如果我删除了限制,那么它将显示完美的数据,而没有重复。您的示例文档是什么?您希望从示例中获得什么样的输出?你能用这两件事更新你的问题吗?是的,当然,我已经更新了我的问题,现在请检查聚合这些样本文档的预期输出是什么?我在问题中提到了,请检查一下,你会理解的。我用预期的json输出更新了问题。