I have two collections in mongoose db. I have to find all the documents in a collection. After that, i have to iterate all the documents to find the corresponding document in second collection. For that, i was thinking to use for loop. But, since it is blocking in nature. How can I perform my task.
const docs = await collection1.find({name:"asdf"})
for(let i=0;i<docs.length;i++){
const doc2 = await collection2.findOne({address:docs.address})
}
await collection2.findOne(...)), the main thread is taking care of "other things". Async/await does not block the event loop, in simple wordsawaitis just sugar-coating the nesting of promises and callbacks so that your code structure looks "flat". Something like reading a file withfs.readFileSync()inside the loop (or anywhere in your code) would block the event loop, but that's not the case withawait.