首页 > 解决方案 > MongoDB,按经常更新的字段对大集合进行排序

问题描述

我有这个模式(使用 nodejs - mongoose):

const Post = new mongoose.Schema({
  title: {
    type: String,
    required: true,
    unique: true,
  },
  description: {
    type: String,
    required: true,
  },
  likes: {
    type: Number,
    required: true,
    default: 0,
    min: 0,
  }
}, {
  timestamps: true,
});

假设我的收藏有数百万个这样的文档,我想按“喜欢”排序。同时,“喜欢”是非常频繁更新的东西,所以我认为我不应该在上面使用排序索引。如果我使用排序和限制在分页中提供该内容,即使我不使用索引,这是否保证我在读取数据时具有良好的性能?(我知道mongo默认使用一些算法来创建内存桶对数据进行排序,当没有提供索引时)

标签: mongodbperformancemongoose

解决方案


推荐阅读