首页 > 解决方案 > Mongoose如何按嵌入式数组的总和排序

问题描述

我有一个课程集合,其中的一个数组存储课程等级 1-5 的计数。如 [0, 3, 0, 0, 1] 表示 3 人投 2 星,1 人投 5 星。如何使用猫鼬按最终分数排序?我的意思是先计算 (a + 2b + 3c + 4d + 5e) / (a + b + c + d + e) 作为分数,然后对集合进行排序?</p>

const CourseSchema = new mongoose.Schema({
  courseName: {
    type: String,
    required: true
  },
  rateCount: {
    type: [Number],
    required: true,
    default: [0, 0, 0, 0, 0]
  }
}

标签: mongodbmongoose

解决方案


推荐阅读