I have a function that is calling my atlas cluster 3 times just to find and update data. I would like to know if I can change the query below perhaps to use Aggregations to make it more succinct? It works , but looks very ameteruish! Help me improve the psudeo code below
//step 1 - findOne to obtain the results to use in the next stage
const dbResult = await mClient.db().collection(‘quicksend’).findOne({ slug: data.slug })
//step 2
if (!dbResult) {
throw Error(‘No Results’)
}
let fileObjects = dbResult.fileObjects // get the data I need
// Do some external processing on AWS s3 based on the dbResults
//step 3 - Get some userDAta from another collection with a filter from the post body
const userData = await mClient.db().collection(‘users’).findOne({_id: new ObjectId(data.userid)}, {projection : {fullName : 1, company : 1,email:1}})
//step 4
create a new array based on the result of step 3
// step 5 - Finally update the database with the data I need from the previous steps
await mClient.db().collection(‘quicksend’).findOneAndUpdate({ slug: data.slug }, {$set: {senderId : data.userid, senderEmail : userData.email,senderName: userData.fullName,company:userData.company,fileObjects:newFileObjectsPath }})
Above works, but as you can see I am calling the database 3 times. I think I can use lookup, but I need to do some external processing based on the result after step 1.
Is there anything wrong with what I am doing above?
Thanks.