gpt4 book ai didi

node.js - 使用 mongoose 和 node express 查找一个集合中的所有文档,更改数据并保存到另一个集合中

转载 作者:行者123 更新时间:2023-12-03 12:17:51 25 4
gpt4 key购买 nike

我正在尝试从 1 个集合中获取选择数据,删除我不需要的字段,并添加一些新字段,并传递正在为新字段在每个文档上复制的所需数据,并将它们保存到一个不同的集合。

我是接近还是远离?任何帮助将不胜感激。

router.post('', (req, res, next) => {
const taskDay = req.body.taskDay;
switch (taskDay) {
case 'taskSunday':
MasterTask.find({taskSunday: true}).forEach(tempData => {
const taskSheet = new Tasksheet({
taskDate: req.body.taskDate,
taskTicket: req.body.taskTicket,
taskTime: tempData.taskTime,
taskCompany: tempData.taskCompany,
taskDescription: tempData.taskDescription,
taskCompleted: req.body.taskCompleted,
taskComments: req.body.taskComments,
taskCompletedBy: req.body.taskCompletedBy,
taskTimeStamp: req.body.taskTimeStamp,
taskKnowledgeTitle: tempData.taskKnowledgeTitle,
taskKnowledgeUrl: tempData.taskKnowledgeUrl,
taskType: tempData.taskType
});
Tasksheet.insertMany(taskSheet).then(result => {
console.log(result);
res.status(200).json({message: 'task sheet created'})
});
});
break;

在查找中提取的示例数据:

    _id: "5f0acb7209e5981d10bb765a",
taskTime: "00:00",
taskCompany: "CompanyName",
taskDescription: "Some Needed Daily Task",
taskSunday: true,
taskMonday: true,
taskTuesday: true,
taskWednesday: true,
taskThursday: true,
taskFriday: true,
taskSaturday: true,
taskDisable: false,
taskEndOfMonth: null,
taskKnowledgeTitle: "Some Title",
taskKnowledgeUrl: "Some URL",
taskForSpecificDays: null,
taskType: "Core",
__v: 0
}

我试图保存到新集合的数据转换:

  {
id: "mongooseID",
taskTicket: "",
taskDate: null,
taskTime: "00:00",
taskCompany: "somename",
taskDescription: "This is an example description of a task. This is going to be an extra long line of text to test.",
taskCompleted: false,
taskComments: "",
taskCompletedBy: "",
taskTimeStamp: "",
taskKnowledgeTitle: "Test",
taskKnowledgeUrl: "http://www.google.com",
taskType: "Core"
}

最佳答案

试试这个。

MasterTask.find(
{
taskSunday: true
},
function(err, tempData) {
let taskSheet = [];
tempData.forEach(t => {
taskSheet.push({
taskDate: req.body.taskDate,
taskTicket: req.body.taskTicket,
taskTime: t.taskTime,
taskCompany: t.taskCompany,
taskDescription: t.taskDescription,
taskCompleted: req.body.taskCompleted,
taskComments: req.body.taskComments,
taskCompletedBy: req.body.taskCompletedBy,
taskTimeStamp: req.body.taskTimeStamp,
taskKnowledgeTitle: t.taskKnowledgeTitle,
taskKnowledgeUrl: t.taskKnowledgeUrl,
taskType: t.taskType
})
})

Tasksheet.insertMany(taskSheet, (err, t) => {
console.log(err)
console.log(t)
})
})

关于node.js - 使用 mongoose 和 node express 查找一个集合中的所有文档,更改数据并保存到另一个集合中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63030870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com