push nested loop async await mongodb

Solutions on MaxInterview for push nested loop async await mongodb by the best coders in the world

showing results for - "push nested loop async await mongodb"
Royce
23 Feb 2019
1//Import ObjectId method
2ObjectId = require('mongodb').ObjectID;
3
4//Get a promise from DB
5const getCollectionItem = (Collection, id) => {
6    return Collection.find({_id:ObjectId(id)});
7}
8
9//Iterate over a list of items to store them in a final list
10const forLoopPromisses = async (Collection, idList) => {
11    let finalList = [];
12
13    for (let index = 0; index < idList.length; index++) {
14        const id = idList[index];
15        //store the promise
16        const promss = await getCollectionItem(id);
17        //you can push promss to a list or do whatever to it next
18      	//Useful to do conditional updates & deletions
19        //Useful to do what u can't through mongodb aggregate
20        finalList.push(promss);
21    }
22    return finalList;
23}