简体   繁体   English

如何使用nodejs和mongodb将单个表单的数据插入两个collections

[英]How to insert data of a single form into two collections using nodejs and mongodb

how do i insert the first three data coming from req.body into event collection and rest into personal collection我如何将来自 req.body 的前三个数据插入事件集合,将 rest 插入个人集合

app.post("/api/bookings", async(req, res) => {
          const {
            eventName,
            date,
            venue,
            firstname,
            lastname,
            email,
            contact,
            address1,
            address2,
            city,
            state,
            zip,
          } = req.body;

          try {
            await new Event(req.body).save();
            await new Personal(req.body).save()
            console.log("inserted");
          } catch (error) {
            console.log(error);
          } 
})

You can create two different objects for event and personal to store data您可以为事件和个人创建两个不同的对象来存储数据

app.post("/api/bookings", async(req, res) => {
          const {
            eventName,
            date,
            venue,
            firstname,
            lastname,
            email,
            contact,
            address1,
            address2,
            city,
            state,
            zip,
          } = req.body;

          const EventData = {
              eventName,
              data,
              venue
          }

          const PersonalData = {
               firstname,
                lastname,
                email,
                contact,
                address1,
                address2,
                city,
                state,
                zip,
          }
       
          try {
            await new Event(EventData).save();
            await new Personal(PersonalData).save()
            console.log("inserted");
          } catch (error) {
            console.log(error);
          } 
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用带有 nodejs 和 mongodb 的单一表单 texarea 插入多条记录 - How to insert multiple record using single form texarea with nodejs and mongodb 如何在nodejs API的现有数组中将数据插入或删除到mongodb集合中? - How to insert or delete data into mongodb collections in existing array for nodejs API? 如何使用 Mongodb Expressjs React js 和 Nodejs 在 2 个不同的 collections 中添加或插入数据? - How to add or insert data in 2 different collections using Mongodb Expressjs React js and Nodejs? 如何使用nodejs在mongodb中的两个不同集合中搜索值 - how to search for values in two different collections in mongodb using nodejs 在AngularJS,NodeJS,MongoDB,Mongoose和ExpressJS中以单一表单发布到多个集合 - Posting to multiple collections with a single form in AngularJS, NodeJS, MongoDB, Mongoose, and ExpressJS 使用MongoDB的NodeJS-尝试使用两个不同的集合数据 - NodeJS with MongoDB - Trying to use two different collections data 如何使用 mongoose 和 NodeJs 在 Mongodb 的数组字段中插入数据 - How to insert data in array field in Mongodb using mongoose and NodeJs 如何使用nodejs中的工作线程在mongoDB中插入新数据 - How to insert a new data in mongoDB using worker thread in nodejs 如何使用$ lookup和DbRef加入MongoDB和NodeJS中的两个集合? - How to Join two collections in MongoDB and NodeJS with $lookup and DbRef? 如何使用 express/nodejs 将下拉表单数据发布到 MongoDB? - How to POST dropdown form data to MongoDB using express/nodejs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM