【问题标题】:Mongoose saving dynamic array of subdocuments in one shotMongoose 一次性保存动态子文档数组
【发布时间】:2020-06-26 05:47:19
【问题描述】:

我搜索了高低,但没有找到解决方案。

我正在尝试保存一组子文档(即动态的)。

Here's my schema:

    const EventSchema = new Schema({
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'users'
  },
  title: {
    type: String,
    required: true
  },
  attendee:[ 
    {
      email: {
        type: String,
        required: true
      },
      name: {
        type: String,
        required: true
      },
      status: {
        type: String
      }
    }]
});

Here's the route:

router.post('/', auth, async (req, res) => {
  const {title, attendee: [{ email, name, status }] } = req.body

  try{  
    const newEvent = new Event({
        title,
        user: req.user.id,
        attendee: [{ email, name, status }]
    });

    const event = await newEvent.save();
    if (!event) throw Error('Something went wrong saving the event');

    res.status(200).json(event);

   
  catch (e) {
  res.status(400).json({ msg: e.message });
}
});

目前我只需要保存数组中的 1 个元素。

数组中的项目总是不同的。

我没有先创建“事件”然后添加“参与者”的选项。

Example of input:

{
    "title": "Something",
    "attendee": [
      {
        "email": "email@gmail.com",
        "name": "Bob"
      },
             {
        "email": "sandwich@gmail.com",
        "name": "Martha"
      }
    ]
  }

Output:

{
  "_id": "5ef1521f06a67811f74ba905",
  "title": "Something",
  "user": "5ecdaf3601cd345ddb73748b",
  "attendee": [
    {
      "_id": "5ef1521f06a67811f74ba906",
      "email": "email@gmail.com",
      "name": "Bob"
    }
  ],
  "__v": 0
}

【问题讨论】:

    标签: arrays mongodb mongoose


    【解决方案1】:

    您可以从请求正文中获取整个与会者数组并按原样保存,而不是对数组的一个对象进行解构。

    router.post('/', auth, async (req, res) => {
      
      const eventObj =  {
      user: req.user.id,
      title : req.body.title, 
      // get the whole array of attendee objects from the request
      attendee: req.body.attendee 
      } 
    
      try{  
        const newEvent = new Event(eventObj);
    
        const event = await newEvent.save();
        if (!event) throw Error('Something went wrong saving the event');
    
        res.status(200).json(event);
    
       
      catch (e) {
      res.status(400).json({ msg: e.message });
    }
    });
    

    【讨论】:

    • 大声笑,我觉得自己很笨。非常感谢!
    • 我现在正在尝试检查与会者是否是注册用户,然后再使用以下内容进行保存: if (!attendee) throw Error("用户不存在。");
    【解决方案2】:

    如果我理解正确,您不应该解构 attendee 并将每个与会者插入到新的 Event 中(选择在数据库中插入哪个键)。

    const {
      title,
      attendee,
    } = req.body;
    
    const newEvent = new Event({
      title,
      user: req.user.id,
    
      attendee: attendee.map(x => ({
        email: x.email,
        name: x.name,
        status: x.status,
      })),
    });
    

    【讨论】:

    • Salut Grégory,我最终使用了您的解决方案,因为现在我想搜索与会者的电子邮件并检查他们是否是注册用户。我无法为此创建 for 循环。
    • 您好,您的循环有问题是什么意思?我能提供什么帮助?
    • 我创建了一个新问题:stackoverflow.com/questions/63430469/… 感谢您的帮助,我的朋友!
    猜你喜欢
    • 2020-05-15
    • 2013-09-20
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2013-03-29
    • 1970-01-01
    • 2016-08-10
    相关资源
    最近更新 更多