首页 > 解决方案 > Mongoose 中的实用模式设计,用于主页中的文章/主题列表以及每个完整的详细文章/主题

问题描述

我是 Mongoose 的新手,并试图在上面创建我的第一个数据库,

这是我的问题

我为我的 Node.js API 项目的一个旅游站点创建了一个旅游模式。

前端的两个部分将从该模型中获取数据:

这是JSON文件

  {
    "name": "some name",
    "type": "Bestseller tour",
    "image-thumbnail": "https://cdn.vuetour.com/images/cards/docks.jpg",
    "rating": 4.3,
    "duration": 4,
    "transportation": "plane",
    "reviews-count": 25,
    "price": 6999,
    "reviews":[
         "Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. What else can I say? Quick, thorough, excellent.",
         "Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. What else can I say? Quick, thorough, excellent.",
         "Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. Excellent work. What else can I say? Quick, thorough, excellent."
    ],
    "timeline": [
      {
        "day": 1,
        "title": "Day 1: Arrive and meet the Group",
        "description": [
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        ]
      },
      {
        "day": 2,
        "title": "Day 2: Beaches, Temples & Sunsets",
        "description": [
           "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        ]
      },
      {
        "day": 3,
        "title": "Day 3: Learn to Surf",
        "description": [
          "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        ]
      },
      {
        "day": 4,
        "title": "Day 3: Learn to Surf",
        "description": [
          "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        ]
      },
      {
        "day": 5,
        "title": "Day 3: Learn to Surf",
        "description": ["Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        ]
      }
    ]
  },,
  },


我是否需要分成 2 个集合:

如果像这样划分,我必须为一个详细游览页面查询 DB 2 次 - 获取短数据和长数据,

或者我只是为上面的所有数据创建一个集合?

我在Realworld Node.js 项目中看到他们只创建了一个文章模型来存储所有数据,

var ArticleSchema = new mongoose.Schema({
  slug: {type: String, lowercase: true, unique: true},
  title: String,
  description: String,
  body: String, // long string data for the content of the blog
  favoritesCount: {type: Number, default: 0},
  comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],
  tagList: [{ type: String }],
  author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
}, {timestamps: true});

而当我们在首页获取文章列表时,所有数据都已发送回用户——甚至是隐藏的文章的长篇正文!

那么实际项目中的解决方案是什么?

谢谢

标签: node.jsmongodbmongoose

解决方案


我不会添加两个单独的集合,我只会使用一个并通过 API 仅发送回所需的数据。如果您想对发回的数据进行超级细分,您可以使用 GraphQL。

但就 Mongo 架构而言,我从未听说过分离集合。


推荐阅读