0
我有一個子集合Id的列表並運行for循環。在該循環中,我嘗試查找父集合數據並插入另一個集合中。示例代碼如下節點Js Async for循環並在mongo中插入數據
for(let i=0; i<test.length; i++;){
db.collection('Parent').find({ "Id" : test[i].Id }).toArray(function(err, result){
if(result.length > 0){
db.collection('anotherCollection', function(err, collection){
collection.insert({
field1: test[i].field1,
field2: test[i].field2
})
})
}
})
}
當我嘗試執行此代碼的循環在集合之前完成insert.So我需要在每次迭代中插入集合。
我曾使用「async.eachSeries」並解決了這個問題。 –