nodejs-使用猫鼬

时间:2020-08-05 17:42:11

标签: node.js mongodb mongoose memory promise

我正在使用nodejs和mongodb创建一个聊天应用程序。它还具有提醒功能。现在,用户可以一次创建多个提醒。每个提醒都是Reminder集合的mongo文档。用户从前端将数据作为多个提醒的数组发送。 示例:

 receivedReminders: [ 
           {
              title: 'Example Title 1',
              body: 'Example body 1 of fake title',
              date: DATE_ON_WHICH_TO_BE_REMINDED,
           },
           {
              title: 'Example Title 2',
              body: 'Example body 2 of fake title',
              date: DATE_ON_WHICH_TO_BE_REMINDED,
           },
           {
              title: 'Example Title 3',
              body: 'Example body 3 of fake title',
              date: DATE_ON_WHICH_TO_BE_REMINDED,
           },
        ]

现在我要做的是从其中创建猫鼬对象,并将它们全部并发保存到数据库中。这是我现在正在应用的技术:

const saveReminderPromises = [];
receivedReminders.forEach((curReminder) => {
  const newReminder= new Reminder({
    ...curReminder,
    userID,
  });
  saveReminderPromises.push(newReminder.save());
});
await Promise.all(saveReminderPromises); // Saving all the reminders

现在,我想知道这种方法还可以,还是会有任何陷阱?还有其他更好,有效的替代方法吗?因此会有内存问题吗?提前谢谢大家!

0 个答案:

没有答案