[英]node, mongoose 'findOne' on one collection inside 'find' of another collection
[英]Find all documents in one collection, change data and save into another collection using mongoose and node express
我正在嘗試從 1 個集合中獲取 select 數據,刪除我不需要的字段,並添加一些新字段,並傳遞在每個文檔上為新字段復制的所需數據,並將它們保存到不同的集合中.
我是接近還是遠離? 任何幫助,將不勝感激。
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;
在 find 中提取的示例數據:
_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)
})
})
好吧,你做的有點不對。 如果您想簡單地重新導出您的集合值(不修改它),您最好使用諸如mongodump
或mongoexport
之類的實用程序(取決於場景),然后通過mongorestore
或mongoimport
導入它
如果你想用JS代碼處理你的問題,那很好,但是上面的代碼有點錯誤,這是為什么呢? 因為如果你在一個集合中處理大量的文檔,這段代碼永遠不會完成。
所以最好使用帶有 cursor 的查找查詢,如下所示:
async function t () {
try {
await collection_name.find({taskSunday: true}).lean().cursor({batchSize: 10}).eachAsync(async (document) => {
/**
* Your could write any logic here related with your document
*/
const taskSheet = new Tasksheet({
taskDate: document.taskDate,
taskTicket: document.taskTicket,
taskTime: document.taskTime,
taskCompany: document.taskCompany,
taskDescription: document.taskDescription,
taskCompleted: document.taskCompleted,
taskComments: document.taskComments,
taskCompletedBy: document.taskCompletedBy,
taskTimeStamp: document.taskTimeStamp,
taskKnowledgeTitle: document.taskKnowledgeTitle,
taskKnowledgeUrl: document.taskKnowledgeUrl,
taskType: document.taskType
});
taskSheet.save()
}, { parallel: 10})
} catch (e) {
console.error(e)
}
}
此代碼將保證完成,因為它從您的原始collection_name
中獲取每 10 個文檔(它取決於{batchSize: 10}
和{ parallel: 10}
值)並在 10 個流中一個接一個地插入。 將允許您不加載 RAM 中的所有集合。
並且還允許您即時修改必要的數據
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.