'Object transformation using mongoose in nodejs

Actually I'm new in nodejs. Currently I'm trying to solve a easy problem in nodejs but I'm stuck on it. Please help me to figure out this, I have this object,

[
    {
        "_id": "6241545e1ef44f0794bc2829",
        "liveId": "adsjbpofdsbupfadsubarr",
        "courses": [
            {
                "status": "In Progress",
                "language": "pt",
                "_id": "6241545e1ef44f0794bc282b",
                "coursename": "Thinking Critically ML"
            },
            {
                "status": "In Progress",
                "language": "pt",
                "_id": "6241545e1ef44f0794bc282b",
                "coursename": "Thinking Critically ML"
            }
        ]
    },
    {
        "_id": "6241545e1ef44f0794bc2829",
        "liveId": "adsjbpofdsbupfadsubarr",
        "courses": [
            {
                "status": "In Progress",
                "language": "pt",
                "_id": "6241545e1ef44f0794bc282b",
                "coursename": "Thinking Critically ML"
            },
            {
                "status": "In Progress",
                "language": "pt",
                "_id": "6241545e1ef44f0794bc282b",
                "coursename": "Thinking Critically ML"
            }
        ]
    }
]

I just want to transform this objects to this,

[
    {
        "status": "In Progress",
        "language": "pt",
        "_id": "6241545e1ef44f0794bc282b",
        "coursename": "Thinking Critically ML",
        "liveId": "adsjbpofdsbupfadsubarr",
    },
    {
        "status": "In Progress",
        "language": "pt",
        "_id": "6241545e1ef44f0794bc282b",
        "coursename": "Thinking Critically ML",
        "liveId": "adsjbpofdsbupfadsubarr",
    }
]

Here is my schema,

const courseSchema = new mongoose.Schema({
    coursename: String,
    
    status: {
        type: String,
        enum: ['In Progress', 'Live'
        ],
        default: 'In Progress'
    },
    
    language: {
        type: String,
        enum: ['en', 'es', 'pt', 'ja'
        ],
        default: 'en',
    },  
})


const DemoCourse= new mongoose.Schema({
    liveId: String,
    courses: {
        type: [courseSchema
        ]
    }
})

I did this,

DemoCourse.aggregate([
    {
        "$match": {
            "courses.isDeleted": false
        }
    },
    {
        "$unwind": "$courses"
    },
    {
        "$group": {
            "_id": {
                "course_id": "$courses._id",
                "status": "$courses.status",
                "coursename": "$courses.coursename",
                "language": "$courses.language",
            },
        }
    },
    {
        "$group": {
            "_id": null,
            "courses": {
                "$push": {
                    "_id": "$_id.course_id",
                    "coursename": "$_id.coursename",
                    "language": "$_id.language",
                    "status": "$_id.status",
                }
            }
        }
    },
    {
        "$project": {
            "courses": 1
        }
    }
])

and get this output,

{
    "_id": null,
    "courses": [
        {
            "_id": "62416ed100e46a1f48473488",
            "status": "In Progress",
            "courseName": "Thinking Critically ML - Lastly",
            "language": "pt",
        },
        {
            "_id": "62416ed100e46a1f48473488",
            "status": "In Progress",
            "courseName": "Thinking Critically ML - Lastly",
            "language": "en",
        },

    ]
}

Just not able to bind liveId property . How can I do that or am I missing a silly point ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source