node.js - 如何为 Express/Mongoose 规范化多对多关系创建发布请求
问题描述
我正在尝试学习如何在 express/router 和 Mongoose 中创建规范化的多对多 Post 请求。
我有三个集合:用户、建筑物和房间,这也是父文档到子文档的顺序。
我的建筑文档架构包括用户和房间 ID,如下所示:
const mongoose = require('mongoose');
const BuildingSchema = new mongoose.Schema({
user: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "user"
}
],
room: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "room"
}
],
My Room 文档架构包括 Building ID 和另一个子文档,如下所示:
const mongoose = require('mongoose');
const RoomSchema = new mongoose.Schema({
building: {
type: mongoose.Schema.Types.ObjectId,
ref: "building"
},
furniture: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "furniture"
}
],
我无法理解如何创建一个发布请求,该请求允许用户创建一个 Building 实例和多个与之关联的 Room 实例......
到目前为止,我的快速代码可以创建一个新的 Building 实例,但我不确定如何处理包括 Room id 的:
router.post('/createbuilding', [auth, [
check('name', 'Building Name is required').not().isEmpty(),
]
], async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const {
name,
type,
website,
location,
bio,
} = req.body;
const buildingFields = {
user: req.user.id,
//room: req.room.id,
name,
type,
website: website && website !== '' ? normalize(website, { forceHttps: true }) : '',
location,
bio
};
try {
let building = await Building.findOneAndUpdate(
{ user: req.user.id },
//{ room: req.room.id },
{ $set: buildingFields },
{ new: true, upsert: true }
);
res.json(building);
} catch (err) {
console.error(err.message);
res.status(500).send('Server Error');
}
}
);
注意:对房间 ID 的引用是故意注释掉的,因为我不确定如何将其包含在 Post 请求中。
解决方案
使用 Mongoose 的.findOneAndUpdate()
第一个参数是查询文档,类似于.findOne()
. 您的第二个参数是要更新的字段。$set
是多余的,因为猫鼬会为你做这件事。
如果您想在建筑物中添加一个房间而不是替换所有关联,您需要添加带有$push
.
const buildingFields = {
$push: {
room: {
req.room.id
}
},
name,
type,
...
}
我还假设您希望 BuildingSchema 中的用户字段是单个关联而不是多个关联。如果没有,您需要使用 a$elemMatch
来查询该文档:
推荐阅读
- javascript - 需要点击两次才能从浏览器下载图片
- java - 在 Stream Start 之前访问 Flink Classloader
- spring - 使用 RestTemplate 发送二进制文件,cURL 的 --data-binary 方式
- android - 如何在 Android 应用程序中设置测试标志以区分在测试和生产环境中运行应用程序?
- java - 需要了解语法类Builder
> - openedge - 如何在 Progress 4gl 中创建带有滚动条的多行字符串?
- java - 从Java中的不同类引用对象时出错
- python - SciPy 广义特征值:eig 和 eigh 产生不同的结果
- python-3.x - 仅使用 Python 解决重复任务
- ios - AMP HTML 动画在 iPad 上的行为不正确