📌  相关文章
📜  如何在 mongodb 中对对象数组进行排序 - 任何代码示例

📅  最后修改于: 2022-03-11 14:59:46.595000             🧑  作者: Mango

代码示例1
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
    }}
)