小编UCA*_*dio的帖子

MongoDb 中的存储桶模式是处理大型无界数组的最佳方式吗?

我正在为 MERN 堆栈应用程序实现社交功能(关注/取消关注用户),并尝试提出一个良好的 MongoDB 解决方案,以避免潜在的大量无限关注者出现问题。具体来说,我希望避免:

  • MongoDB 必须在磁盘上移动大型追随者数组并随着其变大而重建索引
  • 如果用户拥有大量关注者(> 100 万),则达到 16mb bson 限制
  • 查询/返回关注者以通过分页显示或计算/显示关注者计数时性能缓慢

从我研究的所有内容来看,似乎使用存储桶模式方法是最好的解决方案......我在这方面找到了两篇好文章: https ://www.mongodb.com/blog/post/paging-with-the-桶模式--part-1 https://www.mongodb.com/blog/post/paging-with-the-bucket-pattern--part-2

我开始像这样处理它......追随者模型:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const FollowerSchema = new Schema({
  user: {
    type: Schema.Types.ObjectId,
    ref: 'user',
  },
  // creating an array of followers
  followers: [
    {
      user: {
        type: Schema.Types.ObjectId,
        ref: 'user',
      },
      datefol: {
        type: Date,
        default: Date.now,
      },
    },
  ],
  count: {
    type: Number,
  },
  createdate: {
    type: Date,
    default: Date.now,
    required: true,
  },
});

module.exports …
Run Code Online (Sandbox Code Playgroud)

arrays pagination mongoose mongodb node.js

5
推荐指数
1
解决办法
5084
查看次数

标签 统计

arrays ×1

mongodb ×1

mongoose ×1

node.js ×1

pagination ×1