Find all documents in one collection, change data and save into another collection using mongoose and node express Find all documents in one collection, change data and save into another collection using mongoose and node express mongoose mongoose

Find all documents in one collection, change data and save into another collection using mongoose and node express


Try this.

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)    })})


Well, you do it a bit wrong. If you want to simply re-export your collection values (without modifying it) you'd better use such utilities like mongodump or mongoexport (depends on scenario) and then import it via mongorestore or mongoimport

If you want to handle your problem with JS code, it's fine, but code above is a bit wrong, why is that? Because if you deal with real huge number of documents in a collection, this code will never be finished.

So it's better to use find query with a cursor, like this:

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)   }}

This code will be guaranteed finished, because it takes every 10 documents (it depends on {batchSize: 10} and { parallel: 10} values) from your original collection_name and insert in one-by-one in 10 streams. Will allows you not load all the collection in your RAM.

And also allows you to modify the necessary data on-the-fly