【问题标题】:How to create a Post Request for Express/Mongoose Normalized Many-to-Many relationships如何为 Express/Mongoose 规范化多对多关系创建发布请求
【发布时间】:2020-11-01 12:06:45
【问题描述】:

我正在尝试学习如何在 express/router 和 Mongoose 中创建规范化的多对多 Post 请求。

我有三个集合:User、Building 和 Room,这也是父文档到子文档的顺序。

我的 Building 文档架构包括以下 User 和 Room 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.js express post mongoose router


    【解决方案1】:

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

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

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

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

    Mongoose Mongodb querying an array of objects

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-11
      • 2013-04-19
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      • 2020-06-14
      • 2019-08-11
      • 2014-07-29
      相关资源
      最近更新 更多