Answers for "how to sort object array in mongodb"

0

how to sort object array in mongodb

db.students.aggregate(
    // Initial document match (uses index, if a suitable one is available)
    { $match: {
        _id : 1
    }},

    // Expand the scores array into a stream of documents
    { $unwind: '$scores' },

    // Filter to 'homework' scores 
    { $match: {
        'scores.type': 'homework'
    }},

    // Sort in descending order
    { $sort: {
        'scores.score': -1
    }}
)
Posted by: Guest on June-10-2021

Code answers related to "how to sort object array in mongodb"

Browse Popular Code Answers by Language