How can I use a cursor.forEach() in MongoDB using Node.js?

前端 未结 10 858
你的背包
你的背包 2020-11-27 14:04

I have a huge collection of documents in my DB and I\'m wondering how can I run through all the documents and update them, each document with a different value.

10条回答
  •  旧时难觅i
    2020-11-27 14:57

    You can now use (in an async function, of course):

    for await (let doc of collection.find(query)) {
      await updateDoc(doc);
    }
    
    // all done
    

    which nicely serializes all updates.

提交回复
热议问题