2016-10-22 91 views
2

我已经使用了MongoDB聚合,当我尝试使用排序和限制时,我得到重复的记录。用mongodb汇总数据重复

正如你可以看到下面,我有3个文件,并从该document1document2具有相同的标题和document3有不同的视频节目。

我想按标题对文档进行分组,并按desc顺序对序列剧集中的分组文档进行排序。

document1document2将在同一组,将获得独特的书2,然后现在我想这个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" 
     } 
    ] 
}   

Expexcted输出:

我想要的结果与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){}); 
+0

什么是您的示例文档,样本中您的预期输出是什么?你能用这两件事来更新你的问题吗? – chridam

+0

是的,当然,我已更新我的问题,请现在检查 –

+0

汇总这些样本文档的预期输出是什么? – chridam

回答

0

你为什么退绕..? 取消展开,它会将文档与阵列长度相乘。

+0

我有放松,因为我有对象数组中的数据。像 {_id:1234655, video_view_count:0 video_data:[ { VIDEO_TITLE: “你好” }, { VIDEO_TITLE: “HI” }] } –

+0

我认为这个问题是与您的数据。 。在文档1中没有“video_title”字段使用$ exists –

+1

你能告诉你从你的查询中得到什么样的信息 –