node.js - 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 个集合:
1 用于我在旅行主页列表中需要的一些简短信息 - 例如名称、价格、评级、评论数等。
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});
而当我们在首页获取文章列表时,所有数据都已发送回用户——甚至是隐藏的文章的长篇正文!
那么实际项目中的解决方案是什么?
谢谢
解决方案
我不会添加两个单独的集合,我只会使用一个并通过 API 仅发送回所需的数据。如果您想对发回的数据进行超级细分,您可以使用 GraphQL。
但就 Mongo 架构而言,我从未听说过分离集合。
推荐阅读
- php - .htaccess 不工作并给我一个“500 内部服务器错误”警告
- java - 删除字符并将其添加到字符串递归
- math - 在 Idris 中,为什么接口参数必须是类型或数据构造函数?
- javascript - React Router 4——监听路由动作并以编程方式继续/停止路由
- android - 检查路线是否包含特定坐标
- css - CSS 过渡不适用于高度:0(但适用于其他高度)
- python - 将字典转换为 Json 并附加到文件
- timer - VHDL 中的 BCD 定时器
- node.js - 如何对函数使用 Node.js 承诺(执行查询时的 CassandraDB 驱动程序)
- javascript - 用阵列检测运动范围