我一直在为将在不同页面上显示的文本构建 Mongoose 模式,并且它具有用于更新文本的 POST 数据的端点。
例如,我想存储将在“关于”页面和“联系”页面中显示/更新的短信
设计文本模型的首选方式是什么?
1) 将所有消息存储在一个数据对象中的模型
- 在前端,父组件使用 Texts.findOne() 获取所有文本消息,并向下渗透到需要它的页面
const textsSchema = new Schema(
{
aboutMessage1: {
type: String,
required: true
},
aboutMessage2: {
type: String,
required: true
},
contactMessage1: {
type: String
},
contactMessage2: {
type: String
}
},
{ timestamps: true }
);
2) 包含每条消息的模型——因此它将有多个对象
- 在前端,每个页面都使用 Text.findById(textId) 来检索每条消息
const textSchema = new Schema(
{
// Example: name = contactMessage
name: {
type: String
},
message: {
type: String
}
},
{ timestamps: true }
);
3) 包含每个页面文本的多个模型
- 与 1) 方法类似,通过 Texts.findOne() 获取文本,但在每个页面中执行
const aboutTextsSchema = new Schema(
{
message1: {
type: String,
required: true
},
message2: {
type: String,
required: true
},
},
{ timestamps: true }
);
const contactTextsSchema = new Schema(
{
message1: {
type: String,
},
message2: {
type: String,
},
},
{ timestamps: true }
);
最佳答案
最有希望的选择是第二个。因为第一个和第三个选项是静态的,如果将来您需要向现有页面添加新页面或新消息,则需要更改 mongoose 模型并部署 API。
但我认为,最好为您的场景创建页面架构,而不是创建文本架构。
这里我将消息嵌入到页面架构中。
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const pageSchema = new Schema(
{
page: {
type: String
},
messages: [
new Schema({
name: {
type: String
},
message: {
type: String
}
})
]
},
{ timestamps: true }
);
module.exports = mongoose.model("Page", pageSchema);
现在我们可以使用此帖子路由来创建页面:
router.post("/pages", async (req, res) => {
const result = await Text.create(req.body);
res.send(result);
});
我们可以使用之前的帖子路由创建一个页面及其消息。
请求正文:
{
"_id": "5e4937e9e2454a2c0c162890",
"page": "About",
"messages": [
{
"_id": "5e4937e9e2454a2c0c162892",
"name": "Abou1",
"message": "About1 message..."
},
{
"_id": "5e4937e9e2454a2c0c162891",
"name": "Abou2",
"message": "About2 message..."
}
],
"createdAt": "2020-02-16T12:39:05.154Z",
"updatedAt": "2020-02-16T12:39:05.154Z",
"__v": 0
}
回应:
{
"_id": "5e4937e9e2454a2c0c162890",
"page": "About",
"messages": [
{
"_id": "5e4937e9e2454a2c0c162892",
"name": "Abou1",
"message": "About1 message..."
},
{
"_id": "5e4937e9e2454a2c0c162891",
"name": "Abou2",
"message": "About2 message..."
}
],
"createdAt": "2020-02-16T12:39:05.154Z",
"updatedAt": "2020-02-16T12:39:05.154Z",
"__v": 0
}
如果稍后我们想向页面添加消息,我们可以使用以下 put 路由。
router.put("/pages/:id", async (req, res) => {
const result = await Page.findByIdAndUpdate(
req.params.id,
{
$push: { messages: req.body }
},
{ new: true }
);
res.send(result);
});
请求正文:
{
"name": "Abou3",
"message": "About3 message..."
}
回应:
{
"_id": "5e4937e9e2454a2c0c162890",
"page": "About",
"messages": [
{
"_id": "5e4937e9e2454a2c0c162892",
"name": "Abou1",
"message": "About1 message..."
},
{
"_id": "5e4937e9e2454a2c0c162891",
"name": "Abou2",
"message": "About2 message..."
},
{
"_id": "5e493926f905ab3300106f94",
"name": "Abou3",
"message": "About3 message..."
}
],
"createdAt": "2020-02-16T12:39:05.154Z",
"updatedAt": "2020-02-16T12:44:22.763Z",
"__v": 0
}
当客户端需要页面消息时,我们所需要做的就是通过页面 ID 或页面名称检索该页面:
router.get("/pages/id/:id", async (req, res) => {
const result = await Page.findById(req.params.id);
res.send(result);
});
//or
router.get("/pages/name/:name", async (req, res) => {
const result = await Page.findOne({ page: req.params.name });
res.send(result);
});
关于javascript - 使用 Node JS 在 mongoose 中存储多个文本的理想方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60245946/