MongoDB体系结构:如何以可扩展的方式存储大量数组或子文档 [英] MongoDB architecture: how to store a large amount of arrays or sub documents in a scalable way

查看:51
本文介绍了MongoDB体系结构:如何以可扩展的方式存储大量数组或子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发博客应用程序,用户可以在其中创建自己的博客,并且每个博客中都有博客文章.我的想法是设计一个在每个博客都有很多博客文章时可扩展的数据库.

I am currently working on a blogging app, in which users can create their own blogs and each blog has blogposts within that. I'm ideating about architecting a database that is scalable when each blog has a lot of blogposts.

这样构造数据库更好吗:

So is it better to structure my database as this:

blog1 : {
 blogname : 'blog1',
 blogposts: [array of blogposts] 
},

blog2 : {
 blogname : 'blog2',
 blogposts: [array of blogposts] 
}

或者我应该使用所有博客文章创建一个单独的集合,诸如此类:

Or should I create a separate collection with all the blogposts, something like this:

blogpost1: {
 id: 'blogpost1',
 content: {blogpost content in json format}
},
blogpost2: {
 id: 'blogpost2',
 content: {blogpost content in json format}
}

并在博客集中引用它们.

and reference them in the blog collection.

我想知道在博客文章很多的情况下哪种选择更好.因为我记得在MongoDB文档中读过某个地方,不建议在文档中放置可能超出范围的数组,所以方法1并不理想,对吧?

I want to know which choice would be superior when there are a lot of blogposts. Because I remember reading somewhere in MongoDB docs that it's not recommended to have arrays within document that can grow beyond bounds, so approach #1 is not ideal, right?

推荐答案

在创建数据库时,考虑到我将要提出的请求非常有用.

When creating databases, I find it useful to think about the requests I would be making.

博客应用程序用户希望根据某些条件搜索所有博客或找到博客.

A blogging app user would want to search all blogs or find a blogger by some criteria.

在这种情况下,针对博客作者和博客的单独收藏集效果最好.然后构造您的文档,以便博客可以链接到他们的博客,反之亦然.

In this case separate collections for bloggers and blogs would work best. Then structure your documents so that the bloggers link to their blogs and vice versa.

这可以通过Mongoose架构( https://mongoosejs.com/docs/index.html).

This can be done with Mongoose Schemas (https://mongoosejs.com/docs/index.html).

// models/blogger.js
const mongoose = require('mongoose')

const bloggerSchema = mongoose.Schema({
  blogs: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Blog'
    }
  ],
  name: String
})

bloggerSchema.set('toJSON', {
  transform: (document, returnedObject) => {
    const blogger = returnedObject

    blogger.id = blogger._id.toString()
    delete blogger._id
    delete blogger.__v
  }
})

module.exports = mongoose.model('Blogger', bloggerSchema)

然后使用您的请求填充:

Then use populate with your request:

// controllers/bloggers.js
const bloggersRouter = require('express').Router()
const Blogger = require('../models/blogger')

bloggersRouter.get('/', async (request, response) => {
  const bloggers = await Blogger.find({}).populate(
    'blogs', {
      title: 1
    }
  )
  response.json(bloggers.map(blogger => blogger.toJSON()))
})

module.exports = bloggersRouter

通过这种方式,您不必将整个博客都添加到博客文档中,只需在博客的初始视图中包括标题或您需要的其他任何内容即可.

This way you don't have to add the blogs in their entirety to the blogger document, you can just include the title or anything else that you need on the bloggers initial view.

您还可以考虑限制博客的长度,以便可以更好地控制数据,然后考虑乔建议的选项.

You could also think about limiting the length of a blog, so you can have more control over the data and then think about the options Joe suggested.

这篇关于MongoDB体系结构:如何以可扩展的方式存储大量数组或子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆