猫鼬中的实用模式设计,用于主页中的文章/主题列表以及每个完整的详细文章/主题



我是猫鼬的新手,并试图在其上创建我的第一个数据库,

这是我的问题

我已经为我的 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 个集合:

  • 1 用于我在主页旅游列表中需要的一些简短信息 - 例如名称、价格、评级、评论计数等。

  • 2 是每个特定详细信息浏览页面的长详细信息值 - 例如时间线、评论等。

如果像这样划分,我必须查询数据库 2 次才能获得一个详细信息浏览页面 - 获取短数据和长数据,

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

我在现实世界节点.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});

当我们在主页上获得文章列表时,所有数据都已发回给用户 - 甚至是隐藏的文章的长正文!

那么现实生活中的解决方案是什么?

谢谢

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

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

最新更新