首页 > 解决方案 > 如何为 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 请求中。

标签: node.jsexpresspostmongooserouter

解决方案


使用 Mongoose 的.findOneAndUpdate()第一个参数是查询文档,类似于.findOne(). 您的第二个参数是要更新的字段。$set是多余的,因为猫鼬会为你做这件事。

如果您想在建筑物中添加一个房间而不是替换所有关联,您需要添加带有$push.

const buildingFields = {
  $push: {
    room: {
      req.room.id
    }
  },
  name,
  type,
  ...
}

我还假设您希望 BuildingSchema 中的用户字段是单个关联而不是多个关联。如果没有,您需要使用 a$elemMatch来查询该文档:

Mongoose Mongodb 查询对象数组


推荐阅读