2
votes

I have used MongoDB aggregation and when I try to use sort and limit I get duplicate records.

As you can see below, I have 3 documents and from that document1 and document2 have the same title and document3 has different video title.

I want to group documents by title and also sort the grouped documents by serial episode in desc order.

document1 and document2 will be in the same group and will get unique document2 and then now I want to sort this document2 and document3 with 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 Output:

I want a result with document3 and then document2 because docuemnt1-2 are group together to produce a uniqe document2 and docuemnt2 has the latest episode. I need to compare document2 and document3 for video view count:

/* 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"
        }
    ]
}

Current Aggregate Operation:

// 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){});
1
What are your sample documents and what's your expected output from the sample? Can you update your question with those two things?chridam
Yes,sure, i have updated my question ,Please check nowUjjaval
What's your expected output from aggregating those sample docs?chridam
i have mentioned in my question please check there you will understand.Ujjaval
I Have updated question with expected json output.Ujjaval

1 Answers

0
votes

Why are you unwinding it..? Remove unwind,it will multiply the document with the array length..